原文:http://www.cnblogs.com/dlexia/p/4449841.html
http://blog.csdn.net/a379850992/article/details/55655495
紙上得來終覺淺,絕知此事要躬行。
理論知識要靠實踐來加強。
1.創建四個表學生表,課程表和成績表。
Teacher教師表(教工編號Tno,教工姓名Tname,教工性別Tsex,教工出生年月birthday,職稱Prof,所在學院Depart )
Student 學生表 (學號sno,姓名sname,性別ssex,學生出生年月Sbirthday ,班級class)
Course 課程表 (課程編號Cno,課程名稱Cname)
Score 成績表 (學號Sno,課程編號Cno,成績degree)
create table Teacher--教師表
(
Tno varchar(50) primary key not null,--教工編號(主碼)
Tname varchar(50)not null,--教工姓名
Tsex varchar(50)not null,--教工性別
Tbirthday datetime,--教工出生年月
Prof varchar(50),--職稱
Depart Varchar(10)not null--教工所在學院
)
insert into Teacher values(804,'李誠','男','1958-12-02','副教授','計算機系')
insert into Teacher values(856,'張旭','男','1969-03-12','講師','電子工程系')
insert into Teacher values(825,'王萍','女','1972-05-05','助教','計算機系')
insert into Teacher values(831,'劉冰','女','1977-08-14','助教','電子工程系')
create table Course--課程表
(
Cno char(5) primary key not null,--課程號主鍵
Cname Varchar(10)not null,--課程名稱
Tno Varchar(50) not null--教工編號(外碼)
foreign key(Tno)
references Teacher (Tno),
)
insert into Course values('3-105','計算機導論','825')
insert into Course values('3-245','操作系統','804')
insert into Course values('6-166','數字電路','856')
insert into Course values('9-888','高等數學','831')
select *from Course
create table Student--學生表
(
Sno int primary key not null,--學號主鍵
Sname varchar(50) not null,--學生姓名
Ssex varchar(50) not null,--學生性別
Sbirthday datetime,--出生年月
Class int--班級
)
truncate table Student--清空表格
insert into Student values(108,'曾華','男','1977-09-01',95033)
insert into Student values(105,'匡明','男','1975-10-02',95031)
insert into Student values(107,'王麗','女','1976-01-23',95033)
insert into Student values(101,'李軍','男','1976-02-20',95033)
insert into Student values(109,'王芳','女','1975-02-10',95031)
insert into Student values(103,'陸君','男','1974-06-03',95031)
select *from Student
create table Score--成績表
(
Sno int not null, --學號(外碼)
Cno Char(5) not null,--課程號(外碼)
Degree Decimal(4,1),--成績
foreign key(Sno)
references Student(Sno),--學號(外碼)
foreign key(Cno)
references Course(Cno),--課程號(外碼)
primary key(Sno,Cno)
)
insert into Score values(103,'3-245',86)
insert into Score values(105,'3-245',75)
insert into Score values(109,'3-245',68)
insert into Score values(103,'3-105',92)
insert into Score values(105,'3-105',88)
insert into Score values(109,'3-105',76)
insert into Score values(101,'3-105',64)
insert into Score values(107,'3-105',91)
insert into Score values(108,'3-105',78)
insert into Score values(101,'6-166',85)
insert into Score values(107,'6-166',79)
insert into Score values(108,'6-166',81)
1.查詢Student表中的所有記錄的Sname、Ssex和Class列。
SELECT Sname,Ssex,Class FROM Student;
2.查詢教師所有的單位即不重複的Depart列。
select distinct Depart from Teacher;
3.查詢Score表中成績在60到80之間的所有記錄。
select *
from Score
where Degree between 60 and 80;
4..查詢Score表的所有記錄,以Cno升序、Degree降序。
select *from Score order by Cno,Degree desc;
5.查詢“95031”班的學生人數。
select COUNT(*) from Student where Class=95031;
6.查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)
select Sno,Cno from Score where Degree=(select MAX(Degree) from Score);
select top 1 *from Score order by Degree desc--從大到校排序再取第一行
7.查詢每門課的平均成績–分組和聚合結合的時候,先分組,然後對每一組分別進行聚合
Select AVG(Degree) from Score group by Cno;--先按Cno分組,在對Cno聚合
8.查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
select x.Cno ,avg(x.Degree) from Score x where Cno like '3%' and 5<(select count (*) from Score y where x.Cno = y.Cno) group by x.Cno
select Cno,AVG(Degree) from Score where Cno like '3%' group by Cno having COUNT(*)>=5--篩選分組完了再對每一組進行AVG聚合,分完組之後相當於只剩下分完組的列
9.查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。
select *
from Score
where Cno='3-105'
and degree >
(select degree from score where Sno='109' and Cno='3-105')
10.查詢選修某課程的同學人數多於5人的教師姓名。
select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(Cno)>=5))
11.查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof。
select Tname,Prof
from Teacher
where depart="計算機系" and prof not in (
select distinct Prof
from Teacher
where depart = "計算機系");
12.查詢選修編號爲“3-105“課程且成績至少高於任意選修編號爲“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。
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;
13.查詢所有教師和同學的name、sex和birthday。(union縱向連接)
select Tname,Tsex,Tbirthday
from Teacher
union
select sname,ssex,sbirthday
from student ;
14.查詢成績比該課程平均成績低的同學的成績表。
select *
from Score a
where degree < (
select avg(degree)
from Score b
group by cno
having a.cno=b.cno
);
15.查詢至少有2名男生的班號。
select Class from Student where Ssex='男' group by Class having COUNT(*)>=2