MySql练习---[SQL查询语句练习]
Posted 小智RE0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySql练习---[SQL查询语句练习]相关的知识,希望对你有一定的参考价值。
首先完成建表,插入数据:
建表:
CREATE TABLE students
(sno VARCHAR(3) NOT NULL,
sname VARCHAR(4) NOT NULL,
ssex VARCHAR(2) NOT NULL,
sbirthday DATETIME,
class VARCHAR(5))
CREATE TABLE courses
(cno VARCHAR(5) NOT NULL,
cname VARCHAR(10) NOT NULL,
tno VARCHAR(10) NOT NULL)
CREATE TABLE scores
(sno VARCHAR(3) NOT NULL,
cno VARCHAR(5) NOT NULL,
degree NUMERIC(10, 1) NOT NULL)
CREATE TABLE teachers
(tno VARCHAR(3) NOT NULL,
tname VARCHAR(4) NOT NULL, tsex VARCHAR(2) NOT NULL,
tbirthday DATETIME NOT NULL, prof VARCHAR(6),
depart VARCHAR(10) NOT NULL)
插入数据:
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' ,'1977-09-01',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,'1975-10-02',95031);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,'1976-01-23',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,'1976-02-20',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,'1975-02-10',95031);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,'1974-06-03',95031);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,100);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT Sname,Ssex,Class FROM Students;
2、 查询教师所有的单位即不重复的Depart列。
SELECT DISTINCT Depart FROM teachers ;
3、 查询Student表的所有记录。
SELECT * FROM Students;
4.查询Score表中成绩在60到80之间的所有记录。
SELECT * FROM scores WHERE degree BETWEEN 60 AND 80;
5、 查询Score表中成绩为85,86或88的记录。
SELECT * FROM scores WHERE degree IN(85,86,88);
6、 查询Student表中“95031”班或性别为“女”的同学记录。
SELECT * FROM students WHERE class=95031 OR ssex='女';
7、 以Class降序查询Student表的所有记录。
SELECT * FROM students ORDER BY class DESC;
8.以Cno升序、Degree降序查询Score表的所有记录。
SELECT * FROM scores ORDER BY cno ASC , degree DESC;
9、 查询“95031”班的学生人数。
SELECT count(*)95031班的人数 FROM students WHERE class=95031;
10、查询Score表中的最高分的学生学号和课程号。
SELECT sno,cno FROM scores WHERE degree=(SELECT max(degree)FROM scores);
11、查询‘3-105’号课程的平均分。
SELECT avg(degree)平均成绩 FROM scores WHERE cno='3-105';
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT avg(degree)平均分数 FROM scores WHERE cno LIKE '3%' GROUP BY cno HAVING count(cno)>4;
13、查询最低分大于70,最高分小于90的Sno列。
SELECT sno FROM scores GROUP BY sno HAVING min(degree)>70 AND max(degree)<90;
14、查询所有学生的Sname、Cno和Degree列。
SELECT sname,s2.cno,s2.degree
FROM students s1,scores s2
WHERE
s1.sno = s2.sno;
15、查询所有学生的Sno、Cname和Degree列。
SELECT sno,cname,degree
FROM courses,scores
WHERE courses.cno = scores.cno;
16、查询所有学生的Sname、Cname和Degree列。
SELECT sname,cname,degree
FROM students,courses,scores
WHERE students.sno = scores.sno
AND
scores.cno = courses.cno;
17、查询“95033”班所选课程的平均分。
SELECT avg(degree)
FROM students,scores
WHERE students.sno = scores.sno
AND
class = '95033';
18、假设使用如下命令建立了一个grade表:
create table grade(low int,upp int,ranks char(1));
insert into grade values(90,100,’A’);
insert into grade values(80,89,’B’);
insert into grade values(70,79,’C’);
insert into grade values(60,69,’D’);
insert into grade values(0,59,’E’);
commit;
现查询所有同学的Sno、Cno和ranks列。
SELECT sno,cno,ranks
FROM scores,grade
WHERE scores.degree
BETWEEN grade.low
AND grade.upp;
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SELECT
*
FROM
scores s
WHERE s.cno = '3-105'
AND s.degree >
(SELECT
degree
FROM
scores e
WHERE e.sno = '109'
AND e.cno = '3-105');
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
SELECT
*
FROM
scores a
WHERE sno IN
(SELECT
sno
FROM
scores
GROUP BY sno
HAVING count (sno) > 1)
AND degree <
(SELECT
max (degree)
FROM
scores b
WHERE b.cno = a.cno);
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
SELECT
*
FROM
scores s
WHERE s.degree >
(SELECT
degree
FROM
scores e
WHERE e.sno = '109'
AND e.cno = '3-105');
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SELECT
Sno,Sname,Sbirthday
FROM
students
WHERE YEAR (sbirthday) =
(SELECT
YEAR (sbirthday)
FROM
students
WHERE sno= 108);
23、查询“张旭“教师任课的学生成绩。
SELECT
degree
FROM
courses s
JOIN teachers t
ON t.tno = s.tno
JOIN scores e
ON s.cno = e.cno
WHERE t.tname = '张旭';
24、查询选修某课程的同学人数多于5人的教师姓名。
SELECT
Tname
FROM
Teachers
WHERE Tno =
(SELECT
Tno
FROM
Courses
WHERE Cno =
(SELECT
Cno
FROM
Scores
GROUP BY Cno
HAVING count(Cno) >= 5));
25、查询95033班和95031班全体学生的记录。
SELECT * FROM students WHERE class='95033'OR class='95031' ORDER BY class;
26、查询存在有85分以上成绩的课程Cno.
SELECT DISTINCT cno FROM scores WHERE degree>85;
27、查询出“计算机系“教师所教课程的成绩表。
SELECT
degree
FROM
scores s
JOIN courses c
ON c.cno = s.cno
JOIN teachers t
ON t.tno = c.tno
WHERE t.depart = '计算机系';
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
SELECT
tname,
prof
FROM
teachers
WHERE prof NOT IN
(SELECT
prof
FROM
teachers
WHERE depart = '电子工程系'
AND prof IN
(SELECT
prof
FROM
teachers
WHERE depart = '计算机系'))
AND depart IN (
'计算机系',
'电子工程系'
);
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
SELECT
*
FROM
scores
WHERE cno = '3-105'
AND degree >
(SELECT
max(degree)
FROM
scores
WHERE cno = '3-245')
ORDER BY degree DESC;
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
SELECT
*
FROM
scores
WHERE cno = '3-105'
AND degree >
(SELECT
max(degree)
FROM
scores
WHERE cno = '3-245');
31、查询所有教师和同学的name、sex和birthday.
SELECT sname 教师和学生的姓名,ssex,sbirthday FROM students UNION SELECT tname,tsex,tbirthday FROM teachers ;
32、查询所有“女”教师和“女”同学的name、sex和birthday.
SELECT
sname 女教师和女同学的姓名,
ssex,
sbirthday
FROM
students
WHERE ssex = '女'
UNION
SELECT
tname,
tsex,
tbirthday
FROM
teachers
WHERE tsex = '女';
33、查询成绩比该课程平均成绩低的同学的成绩表。
SELECT * FROM scores s WHERE s.degree<(SELECT avg(degree) FROM scores e WHERE e.cno=s.cno GROUP BY cno);
34、查询所有任课教师的Tname和Depart.
SELECT
t.depart,
t.tname
FROM
teachers t
WHERE t.tno IN
(SELECT
tno
FROM
courses c
WHERE c.cno IN
(SELECT
cno
FROM
scores
GROUP BY cno));
35 查询所有未讲课的教师的Tname和Depart.
SELECT
tname,
depart
FROM
teachers
WHERE tno NOT IN
(SELECT DISTINCT
tno
FROM
courses
WHERE cno IN
(SELECT DISTINCT
cno
FROM
scores));
36、查询至少有2名男生的班号。
SELECT class FROM students WHERE ssex='男' GROUP BY class HAVING count(*)>=2 ;
37、查询Student表中不姓“王”的同学记录。
SELECT * FROM students WHERE sname SQL语句练习