SQL 牛刀小试 1 —— 查询操作
Posted 落雨心星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 牛刀小试 1 —— 查询操作相关的知识,希望对你有一定的参考价值。
#创建数据库
create database ST CHARACTER set utf8;
#创建用户
create user ST identified by ‘19980510‘;
#授权用户操作该数据库
grant all on ST.* to ST;
----------------
#创建学生表
create table Student
(
Sno char(9) primary key ,
Sname char(20) not null,
Ssex char(2),
Sage smallint,
Sdept char(20)
);
#插入信息
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(201215121,‘李勇‘,‘男‘,20,‘CS‘);
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(201215122,‘刘晨‘,‘女‘,19,‘CS‘);
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(201215123,‘王敏‘,‘女‘,18,‘MA‘);
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(201215125,‘张立‘,‘男‘,19,‘IS‘);
--------------
#创建课程表
create table Course
(
Cno char(4) primary key,
Cname char(40) not null,
Cpno char(4),
Ccredit smallint,
foreign key(Cpno) references Course(Cno)
);
#插入信息
insert into Course(Cno,Cname,Cpno,Ccredit) values(1,‘数据库‘,5,4);
insert into Course(Cno,Cname,Cpno,Ccredit) values(2,‘数学‘,,2);
insert into Course(Cno,Cname,Cpno,Ccredit) values(3,‘信息系统‘,1,4);
insert into Course(Cno,Cname,Cpno,Ccredit) values(4,‘操作系统‘,6,3);
insert into Course(Cno,Cname,Cpno,Ccredit) values(5,‘数据结构‘,7,4);
insert into Course(Cno,Cname,Cpno,Ccredit) values(6,‘数据处理‘,,2);
insert into Course(Cno,Cname,Cpno,Ccredit) values(7,‘PASCAL语言‘,6,4);
-------------------
/*
[SQL]insert into Course(Cno,Cname,Cpno,Ccredit) values(1,‘数据库‘,5,4);
[Err] 1452 - Cannot add or update a child row: a foreign key constraint fails (`st`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`Cpno`) REFERENCES `course` (`Cno`))
*/
-- 解决方法 先不录入先修课(Cpno)的值
insert into Course(Cno,Cname,Ccredit) values(1,‘数据库‘,4);
insert into Course(Cno,Cname,Ccredit) values(2,‘数学‘,2);
insert into Course(Cno,Cname,Ccredit) values(3,‘信息系统‘,4);
insert into Course(Cno,Cname,Ccredit) values(4,‘操作系统‘,3);
insert into Course(Cno,Cname,Ccredit) values(5,‘数据结构‘,4);
insert into Course(Cno,Cname,Ccredit) values(6,‘数据处理‘,2);
insert into Course(Cno,Cname,Ccredit) values(7,‘PASCAL语言‘,4);
------------------
#创建选课表
create table SC
(
Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
foreign key(Sno) references Student(Sno),
foreign key(Cno) references Course(Cno)
);
#插入信息
insert into SC(Sno,Cno,Grade) values(201215121,1,92);
insert into SC(Sno,Cno,Grade) values(201215121,2,85);
insert into SC(Sno,Cno,Grade) values(201215121,3,88);
insert into SC(Sno,Cno,Grade) values(201215122,2,90);
insert into SC(Sno,Cno,Grade) values(201215122,3,80);
--------------------------
#查询全体学生的学号和姓名
select Sno,Sname from Student ;
---------
#查询全体学生的姓名、学号、所在系
select Sname,Sno,Sdept from Student;
---------
#查询全体学生的详细记录 (查询经过计算的值)
select * from Student;
-----------
#查询全体学生的姓名及其出生年份
select Sname,2018-Sage from Student;
-----------
#查询全体学生的姓名、出生年份和所在院系,要求用小写字母表示系名
select Sname,2018-Sage,lower(Sdept) from Student;
------------
#查询选修了课程的学生学号
select Sno from SC; -- select all Sno from SC; -- 有重复
select distinct Sno from SC; -- 无重复
------------
#查询计算机科学系全体学生的名单
select * from Student where Sdept = ‘CS‘;
------------
#查询所有年龄在20岁以下的学生姓名及其年龄
select Sname,Sage from Student where Sage<20;
------------
#查询考试成绩不合格的学生的学号
select Sno from SC where Grade<60;
------------
#查询年龄在20——23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄
select Sname,Sdept,Sage from Student where Sage>=20 and Sage<=23;
select Sname,Sdept,Sage from Student where Sage between 20 and 23;
------------
#查询年龄不在20——23岁之间的学生姓名、系别和年龄
select Sname,Sdept,Sage from Student where Sage not between 20 and 23;
-------------
#查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别
select Sname,Ssex from Student where Sdept in(‘CS‘,‘MA‘,‘IS‘);
select Sname,Ssex from Student where Sdept=‘CS‘ or Sdept=‘MA‘ or Sdept=‘IS‘;
-------------
#查询既不是计算机科学系、数学系、也不是信息系的学生的姓名和性别
select Sname,Ssex from Student where Sdept not in(‘CS‘,‘MA‘,‘IS‘);
-------------
#查询学号为201215121的学生的详细情况
select * from Student where Sno like ‘201215121‘;
select * from Student where Sno=‘201215121‘;
-- 注意:如果like后面的匹配串中不含通配符,则可以用 = (等于)运算符取代 like 谓词,用 != 或 <> (不等于)运算符取代 not like 谓词
-------------
#查询所有姓刘的学生的姓名、学号和性别
select Sname,Sno,Ssex from Student where Sname like ‘刘%‘;
-------------
#查询姓“欧阳”且全名为三个汉字的学生的姓名
select Sname from Student where Sname like ‘欧阳_‘;
-- 注意:数据库字符集为ASCII时一个汉字需要两个 _ ; 当字符集为GBK是只需要一个
--------------
#查询名字中第二个字为“阳”的学生的姓名和学号
select Sname,Sno from Student where Sname like ‘%阳%‘;
-------------
#查询所有不姓刘的学生的姓名、学号和性别
select Sname,Sno,Ssex from Student where Sname not like ‘刘%‘;
-------------
#查询DB_Design课程的课程号和学分
select Cno,Ccredit from Course where Cname like ‘DB\_Design‘ escape ‘\‘;
-------------
#查询以“DB_”开头,且倒数第三个字符为i的课程的详细情况
select * from Course where Cname like ‘DB\_%‘ escape ‘\‘;
-------------
#某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号
select Sno,Cno from SC where Grade is null; -- 注意这里的“IS”不能用 = (等号)代替
-------------
#查询计算机科学系年龄在20岁以下的学生姓名
select Sname from Student where Sdept = ‘CS‘ and Sage<20;
------------
#查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列
select Sno,Grade from SC where Cno=‘3‘ order by Grade desc; -- 降序
select Sno,Grade from Sc where Cno=‘3‘ order by Grade asc; -- 升序
-------------
/*
#查询全体学生情况,查询结果按所在系的系号升序排列,同一系中学生按年龄降序排列
select * from Student where order by Sdept,Sage desc; -- 默认为升序
[SQL]select * from Student where order by Sdept,Sage desc;
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your mysql server version for the right syntax to use near ‘order by Sdept,Sage desc‘ at line 1
*/
------------
#查询学生总人数
select count(*) from Student;
------------
#查询选修了课程的学生人数
select count(distinct Sno) from sc;
------------
#计算选修1号课程的学生平均成绩
select avg(Grade) from SC where Cno = ‘1‘;
------------
#查询选修1号课程的学生最高分数
select max(Grade) from SC where Cno = ‘1‘;
------------
#查询学生201215012选修课程的总学分数
select sum(Ccredit) from SC,Course where Sno = ‘201215122‘ and SC.Cno = Course.Cno;
------------
#求各个课程号及相应的选课人数
select Cno,count(*) from SC group by Cno;
-- group by 子句将查询结果按某一列或多列的值分组,值相等的为一组。
-- 分组后聚集函数将作用于每一个组,即每一组都有一个函数值
------------
#查询选修了三门以上课程的学生学号
select Sno from SC group by Sno having count(*)>3;
-- having 短语作用于组,从中选择满足条件的组
-------------
#查询平均成绩大于等于90分的学生学号和平均成绩
select Sno, avg(Grade) from SC where avg(Grade)>=90 group by Sno; -- 此句不正确,where子句中不能用聚集函数作为 条件表达式
select Sno, avg(Grade) from SC group by Sno having avg(Grade)>=90;
--------------------------------------------------------------- 连接查询
#查询每个学生及其选修课程的情况
select Student.*,SC.* from Student,SC where Student.Sno = SC.Sno;
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from Student,SC where Student.Sno = SC.Sno; -- 自然连接实现
--------
#查询选修2号课程且成绩在90分以上的所有学生的学号和姓名
select Student.Sno,Sname from SC,Student where Student.Sno = SC.Sno and SC.Cno=‘2‘ and SC.Grade >=90;
select student.Sno,Sname from Student inner join SC on(student.Sno = SC.Sno) where Cno= ‘2‘ and Grade >=90;
----------
#查询每一门课的间接先修课(即先修课的先修课).按照Cno降序排列
select first.Cno,second.Cpno from Course as first,Course as second where first.Cpno = second.Cno and Second.Cpno is NOT null order by first.Cno desc -- first,second 是Course表的两个别名
-----------
#查询全体学生的详细信息和所选课程号及成绩
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from Student left outer join SC on (Student.Sno = SC.Sno);
select Student.*,Cno,Grade from Student left outer join SC on (Student.Sno = SC.Sno);
-----------
#查询每个学生的学号、姓名、选修的课程名及成绩
select Student.Sno,Student.Sname,Course.Cname,SC.Grade from Student,Course,SC where Student.Sno = SC.Sno and Course.Cno = SC.Cno;
--------------------------------------------------------------- 嵌套查询
#查询与“刘晨”在同一个系学习的学生
select Student.* from Student where Sdept in (select Sdept from Student where Sname = ‘刘晨‘);
-----------
#查询选修了课程名为“信息系统”的学生学号和姓名
select Sno,Sname from Student where Sno in (select Sno from SC where Cno in (select Cno from Course where Cname = ‘信息系统‘));
select Student.Sno,Sname from Student,Course,SC where Course.Cname = ‘信息系统‘ and Student.Sno = SC.Sno and Course.Cno = SC.Cno;
----------
#找出每个学生超过他自己选修课程平均成绩的课程号
select Sno,Cno from SC x where Grade >= (select avg(Grade) from SC y where y.Sno = x.Sno);
-----------
#查询非计算机科学系中比计算机科学系任意一个学生年龄小于的学生姓名和年龄
select Sname,Sage from Student where Sage<any(select Sage from Student where Sdept = ‘CS‘)and Sdept<>‘CS‘;
select Sname,Sage from Student where Sage<(select max(Sage) from Student where Sdept = ‘CS‘)and Sdept<>‘CS‘;
-----------
#查询非计算机科学系中比计算机科学系所有学生年龄都小的学生姓名及年龄
select Sname,Sage from Student where Sage<all(select Sage from Student where Sdept = ‘CS‘)and Sdept<>‘CS‘;
select Sname,Sage from Student where Sage<(select min(Sage) from Student where Sdept = ‘CS‘)and Sdept<>‘CS‘;
-----------
#查询所有选修了1号课程的学生姓名
select Sname from Student where exists (select * from SC where Sno = student.Sno and Cno = ‘1‘);
select Sname,Sno from Student where Sno in (select Sno from SC where Cno = ‘1‘);
select Sname,Student.Sno,Grade from Student,SC where SC.Cno = ‘1‘ and student.Sno = SC.Sno;
------------
#查询没有选修1号课程的学生姓名
select Sno,Sname from Student where not exists (select * from SC where Sno = student.Sno and Cno = ‘1‘);
------------
#查询选修了全部课程的学生姓名
-----------
#查询至少选修了学生201215122选修的全部课程的学生号码
---------------------------------------------------------------- 集合查询
-- 注意:参加集合操作的各查询结果的列数必须相同;对应项的数据类型也必须相同
#查询计算机科学系的学生及年龄不大于19岁的学生
------------
#查询选修了课程1或者选修了课程2的学生
------------
#查询计算机科学系的学生与年龄不大于19岁的学生的交集
-------------
#查询既选修了课程1又选修了课2的学生
-------------
#查询计算机科学系的学生与年龄不大于19岁的学生的差集
2018-10-07 15:57:06
以上是关于SQL 牛刀小试 1 —— 查询操作的主要内容,如果未能解决你的问题,请参考以下文章