在面試過程當中屢次碰到兩道SQL查詢的題目,一是查詢A(ID,Name)表中第31至40條記錄,ID做爲主鍵多是不是連續增加的列,完整的查詢語句以下:
select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by ID) as T) order by ID
另一道題目的要求是查詢表A中存在ID重複三次以上的記錄,並顯示工有多少個;完整的查詢語句以下:
select id, Count(*) from A group by id having count(*)>1 個數
select * from xs where id1 in (select id1 from xs group by id1 having count(*)>2 ) 數據
以上兩道題目很是有表明意義,望各位把本身碰到的有表明的查詢都貼上來。
分析:一:
1。
select top 6 ID1 from xs order by ID1 對XS表中的前6行按ID字段進行上升排序。
2。
select max(ID1) from (select top 6 ID1 from xs order by ID1)as T 查詢排序後的表(6列)的最大ID
3。select top 4 * from xs where ID1 >(select max(ID1) from (select top 6 ID1 from xs order by ID1) T) order by ID1 查詢當ID1大於最大ID的前4行的數據
二:
1。select id1 from xs group by id1 having count(*)>2 查詢ID1重複3次的ID的值X
2。select * from xs where ID1 in (select id1 from xs group by id1 having count(*)>2) 查詢ID1=X的數據
3 select distinct * from xs where id1 in(select id1 from xs group by id1 having count(*)>3) 這樣就能夠祛除重複值了