经典的数据库查询例题

原文: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 

猜你喜欢

转载自blog.csdn.net/G090909/article/details/79477473