1. 程式人生 > >sql 經典四表查詢

sql 經典四表查詢

要求 信息 arch 1-1 size having select lec BE

題 目 :

student(sid, sname, sage, ssex) -- 學生信息表(學生編號 自增,學生姓名, 學生出生年月, 性別);

teacher(tid, tname) -- 教師信息表(教師編號 自增, 教師姓名)

course(cid, cname, tid) -- 課程表(課程編號 自增, 課程名稱, 教師編號 外鍵)

sc(sid, cid, score) -- 分數表(學生編號,課程編號,分數)

create database work;

use work;
set names work;

# 創建表
create table student(
sid int primary key auto_increment,
sname varchar(20),
sage date,
ssex varchar(10)
)

create table teacher(
tid int primary key auto_increment,
tname varchar(20)
);

create table course(
cid int primary key auto_increment,
cname varchar(20),
tid int ,
foreign key(tid) references teacher(tid)
)

create table sc(
sid int,
cid int,
score int
)

# 插入數據

# 先給student表插入數據
insert into student values
(1,‘趙雷‘,‘1990-01-01‘,‘男‘),
(2,‘錢電‘,‘1990-12-21‘,‘男‘),
(3,‘孫風‘,‘1990-05-20‘,‘男‘),
(4,‘李雲‘,‘1990-08-06‘,‘男‘),
(5,‘周梅‘,‘1991-12-01‘,‘女‘),
(6,‘吳蘭‘,‘1992-03-01‘,‘女‘),
(7,‘鄭竹‘,‘1989-07-01‘,‘女‘),
(8,‘王菊‘,‘1990-01-20‘,‘女‘);
# 教師表
insert into teacher values
(1,‘張三‘),
(2,‘李四‘),
(3,‘王五‘);
# 課程表
insert into course values
(1,‘語文‘,2),
(2,‘語文‘,1),
(3,‘語文‘,3);
# 分數表
insert into sc values
(1,1,90),
(1,2,80),
(1,3,90),
(2,1,70),
(2,2,60),
(2,3,80),
(3,1,80),
(3,2,80),
(3,3,80),
(4,1,50),
(4,2,30),
(4,3,20),
(5,1,76),
(5,2,87),
(6,1,31),
(6,3,34),
(7,2,89),
(7,3,98);

問 題:

-- 1、查詢”01”課程比”02”課程成績高的學生的信息及課程分數
select
st.sid,
st.sname,
st.sage,
st.ssex,
sc1.score as 課程1分數,
sc2.score as 課程1分數
from sc sc1,
sc sc2,
student st
where sc1.sid = sc2.sid
and sc1.sid = st.sid
and sc1.cid = 1
and sc2.cid = 2
and sc1.score > sc2.score

-- 2、查詢學生表前5名信息;
select * from student st limit 5;

-- 3.查詢平均成績大於等於60分的同學的學生編號和學生姓名和平均成績
select
s.sname,
sc.sid,
avg(sc.score)
from student s,
sc
where s.sid = sc.sid
group by sc.sid
having avg(sc.score) >= 60

-- 4、查詢名字中含有”風”字的學生信息
select * from student st where st.sname like ‘%風‘;

-- 5、查詢課程名稱為”數學”,且分數低於60的學生姓名和分數
select
st.sname,
sc.score
from sc,
student st
where st.sid = sc.sid
and sc.score < 60
and sc.cid = (select
co.cid
from course co
where co.cname = ‘數學‘)

-- 6、查詢所有學生的課程及分數情況;
select
st.sname,
co.cname,
sc.score
from student st,
sc,
course co
where st.sid = sc.sid
and co.cid = sc.cid;

-- 7、查詢沒學過”張三”老師授課的同學的信息
select *
from student st
where st.sid not in(select st1.sid
from student st1,
sc,
course co,
teacher te
where st1.sid = sc.sid
and co.cid = sc.cid
and co.tid = te.tid
and te.tname = ‘張三‘) ;

-- 8、查詢學過”張三”老師授課的同學的信息
select
st1.*
from student st1,
sc,
course co,
teacher te
where st1.sid = sc.sid
and co.cid = sc.cid
and co.tid = te.tid
and te.tname = ‘張三‘


-- 9、查詢學過編號為”01”並且也學過編號為”02”的課程的同學的信息
select
st.sid,
st.sname,
st.sage,
st.ssex
from sc sc1,
sc sc2,
student st
where sc1.sid = sc2.sid
and sc1.sid = st.sid
and sc1.cid = 1
and sc2.cid = 2


-- 10、查詢學過編號為”01”但是沒有學過編號為”02”的課程的同學的信息
select
st.sid,
st.sname,
st.sage,
st.ssex
from student st,
sc
where st.sid = sc.sid
and sc.cid = 1
and st.sid not in(select distinct
sc1.sid
from sc sc1,
sc sc2
where sc1.cid = 2)

-- 11、查詢沒有學全所有課程的同學的信息

