表結構
--學生表tblStudent(編號StuId、姓名StuName、年齡StuAge、性別StuSex)
--課程表tblCourse(課程編號CourseId、課程名稱CourseName、教師編號TeaId)
--成績表tblScore(學生編號StuId、課程編號CourseId、成績Score)
--教師表tblTeacher(教師編號TeaId、姓名TeaName)

CREATE TABLE tblStudent ( StuId INT, StuName nvarchar(32), StuAge INT, StuSex nvarchar(8) ) CREATE TABLE tblCourse ( CourseId INT, CourseName nvarchar(32), TeaId INT ) CREATE TABLE tblScore ( StuId INT, CourseId INT, Score INT ) CREATE TABLE tblTeacher ( TeaId INT, TeaName nvarchar(16) ) insert into tblStudent select 1,N'劉一',18,N'男' union all select 2,N'錢二',19,N'女' union all select 3,N'張三',17,N'男' union all select 4,N'李四',18,N'女' union all select 5,N'王五',17,N'男' union all select 6,N'趙六',19,N'女' insert into tblTeacher select 1,N'葉平' union all select 2,N'賀高' union all select 3,N'楊艷' union all select 4,N'周磊' insert into tblCourse select 1,N'語文',1 union all select 2,N'數學',2 union all select 3,N'英語',3 union all select 4,N'物理',4 insert into tblScore select 1,1,56 union all select 1,2,78 union all select 1,3,67 union all select 1,4,58 union all select 2,1,79 union all select 2,2,81 union all select 2,3,92 union all select 2,4,68 union all select 3,1,91 union all select 3,2,47 union all select 3,3,88 union all select 3,4,56 union all select 4,2,88 union all select 4,3,90 union all select 4,4,93 union all select 5,1,46 union all select 5,3,78 union all select 5,4,53 union all select 6,1,35 union all select 6,2,68 union all select 6,4,71
數據庫多表查詢之 where & INNER JOIN
在多表查詢中,一些SQL開發人員更喜歡使用WHERE來做join,比如:
SELECT a.ID, b.Name, b.Date FROM Customers a, Sales b WHERE a.ID = b.ID;
WHERE子句中使用的連接語句,在數據庫語言中,被稱為隱性連接。INNER JOIN……ON子句產生的連接稱為顯性連接。(其他JOIN參數也是顯性連接)WHERE 和INNER JOIN產生的連接關系,沒有本質區別,結果也一樣。但是!隱性連接隨着數據庫語言的規范和發展,已經逐漸被淘汰,比較新的數據庫語言基本上已經拋棄了隱性連接,全部采用顯性連接了。
缺點:在上面語句中,實際上是創建了兩張表的笛卡爾積,所有可能的組合都會被創建出來。在笛卡爾連接中,在上面的例子中,如果有1000顧客和1000條銷售記錄,這個查詢會先產生1000000個結果,然后通過正確的 ID過濾出1000條記錄。 這是一種低效利用數據庫資源,數據庫多做100倍的工作。 在大型數據庫中,笛卡爾連接是一個大問題,對兩個大表的笛卡爾積會創建數10億或萬億的記錄。
為了避免創建笛卡爾積,應該使用INNER JOIN :
SELECT a.ID, b.Name, b.Date FROM Customers a INNER JOIN Sales b ON a.ID = b.ID;
優點:如上面語句,使用inner join 這樣數據庫就只產生等於ID 的1000條目標結果。增加了查詢效率。
查詢及答案
1、查詢“1”課程比“2”課程成績高的所有學生的學號;
select st.StuId from tblStudent as st where (select sc1.Score from tblScore as sc1 where sc1.StuId=st.StuId and sc1.CourseId = '1') > (select sc2.Score from tblScore as sc2 where sc2.StuId=st.StuId and sc2.CourseId = '2')
2、查詢平均成績大於60分的同學的學號和平均成績;
第一種
select t.StuId,t.avgScore from (select sc.StuId,AVG(sc.Score) as avgScore from tblScore as sc group by sc.StuId ) as t where t.avgScore>60
第二種
Select StuId,Avg(Score) as AvgScore From tblScore Group By StuId Having Avg(Score)>60
3、查詢所有同學的學號、姓名、選課數、總成績;
第一種
select st.StuId,st.StuName,count(*) as courseCount,sum(sc.Score) as totalScore from tblStudent as st inner join tblScore as sc on st.StuId=sc.StuId group by st.StuId,st.StuName
第二種
Select StuId,StuName, SelCourses=(Select Count(CourseId) From tblScore t1 Where t1.StuId=s1.StuId), SumScore=(Select Sum(Score) From tblScore t2 Where t2.StuId=s1.StuId) From tblStudent s1
4、查詢姓“李”的老師的個數;
Select Count(*) From tblTeacher Where TeaName like '李%'
5、查詢沒學過“葉平”老師課的同學的學號、姓名;
select st.StuId,st.StuName from tblStudent as st where st.StuId not in ( select sc.StuId from tblScore as sc inner join tblCourse as c on c.TeaId = sc.CourseId inner join tblTeacher as t on t.TeaId = c.TeaId where TeaName ='葉平' )
http://www.cnblogs.com/edisonchou/p/3878135.html
https://blog.csdn.net/sujiacheng_123/article/details/53928474