數據庫練習


參考網站:https://blog.csdn.net/flycat296/article/details/63681089#commentBox

練習的數據表結構

–1.學生表
Student(S,Sname,Sage,Ssex)
–S 學生編號,Sname 學生姓名,Sage 出生年月,Ssex 學生性別

–2.課程表
Course(C,Cname,T#)
–C --課程編號,Cname 課程名稱,T# 教師編號

–3.教師表
Teacher(T,Tname)
–T 教師編號,Tname 教師姓名

–4.成績表
SC(S,C,score)
–S 學生編號,C 課程編號,score 分數

數據表中的數據



create table Student(S varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10));
insert into Student values('01' , N'趙雷' , '1990-01-01' , N'男');
insert into Student values('02' , N'錢電' , '1990-12-21' , N'男');
insert into Student values('03' , N'孫風' , '1990-05-20' , N'男');
insert into Student values('04' , N'李雲' , '1990-08-06' , N'男');
insert into Student values('05' , N'周梅' , '1991-12-01' , N'女');
insert into Student values('06' , N'吳蘭' , '1992-03-01' , N'女');
insert into Student values('07' , N'鄭竹' , '1989-07-01' , N'女');
insert into Student values('08' , N'王菊' , '1990-01-20' , N'女');


create table Course(C varchar(10),Cname nvarchar(10),T varchar(10));
insert into Course values('01' , N'語文' , '02');
insert into Course values('02' , N'數學' , '01');
insert into Course values('03' , N'英語' , '03');

#教師表 Teacher
create table Teacher(T varchar(10),Tname nvarchar(10));
insert into Teacher values('01' , N'張三');
insert into Teacher values('02' , N'李四');
insert into Teacher values('03' , N'王五');

#成績表 SC
create table SC(S varchar(10),C varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);

練習題以及sql語句

1.查詢" 01 “課程比” 02 "課程成績高的學生的信息及課程分數

select A.*,B.C,B.score from (select * from SC where C='01')A 
left join(select * from SC where C='02')B on A.S=B.S 
where A.score>B.score

2.查詢同時存在" 01 “課程和” 02 "課程的情況
(兩個條件查詢,可採用內連接)

select * from (select * from SC where C='01')A 
inner join(select * from SC where C='02')B on A.S=B.S 
#select * from SC where C='01' and C='02'

3、查詢不存在" 01 “課程但存在” 02 "課程的情況

select s from sc where sc.c='02' and s not in (select s from sc where c='01')

5、查詢所有人的平均成績

select S,AVG(score)dc from SC group by S

6、查詢平均成績大於等於 60 分的同學的學生編號和學生姓名和平均成績

select A.s,B.Sname,A.dc from(select s,avg(score)dc from sc group by s)A 
inner join student B on A.s=B.s 

7、其它

#查詢" 01 "課程比" 02 "課程成績高的學生的信息及課程分數
select A.*,B.C,B.score from (select * from SC where C='01')A 
left join(select * from SC where C='02')B on A.S=B.S 
where A.score>B.score;

#查詢同時存在" 01 "課程和" 02 "課程的情況(兩個條件查詢,可採用內連接)
select * from (select * from SC where C='01')A 
inner join(select * from SC where C='02')B on A.S=B.S ;
#select * from SC where C='01' and C='02';

#查詢不存在" 01 "課程但存在" 02 "課程的情況
select s from sc where sc.c='02' and s not in (select s from sc where c='01');

#查詢所有人的平均成績
select S,AVG(score)dc from SC group by S;

#查詢平均成績大於等於 60 分的同學的學生編號和學生姓名和平均成績
select A.s,B.Sname,A.dc from(select s,avg(score)dc from sc group by s)A 
inner join student B on A.s=B.s;

#查詢平均成績大於等於 60 分的同學的學生編號和學生姓名和平均成績
select A.s,B.Sname,A.dc from(select s,avg(score)dc from sc group by s)A 
inner join student B on A.s=B.s ;

#查詢在 SC 表存在成績的學生信息(distinct的應用)
select B.S,B.Sname,B.Sage,B.Ssex from(select s from sc group by s)A 
inner join student B on A.s=B.s;
select * from Student where S in (select distinct S from SC);

#查詢所有同學的學生編號、學生姓名、選課總數、所有課程的總成績(沒成績的顯示爲null)
select B.s,B.Sname,A.y,A.x from (select s,sum(score)x,count(C)y from sc group by s)A
right join student B on A.s=B.s;

#查有成績的學生信息
select * from student where s in (select distinct s from sc);

#查詢「李」姓老師的數量 
select count(*) from teacher where Tname like '李%';

#查詢學過「張三」老師授課的同學的信息
select * from student 
where s in (select s from sc 
where C=(select C from course 
where T=(select T from teacher where Tname like '李%')));

#查詢沒有學全所有課程的同學的信息
select * from student where s in (select s from sc group by s having count(c)>=3);

#查詢至少有一門課與學號爲" 01 "的同學所學相同的同學的信息
select * from student where s in (select s from sc where c in (select c from sc where s='01'));

#查詢和" 01 "號的同學學習的課程完全相同的其他同學的信息

#查詢沒學過「張三」老師講授的任一門課程的學生姓名
select * from student where s not in (select s from sc where c=(select C from course where c=(select T from teacher where Tname='張三')));

#查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select A.s,A.sname,B.x from student A right join (select s,avg(score)x from sc where s in (select s from sc where score<60 group by s having count(c)>=2) group by s)B on A.s=B.s;

#檢索" 01 "課程分數小於 60 ,按分數降序排列的學生信息
select s,score from sc where C='01' and score<60 order by score desc;

#按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
select S,case when C='01' then score else 0 end'01',
case C when '02' then score else 0 end'02',
case C when '03' then score else 0 end'03',AVG(score)平均分 from SC
group by S order by 平均分 desc;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章