(用來進行兩個或以上表之間的查詢)
1、首先新建一個bumen表和一個haha表,填充數據
2、利用兩表進行子查詢:
--部門人數大于5的部門中最大年齡的人的信息---select bumen as 部門,COUNT(*)as 人數 from haha group by bumen having COUNT(*)>5 select MAX(age) from haha where bumen = '銷售部'select *from haha where bumen = '銷售部' and age = 45---子查詢select *from haha where age in (select MAX(age) from haha where bumen = '銷售部')and bumen in (select bumen from haha group by bumen having COUNT(*)>5 )-------練習1:按年齡從小到大排序后第6、7、8人的信息select top 3 *from haha where code not in(select top 5 code from haha order by age)order by age--另一種select *from haha where code not in(select top 5 code from haha order by age)and code in(select top 8 code from haha order by age)-------練習2:查找男生里面年齡最大的人的信息select top 1 *from haha where sex in(select sex from haha where sex = '男') order by age-------練習3:查找人數最多的部門35歲的人的信息select *from haha where bumen in (select top 1 bumen from haha group by bumen order by COUNT(*) desc)and age = 35--select MAX(COUNT(*))from haha order by bumen-----------分頁--------------select top 5*from haha where code not in(select top 10 code from haha)----------一個表按照規定的條目(5)能分多少頁(為防止自動默認為int型不能用5整型)select ceiling(COUNT(*)/5.0)from haha
新聞熱點
疑難解答