Mysql常见面试题(1)

Mysql常见面试题(1)

1、每门课都大于80分的学生姓名

select name from table1 GROUP BY name having min(fenshu)>80;(推荐)

select DISTINCT name from table1 where name not in (select DISTINCT name from table1 where fenshu<=80);

Mysql常见面试题(1)

2、删除除了编号不同,其他都相同的冗余学生信息

delete from table2 where bianhao not in (select min(bianhao) from table2 GROUP BY xuehao,name,kechenghao,kecheng,fenshu);

注意:这么写MySQL会报错,You can't specify target table 'table2' for update in FROM clause。这个错误表示不能在同一表中查询的数据作为同一表的更新数据。

应该这么写:delete from table2 where bianhao not in (select a.bianhao from (select * from  table2 GROUP BY xuehao,name,kechenghao,kecheng,fenshu)a)

3、一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球队,现在四个球队进行比赛,用一条sql 语句显示所有可能的比赛组合.

select * from team a,team b where a.name>b.name;