高級查詢
1.連接查詢,對結果集列的擴展
select * from info
select * from info,nation #形成笛卡爾積
select * from info,nation where info.nation=nation.code
select info.code,info.name,sex,nation.name,birthday from info,nation where info.nation=nation.code
select * from info join nation on info.nation=nation.code
2.聯合查詢,對結果集行的擴展
select code,name from info
union
select code,name from nation
3.子查詢
父查詢:外層查詢
子查詢:里層查詢
子查詢的結果做為父查詢的條件
(1)無關子查詢
子查詢在執行的時候和父查詢沒有關系,子查詢可以單獨執行
1.查詢民族為‘漢族’的所有人員信息
父查詢:select * from info where nation=()
子查詢:select code from nation where name='漢族'
select * from info where nation=(select code from nation where name='漢族')
2.查詢系列名為‘寶馬5系’的所有汽車信息
select * from car where brand=(select brand_code from brand where brand_name='寶馬5系')
(2)相關子查詢
子查詢在執行的時候和父查詢有關系,子查詢不可以單獨執行
1.查詢汽車表中油耗小於該系列平均油耗的所有汽車信息
父查詢:select * from car where oil<(該系列平均油耗)
子查詢:select avg(oil) from car where brand=該系列
select * from car as a where oil<(select avg(oil) from car as b where b.brand=a.brand)