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

相关文章
相关标签/搜索