数据库查询操练习

数据库查询操练习(linux操作)

1、 查询students表中的所有记录的sname、ssex和class列。
MariaDB [Blog]> select sname,ssex,class from students;

2、 查询教师所有的单位即不重复的Depart列。
MariaDB [Blog]> select distinct depart from teachers;

3、 查询students表的所有记录。
MariaDB [Blog]> select * from students;

4、 查询scores表中成绩在60到80之间的所有记录。
MariaDB [Blog]> select * from scores where degree between 60 and 80;

5、 查询scores表中成绩为85,86或88的记录。
MariaDB [Blog]> select * from scores where degree=85 or degree=86 or degree=88;
MariaDB [Blog]> select * from scores where degree in (85,86,88);

6、 查询students表中“95031”班或性别为“女”的同学记录。(作业)
select * from students where xxxxxx or xxxxx;

查询课程名称包含数学的课程信息。(模糊查询)
MariaDB [Blog]> select * from courses where cname like ‘%数学%’;
MariaDB [Blog]> select * from courses where cname like ‘__数学’;

======
1). 查询95033班和95031班全体学生的记录。
2). 查询存在有85分以上成绩的课程Cno.
3). 查询Student表中不姓“王”的同学记录。
4). 查询所有任课教师的Tname和Depart.
5). 查询所有未讲课的教师的Tname和Depart.

关于排序******
7、 以class降序查询students表的所有记录。
MariaDB [Blog]> select * from students order by class desc;
MariaDB [Blog]> select * from students order by class;(默认升序)

8、 以cno升序、degree降序查询scores表的所有记录。
以cno升序、degree降序: 当cno相同时, 按照degree降序排列。
cno degree
1 3
1 2
2 3
MariaDB [Blog]> select * from scores order by cno,degree desc;

*******关于聚合函数
9、 查询“95031”班的学生人数。
MariaDB [Blog]> select * from students where class=‘95031’;
MariaDB [Blog]> select count() from students where class=‘95031’;
MariaDB [Blog]> select count(
) as studentCount from students where class=‘95031’; (最终版)

10、查询‘3-105’号课程的平均分。
MariaDB [Blog]> select avg(degree) as avgScore from scores where cno=‘3-105’;

====
查询Student表中最大和最小的Sbirthday日期值。

*************关于group by 和having
11、查询scores表中至少有5名学生选修的并以3开头的课程的平均分数。
1). 至少有5名学生选修(聚合函数): count(
) >= 5
2). 以3开头的课程(模糊查询): cno like ‘3%’
3). 平均分数(聚合函数): avg(degree)
select cno,avg(degree) from scores where cno like ‘3%’ group by cno having count(cno)>=5;

12、查询最低分大于70,最高分小于90的Sno列。
select * from scores where max(degree) < 90 and min(degree) > 70;(错误的SQL语句)
MariaDB [Blog]> select sno from scores group by sno having max(degree)<90 and min(degree)>70;(正确的)

======
查询scores中选学一门以上课程的同学中分数为非最高分成绩的记录。

  • 非最高分成绩: degree != max(degree)
  • 选学一门以上课程: count(cno) > 1
  • 分组: sno
    MariaDB [Blog]> select * from scores group by sno having count(cno)>1 and degree!=max(degree);

**************关于limit
13、查询scores表中的最高分的学生学号和课程号。
select sno,cno from scores order by degree desc limit 1;(不是最优的方式, 如果最高分有多名学生会出现问题)
MariaDB [Blog]> select sno,cno from scores where degree=(select max(degree) from scores);(较优的方式)

查询成绩最高的前5名学生信息。
MariaDB [Blog]> select * from scores order by degree desc limit 5;(默认从第0个索引开始, 拿前5条数据)
±----±------±-------+
| sno | cno | degree |
±----±------±-------+
| 103 | 3-105 | 92.0 |
| 107 | 3-105 | 91.0 |
| 105 | 3-105 | 88.0 |
| 103 | 3-245 | 86.0 |
| 101 | 6-166 | 85.0 |
±----±------±-------+
5 rows in set (0.00 sec)

MariaDB [Blog]> select * from scores order by degree desc limit 2,3;(从第2个索引开始获取3条数据)

±----±------±-------+
| sno | cno | degree |
±----±------±-------+
| 105 | 3-105 | 88.0 |
| 103 | 3-245 | 86.0 |
| 101 | 6-166 | 85.0 |
±----±------±-------+
3 rows in set (0.01 sec)

****多表查询
eg:
A表:
aid num
1 10
2 20
3 30

B表:
bid bname
1 粉条
3 粉丝
4 粉带

left join(左联接): select * from A left join B on A.aid = b.bid;
right join(右联接): select * from A right join B on A.aid = b.bid;
inner join(等值联接): select * from A inner join B on A.aid = b.bid;

14、查询所有学生的sname、cno和degree列。
MariaDB [Blog]> select sname,cno,degree from students inner join scores on students.sno=scores.sno;
15、查询所有学生的sname、cname和degree列。
MariaDB [Blog]> select distinct sname,cname,degree from students inner join scores
on(students.sno=scores.sno) inner join courses on(scores.cno=courses.cno) order by sname;

16、查询所有学生的Sname、Cname和Degree列。

17、查询“95033”班所选课程的平均分。

数据库常见的面试总结,练习:
https://blog.csdn.net/qq_21156327/article/details/103922492

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值