select * from student st
where st.sid not in
(select
distinct st.sid
from sc sc1,
sc sc2,
sc sc3,
student st
where sc1.sid = sc2.sid
and sc1.sid = sc3.sid
and sc1.sid = st.sid
and sc1.cid = 1
and sc2.cid = 2
and sc3.cid = 3)

-- 12、查詢至少有一門課與學號為”01”的同學所學相同的同學的信息
select distinct
st.*
from student st,
sc
where st.sid = sc.sid
and sc.cid in(select
sc.cid
from sc
where sc.sid = 1)
and st.sid != 1

-- 13、查詢和”01”號的同學學習的課程完全相同的其他同學的信息
select st.sid, st.sname, st.sage, st.ssex
from student st,
sc
where st.sid = sc.sid
and st.sid != 1
and sc.cid in(select
sc.cid
from sc
where sc.sid = 1)
group by st.sid
having count( * ) = (select
count(*)
from sc
where sc.sid = 1)

-- 14、查詢沒學過”張三”老師講授的任一門課程的學生姓名
#方法1:
select *
from student st
where st.sid not in(select
st.sid
from student st,
sc
where sc.sid = st.sid
and sc.cid = (select
co.cid
from course co
where co.tid = (select
te.tid
from teacher te
where te.tname = ‘張三‘)))
#方法2:
select
s.*
from student s
where s.sid not in(select
sc1.sid
from sc sc1,
course c,
teacher t
where sc1.cid = c.cid
and c.tid = t.tid
and t.tname = ‘張三‘);

-- 15、查詢出只有兩門課程的全部學生的學號和姓名
select
st.sid,
st.sname,
st.sage,
st.ssex
from student st,
sc
where st.sid = sc.sid
group by sc.sid
having count( * ) = 2

-- 16、查詢1990年出生的學生名單(註:Student表中Sage列的類型是datetime)

select * from student st where st.sage >= ‘1990-01-01‘ and st.sage <= ‘1990-12-31‘;
select s.* from student s where s.sage like ‘1990-%‘;
select * from student st where st.sage between ‘1990-01-01‘ and ‘1990-12-31‘;

-- 17、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
select sc.cid, avg(sc.score) from sc group by sc.cid order by avg(sc.score) desc, sc.cid

-- 18、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
#方法1:
select
s.sname,
c.cname,
score
from student s,
sc,
course c
where s.sid = sc.sid
and sc.cid = c.cid
and score > 70;
#方法2:
select st.sname, co.cname, sc.score
from student st,
course co,
sc
where st.sid = sc.sid
and sc.cid = co.cid
and st.sid in(select
sc.sid
from sc
group by sc.sid
having min(sc.score) >= 70)

-- 19、查詢平均成績大於等於85的所有學生的學號、姓名和平均成績
select
st.sid,
st.sname,
avg(sc.score) avgscore
from student st,
sc
where st.sid = sc.sid
group by sc.sid
having avg(sc.score) >= 85

-- 20、查詢不及格的課程
select
st.sname,
co.cname,
sc.score
from student st,
course co,
sc
where st.sid = sc.sid
and sc.cid = co.tid
and sc.score < 60

-- 21、查詢課程編號為01且課程成績在80分以上的學生的學號和姓名;
select
st.sid,
st.sname
from student st,
sc
where st.sid = sc.sid
and sc.cid = 1
and sc.score < 60

-- 22、求每門課程的學生人數
select sc.cid, count(*) from sc group by sc.cid
select cid,count(sid) from sc group by sc.cid;

-- 23、統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
select
sc.cid,
count(*) sumstudent
from sc
group by sc.cid
having count( * ) > 5
order by sumstudent desc, sc.cid

-- 24、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
select
sc1.sid, sc1.cid,sc2.sid, sc2.cid,sc1.score
from sc sc1,
sc sc2
where sc1.sid != sc2.sid
and sc1.cid != sc2.cid
and sc1.score = sc2.score

-- 25、檢索至少選修兩門課程的學生學號
select
sc.sid
from sc
group by sc.sid
having count( * ) >= 2

-- 26、查詢選修了全部課程的學生信息
select
st.*
from sc,
student st
where st.sid = sc.sid
group by sid
having count(sc.cid) = 3;

-- 27、查詢各學生的年齡
select s.sname,(to_days(‘2018-06-23‘)-to_days(s.sage))/365 as age from student s;


-- 28、查詢本月過生日的學生
select s.sname from student s where s.sage like ‘_____07%‘;

-- 39、查詢學全所有課程的同學的信息
select
s.*
from student s,
sc sc1,
sc sc2,
sc sc3
where sc1.cid = 1
and sc2.cid = 2
and sc3.cid = 3
and sc1.sid = sc2.sid
and sc1.sid = sc3.cid
and s.sid = sc1.sid
group by s.sid;

-- 30、查詢課程2 第2名到第5名的分數,降序排列
select * from course co, sc where co.cid = sc.cid and co.cid=2 order by sc.score desc limit 1,4


sql 經典四表查詢