sql常見面試(2)

1.sql 刪除表中重複數據保留一條面試

1)刪除表中多餘的重複記錄,重複記錄是根據單個字段(peopleId)來判斷,只留有rowid最小的記錄 sql

delete from people 
where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1) 
and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1) 數學

 

2)刪除表中多餘的重複記錄(多個字段),只留有rowid最小的記錄 it

delete from vitae a 
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) 
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1) table

 

2.查詢出每門課都大於80 分的學生姓名select

select distinct name from table where name not in (select distinct name from table where fenshu<=80)數據

select name from table group by name having min(fenshu)>80查詢

 

3.刪除除了自動編號不一樣, 其餘都相同的學生冗餘信息tab

自動編號   學號   姓名 課程編號 課程名稱 分數
1        2005001 張三 0001     數學    69
2        2005002 李四 0001      數學    89
3        2005001 張三 0001      數學    69di

delete tablename where 自動編號 not in(select min( 自動編號) from tablename group by學號, 姓名, 課程編號, 課程名稱, 分數)

 

4.面試題:怎麼把這樣一個表兒year   month amount1991   1     1.11991   2     1.21991   3     1.31991   4     1.41992   1     2.11992   2     2.21992   3     2.31992   4     2.4查成這樣一個結果year m1   m2   m3   m41991 1.1 1.2 1.3 1.41992 2.1 2.2 2.3 2.4 答案1、select year, (select amount from   aaa m where month=1   and m.year=aaa.year) as m1,(select amount from   aaa m where month=2   and m.year=aaa.year) as m2,(select amount from   aaa m where month=3   and m.year=aaa.year) as m3,(select amount from   aaa m where month=4   and m.year=aaa.year) as m4from aaa   group by year

相關文章
相關標籤/搜索