MySQL经典数据库SQL语句编写练习题——SQL语句扫盲
Posted 林加欣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL经典数据库SQL语句编写练习题——SQL语句扫盲相关的知识,希望对你有一定的参考价值。
【mysql】数据库原理复习——SQL语言
对基本的SQL语句编写的练习题,其中的题目的答案可能会有多种书写方式。
1、题目1
1.1 关系模式
学生student:SNO:学号,SNAME:姓名,AGE:年龄 SEX:性别
SNO |
SNAME |
AGE |
SEX |
1 |
23 |
男 |
|
2 |
22 |
女 |
|
5 |
22 |
男 |
课程course:CNO:课程代码,CNAME:课程名称,TEACHER:教师
CNO |
CNAME |
TEACHER |
K1 |
C语言 |
|
K5 |
数据库原理 |
程军 |
K8 |
编译原理 |
程军 |
学生成绩SC:SNO:学号,CNO:课程代码,SCORE:成绩
SNO |
CNO |
SCORE |
1 |
K1 |
83 |
2 |
K1 |
85 |
5 |
K1 |
92 |
2 |
K5 |
90 |
5 |
K5 |
84 |
5 |
K8 |
80 |
1.2 要求一
用SQL语言完成表的创建以及数据的插入:
- use test;
- create table if not exists student
- (
- SNO varchar(20) primary key,
- SNAME varchar(20) character set gbk,
- AGE int,
- SEX char(2) character set gbk CHECK(SEX IN(‘男‘,‘女‘))
- );
- insert into student values(‘1‘,‘李强‘,23,‘男‘);
- insert into student values(‘2‘,‘刘丽‘,22,‘女‘);
- insert into student values(‘5‘,‘张友‘,22,‘男‘);
- create table if not exists course
- (
- CNO varchar(20) primary key,
- CNAME varchar(20) character set gbk,
- TEACHER varchar(20) character set gbk
- );
- insert into course values(‘K1‘,‘C语言‘,‘王华‘);
- insert into course values(‘K5‘,‘数据库原理‘,‘程军‘);
- insert into course values(‘K8‘,‘编译原理‘,‘程军‘);
- create table if not exists sc
- (
- SNO varchar(20) NOT NULL,
- CNO varchar(20) NOT NULL,
- SCORE int NOT NULL,
- primary key (SNO,CNO),
- foreign key (SNO) references student(SNO),
- foreign key (CNO) references course(CNO)
- );
- insert into sc values(‘1‘,‘K1‘,83);
- insert into sc values(‘2‘,‘K1‘,85);
- insert into sc values(‘5‘,‘K1‘,92);
- insert into sc values(‘2‘,‘K5‘,90);
- insert into sc values(‘5‘,‘K5‘,84);
- insert into sc values(‘5‘,‘K8‘,80);
1.3 要求二
用SQL语言完成如下要求:
(1) 检索至少选修"程军"老师所授全部课程的学生姓名(SNAME);
本题的另外一种说法就是,检索选修了“程军”老师所授全部课程的学生姓名
- select sname from student
- where not exists
- (
- select * from course
- where teacher=‘程军‘ and not exists
- (
- select * from sc
- where sc.sno=student.sno and sc.cno=course.cno
- )
- );
这里的检索结果是:
+-------+
| sname |
+-------+
| 张友 |
+-------+
1 row in set (0.00 sec)
(2) 检索"李强"同学不学课程的课程号(CNO);
- select course.cno from course
- where course.cno not in
- (
- select sc.cno from sc,student
- where student.sname=‘李强‘ and student.sno=sc.sno
- );
+-----+
| cno |
+-----+
| K5 |
| K8 |
+-----+
2 rows in set (0.22 sec)
(3) 检索选修不少于3门课程的学生学号(SNO);
- SELECT sc.sno
- FROM sc
- GROUP BY sc.sno HAVING count(*)>=3
- ;
+-----+
| sno |
+-----+
| 5 |
+-----+
1 row in set (0.09 sec)
(4) 检索选修全部课程的学生姓名(SNAME)。
- SELECT sname FROM student
- WHERE NOT EXISTS
- (
- SELECT * FROM course
- WHERE NOT EXISTS
- (
- SELECT * FROM sc
- WHERE course.cno=sc.cno and student.sno=sc.sno
- )
- );
+-------+
| sname |
+-------+
| 张友 |
+-------+
1 row in set (0.02 sec)
(5) 检索不学"C语言"的学生信息
- SELECT * FROM student
- WHERE sno not in
- (
- SELECT sc.sno FROM sc,course
- WHERE course.cno=sc.cno
- );
1.4 要求三
请用SQL语言完成如下查询:
(1)查询“程军”老师所教授的所有课程;
- SELECT * FROM course
- WHERE teacher=‘程军‘;
- SELECT score FROM student,sc
- WHERE student.sname=‘李强‘ AND student.sno=sc.sno
(3)查询课程名为“C语言”的平均成绩;
- SELECT AVG(score) FROM sc,course
- WHERE course.cname=‘C语言‘ AND course.cno=sc.cno
(4)查询选修了所有课程的同学信息。
- SELECT * FROM student
- WHERE NOT EXISTS
- (
- SELECT * FROM course
- WHERE NOT EXISTS
- (
- SELECT * FROM sc
- WHERE course.cno=sc.cno AND student.sno=sc.sno
- )
- );
1.5 要求四
(1)检索王老师所授课程的课程号和课程名。
- SELECT cno,cname FROM course WHERE teacher LIKE ‘王%‘;
(2)检索年龄大于23岁的男学生的学号和姓名。
- SELECT sno,sname FROM student
- WHERE age>23;
(3)检索至少选修王老师所授课程中一门课程的女学生姓名。
- SELECT sname FROM student
- WHERE sex=‘女‘ AND sno IN
- (
- SELECT distinct sno FROM sc,course
- WHERE teacher LIKE ‘王%‘ AND sc.cno=course.cno
- );
- SELECT course.cno FROM course
- WHERE course.cno NOT IN
- (
- SELECT sc.cno FROM sc,student
- WHERE student.sname LIKE ‘李%‘ AND sc.sno=student.sno
- );
- SELECT sno FROM sc
- GROUP BY sno HAVING (count(*)>=2);
- SELECT course.cno,course.cname FROM course
- WHERE course.cno IN
- (
- SELECT cno FROM sc
- GROUP BY cno HAVing count(*)=(SELECT count(*) FROM student)
- );
- SELECT DISTINCT sno FROM sc
- WHERE cno IN
- (
- SELECT cno FROM course
- WHERE teacher LIKE ‘王%‘
- );
- SELECT count(*) NumberOfCourse
- FROM
- (
- SELECT DISTINCT cno FROM sc
- GROUP BY cno
- ) AS testTab;
- SELECT AVG(age) AverageOfAge
- FROM student
- WHERE sno IN
- (
- SELECT sno FROM sc
- WHERE cno=‘K1‘
- );
- SELECT sc.cno,AVG(sc.score)
- FROM sc
- WHERE sc.cno IN
- (
- SELECT course.cno FROM course
- WHERE course.teacher LIKE ‘王%‘
- )
- GROUP BY sc.cno
- SELECT cno,Num
- FROM
- (
- SELECT cno,count(*) as Num
- FROM sc
- GROUP BY cno HAVING (count(*) >= 2)
- ) tb_temp
- ORDER BY Num DESC,cno ASC
- ;
- SELECT stu1.sname
- FROM student stu1,
- (
- SELECT max(sno) snoLi,min(age) ageLi FROM student
- WHERE sname LIKE ‘李%‘
- ) AS stuLi
- WHERE (stu1.age < stuLi.ageLi) AND (stu1.sno>stuLi.snoLi)
- ;
- SELECT sname,age FROM student
- WHERE sname LIKE ‘李%‘
- ;
- SELECT sno,cno FROM sc
- WHERE score IS NULL;
- SELECT sname,age FROM student
- WHERE
- sex=‘男‘ AND age > (
- SELECT AVG(age)
- FROM student
- WHERE sex=‘女‘
- )
- ;
- SELECT sname,age
- FROM student
- WHERE sex=‘男‘ AND age > (
- SELECT max(age)
- FROM student
- WHERE sex=‘女‘
- )
- ;
以上是关于MySQL经典数据库SQL语句编写练习题——SQL语句扫盲的主要内容,如果未能解决你的问题,请参考以下文章
经典的SparkSQL/Hive-SQL/MySQL面试-练习题