SQL练习

--1.查询每门功课都大于80的学生
select name
from score
group by name
having min(score)>80;
--说明:以什么分组,就最好查询什么。

 

--2.所有部门之间的比赛组合
一个叫team的表,里面只有一个字段name,一共4条记录,分别是a,b,c,d,对应四个球队,
现在四个球队进行比赛,用一条sql语句显示所有可能的组合。
select
from team t1,team t2
where t1.name>t2.name;
name
1 a
2 b
3 c
4 d
 
满足要求后的结果
name name
1 d     c
2 d    b
3 d    a
4 c    b
5 c    a
6 b    a

 --说明:把一张表看成两张表



--3.显示文章标题 发帖人 最后回复时间
错误写法:max(postdate) m 是代表的整列中最大的,而不是各个分组中的最大的。
select title,postuser,m
from articles
group by name
having max(postdate) m;

 

正确写法:
select title,postuser,
(select max(postdate) from articles where a.parentid=a.id) reply
from articles a
where a.parentid is null;

 

--4.删除了id号不同,其它都相同的学生冗余信息

 delete from student2
 where id not in
       ( select min(id)
         from (select * from student2) as t
         group by t.name
        );

 

 

posted @ 2019-03-10 19:52  贰零一八  阅读(115)  评论(0编辑  收藏  举报