SQL语句练习
一、构建表结构
本文全部来自sql语句练习,我是用来进行练习。并对sql的一些基础进行一下记录。
构建一个Grade 的表结构,进行 SQL 语句练习。
|
|
二、练习
-
查询 Student 表中的所有记录的Sname、Ssex和Class 列。
注释
select * from table -- 这是一条注释
# 这是一条注释
/* 注释 */
简单的 检索数据 select 用法
1
SELECT Sname, Ssex, Class FROM student;
-
查询教师所有的单位即不重复的 Depart 列。
检索不同的值,使用 DISTINCT 关键词
1
SELECT DISTINCT depart FROM teacher;
-
查询Student 表的所有记录。
1
SELECT * FROM student;
-
查询 Score 表中成绩在60到 80 之间的所有记录。
过滤数据,where, between
1
SELECT * FROM score WHERE degree > 60 AND degree < 80;
-
查询 Score 表中成绩为85,86或88的记录。
高级过滤数据, where,in、or
1
SELECT * FROM score WHERE degree IN ( 85, 86, 88);
-
查询 Student 表中”95031“班 或性别为 ”女“的同学记录。
高级过滤数据,组合where 子句:and, OR
1
SELECT * FROM student WHERE class = '95031' AND Ssex = '女';
-
以 Class 降序查询 Student 表的所有记录。
排序检索数据,order by 、desc
数据排序默认是升序排序。ASC 没有多大用处。
1
SELECT * FROM student ORDER BY class DESC;
-
以 Cno 升序、Degree降序查询 Score 表的所有记录。
按照多个列进行排序,为每一个列都进行指定顺序
1
SELECT * FROM score ORDER BY Cno, Degree DESC;
-
查询 ”95031“ 班的学生人数。
汇总数据,count、avg、max、min、sum
1
SELECT COUNT(*) AS num FROM student WHERE class = '95031';
-
查询 Score 表中的最高分的学生学号和课程号。
子查询
1 2
SELECT sno, cno FROM score WHERE degree = ( SELECT MAX(degree) FROM score);
-
查询’3-105‘ 号课程的平均分。
AVG字段
1
SELECT AVG(degree) AS avgdegree FROM score WHERE Cno = '3-105';
-
查询 score 表中至少有5名学生选修的并以3开头的课程的平均分数。
分组数据:group by、having 过滤
group by 子句必须出现在 where 子句之后, order by 子句之后
1 2 3 4 5 6 7 8 9 10 11
select avg(DEGREE), CNO from SCORE where cno like '3%' group by CNO having count(*) > 5; 下面的方式是冗余的 SELECT AVG(degree) FROM score WHERE Cno LIKE '3%' AND Cno in (SELECT Cno FROM score GROUP BY Cno HAVING COUNT(*) >= 5);
-
查询最低分大于70,最高分小于90的sno列。
1
SELECT sno, MAX(degree), MIN(degree) FROM score GROUP BY sno HAVING MAX(degree) < 90 AND MIN(degree) > 70;
-
查询所有学生的 Sname、Con和Degree列。
联结:内连接、左外连接、右外连接、全外连接、笛卡儿积
1 2
SELECT Sname, Cno, Degree FROM student, score WHERE student.sno = score.sno; SELECT Sname, Cno, Degree FROM student INNER JOIN score ON student.sno = score.sno;
-
查询所有学生的 Sno、Cname和 Degree 列。
联结多个表数据
1
SELECT student.Sno, Cname, Degree FROM student, score, course WHERE student.sno = score.sno AND score.cno = course.cno;
-
查询所有学生的Sname、Cname和Degree列。
1
SELECT A.SNAME, C.cname, B.degree FROM student AS A INNER JOIN (score AS B, course AS C) ON A.sno = B.sno AND B.cno = C.cno;
-
查询“95033”班所选课程的平均分。
1 2 3 4 5 6 7 8
SELECT AVG(B.degree) FROM student AS A INNER JOIN score AS B ON A.sno = B.sno WHERE A.CLASS = '95033'; select avg(DEGREE) from SCORE where sno in (select SNO from STUDENT where CLASS = '95033');
-
假设使用如下命令建立了一个grade表:
1 2 3 4 5 6 7 8 9 10
create table grade ( low numeric(3, 0), upp numeric(3), rank 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');
现查询所有同学的Sno、Cno和rank列。
1 2 3 4 5 6 7
SELECT A.SNO, A.CNO, B.RANK FROM SCORE A, grade B WHERE A.DEGREE BETWEEN B.LOW AND B.UPP ORDER BY RANK;
-
查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
1 2
SELECT * FROM score WHERE cno = '3-105' and degree > ALL(SELECT degree FROM score where sno = '109'); SELECT * FROM score WHERE cno = '3-105' and degree > (SELECT MAX(degree) FROM score where sno = '109');
-
查询score中选学一门以上课程的同学中分数为非最高分成绩的学生记录
1 2 3 4 5 6
select * from STUDENT where SNO in (select SNO from SCORE where DEGREE < (select MAX(DEGREE) from SCORE) group by SNO having count(*) > 1);
-
查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录.
1 2 3 4 5 6 7
select * from SCORE where CNO = '3-105' and DEGREE > ALL ( select DEGREE from SCORE where SNO = '109' );
-
查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
数据处理函数,时间处理
1
SELECT Sno, Sname, Sbirthday FROM student WHERE YEAR(Sbirthday) = (SELECT YEAR(Sbirthday) FROM student WHERE sno = 108);
-
查询“张旭“教师任课的学生成绩。
1 2 3 4 5 6 7
select * from SCORE where cno = ( select CNO from COURSE inner join TEACHER on COURSE.TNO = TEACHER.TNO and TNAME = '张旭' );
-
查询选修某课程的同学人数多于5人的教师姓名。
1 2 3
SELECT tname FROM teacher WHERE tno = ( SELECT tno FROM course WHERE cno = ( SELECT cno FROM score GROUP BY cno HAVING COUNT(sno) > 5));
-
查询95033班和95031班全体学生的记录。
1 2 3
select * from STUDENT where CLASS in ('95033', '95031');
-
查询存在有85分以上成绩的课程Cno
1
SELECT cno FROM score GROUP BY cno HAVING MAX(degree) > 85;
-
查询出“计算机系“教师所教课程的成绩表
1 2 3
SELECT * FROM score WHERE cno IN ( SELECT cno FROM course WHERE tno IN ( SELECT tno FROM teacher WHERE depart = '计算机系'));
-
查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof
1 2 3 4 5 6 7 8 9
select tname, prof from TEACHER where depart = '计算机系' and prof not in ( select prof from TEACHER where depart = '电子工程系' );
-
查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
1 2 3 4 5
SELECT cno, sno, degree FROM score WHERE cno = '3-105' AND degree > any ( SELECT degree FROM score WHERE cno = '3-245' ) ORDER BY degree desc;
-
查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
1 2 3 4 5
SELECT cno, sno, degree FROM score WHERE cno = '3-105' AND degree > ALL ( SELECT degree FROM score WHERE cno = '3-245' ) ORDER BY degree desc;
-
查询所有教师和同学的name、sex和birthday
组合查询:union
1 2 3 4 5 6 7 8 9 10 11
select TNAME name, TSEX sex, TBIRTHDAY birthday from TEACHER union select sname name, SSEX sex, SBIRTHDAY birthday from STUDENT;
-
查询所有“女”教师和“女”同学的name、sex和birthday
1 2 3 4 5 6 7 8 9 10 11 12 13
select TNAME name, TSEX sex, TBIRTHDAY birthday from TEACHER where TSEX = '女' union select sname name, SSEX sex, SBIRTHDAY birthday from STUDENT where SSEX = '女';
-
视图
视图是虚拟的表,本身不包含数据,也就不能对其进行索引操作。
对视图的操作和对普通表的操作一样。
视图具有如下好处:
- 简化复杂的 SQL 操作,比如复杂的连接;
- 只使用实际表的一部分数据;
- 通过只给用户访问视图的权限,保证数据的安全性;
- 更改数据格式和表示。
1 2 3 4
CREATE VIEW myview AS SELECT Concat(col1, col2) AS concat_col, col3*col4 AS compute_col FROM mytable WHERE col5 = val;