数据库(一)-- 常用查询语句

来源:互联网 发布:linux shell脚本入门 编辑:程序博客网 时间:2024/06/07 10:27

很多查询都可以用经典的学生-课程-成绩案例来表示,下面是一些我在笔试或面试中遇到的经典题型。这些题目没有涉及到索引,函数,存储过程等高级功能,都是一些基本的查询语句,但难度却不小。

表结构

  • 学生表student(id,name)
  • 学生课程表student_course(sid,cid,score)

问题

1.    查询student表中重名的学生,结果包含id和name,按name,id升序

2.  select id,name
3.  from student
4.  where name in (
5.  select name from student group by name having(count(*) > 1)
6.  ) order by name,id;

我们经常需要查询某一列重复的行,一般通过group by(有重复的列)然后取count>1的值。 关系型数据库有他的局限性, 有些看似简单的查询写出来的sql很复杂,而且效率也会很低。

7.    在student_course表中查询平均分不及格的学生,列出学生id和平均分

8.  select sid,avg(score) as avg_score
9.  from student_course
10. group by sid having(avg_score<60);

group by和having是最常考的。 where子句中不能用聚集函数作为条件表达式,但是having短语可以,where和having的区别在于对用对象不同,where作用于记录,having作用于组

11.  在student_course表中查询每门课成绩都不低于80的学生id

12. select distinct sid
13. from student_course
14. where sid not in (
15. select sid from student_course
16. where score < 80);

用到反向思想,其实就是数理逻辑中的x:P¬x:¬P是等价的。

17.  查询每个学生的总成绩,结果列出学生姓名和总成绩 如果使用下面的sql会过滤掉没有成绩的人

18. select name,sum(score) total
19. from student,student_course
20. where student.id=student_course.sid
21. group by sid;

更保险的做法应该是使用**左外连接**

select name,sum(score)
from student left join student_course
on student.id=student_course.sid
group by sid;

22.  总成绩最高的学生,结果列出学生id和总成绩 下面的sql效率很低,因此要重复计算所有的总成绩。

23. select sid,sum(score) as sum_score
24. from student_course group by sid having sum_score>=all
25. (select sum(score) from student_course group by sid);

因为order by中可以使用聚集函数,最简单的方法是:

select sid,sum(score) as sum_score
from student_course group by sid
order by sum_score desc limit 1;

同理可以查总成绩的前三名。

26.  在student_course表查询课程1成绩第2高的学生,如果第2高的不止一个则列出所有的学生 先查出第2高的成绩:

27. select min(score) from student_course where cid = 1 order by score desc limit 2;

然后再取出该成绩对应的学生:

select * from student_course
where cid=1 and score = (
select min(score)
from student_course where cid = 1 order by score desc limit 2);

类似的,可以查询**某个值第N高**的记录。

28.  在student_course表查询各科成绩最高的学生,结果列出学生id、课程id和对应的成绩 你可能会这样写:

29. select sid,cid,max(score) from student_course group by cid;

然而上面是不对的,因为**使用了group by的查询字段只能是group by中的字段或者聚集函数**。 虽然不会报错,但是sid是无效的,如果去掉sid的话只能查出没门课程的最高分,不包含学生id。 本题的正确解法是使用相关嵌套查询:

select * from A as x where score>=
(select max(score) from A as y where y.clsid=x.clsid);

相关嵌套查询也就是在进行内层查询的时候需要用到外层查询,有一些注意事项:

    • 子查询一定要有括号
    • as可以省略
    • 使用相关查询;>=max等价于>=all,但是聚合函数比使用any或all效率高

30.  在student_course表中查询每门课的前3名,结果按班级课程id升序,同一课程按成绩降序 类似的查询在csdn上也有征集答案

31. select * from student_course x where
32. 3>(select count(*) from student_course y where y.cid=x.cid and y.score>x.score)
33. order by cid,score desc;

这也是一个相关嵌套查询,对于每一个分数,如果同一门课程下只有0个、1个或2个分数比这个分数还高,那么这个分数肯定是前3名之一

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

35. select a.name, b.name
36. from team a, team b
37. where a.name < b.name

其实就是一个表和自己连接查询。

38.  题目:数据库中有一张如下所示的表,表名为sales。

季度

销售

1991

1

11

1991

2

12

1991

3

13

1991

4

14

1992

1

21

1992

2

22

1992

3

23

1992

4

24

39.  要求:写一个SQL语句查询出如下所示的结果。

一季度

二季度

三季度

四季度

1991

11

12

13

14

1992

21

22

23

24

40. select 年, 
41. sum(case when 季度=1 then 销售量 else 0 end) as 一季度, 
42. sum(case when 季度=2 then 销售量 else 0 end) as 二季度, 
43. sum(case when 季度=3 then 销售量 else 0 end) as 三季度, 
44. sum(case when 季度=4 then 销售量 else 0 end) as 四季度 
45. from sales group by 年;
0 0
原创粉丝点击