自学内容网 自学内容网

高级查询(连接查询,不相关子查询,相关子查询)

连接查询

  1. 查询姓李的老师所讲授的课程。
  2. 查询成绩在80分以上的学生编号,姓名,课程名称,成绩。
  3. 查询计算机系姓张的和姓李的老师教授的课程名称,显示教师名称和课程名称。
  4. 查询男同学所选修的课的平均成绩,显示性别和平均成绩。
  5. 查询每门课称的选课人数,最高分,平均分数,最低分。显示课程名称和各个统计字段,按照选课人数多少降序排序。
  6. 查询与张爽老师在同一个系工作的老师。
  7. 查询同时选修了2门或2门以上课程的学生学号和姓名。
  8. 查询所有课程的选修情况(包括没有选修的课程)。
--1.查询姓李的老师所讲授的课程。
select name,cname from C,T
where T.name like '李%' and C.Tid=T.Tid;
--2.查询成绩在80分以上的学生编号,姓名,课程名称,成绩。
select S.Sid,S.sname,C.cname,SC.score from S,SC,C
where SC.score>80 and S.Sid=SC.Sid and C.Cid=SC.Cid;
--3.查询计算机系姓张的和姓李的老师教授的课程名称,显示教师名称和课程名称。
select T.name,C.cname from T,C
where T.department='计算机系' and T.Tid=C.Tid and T.name like '[张,李]%';
--4.查询男同学所选修的课的平均成绩,显示性别和平均成绩。
select sex,avg(SC.score) from S,SC
where S.sex='男' and S.Sid=SC.Sid
group by S.sex
--5.查询每门课称的选课人数,最高分,平均分数,最低分。显示课程名称和各个统计字段,按照选课人数多少降序排序。
select cname,count(*) '选课人数',max(score) '最高分',avg(score) '平均分',min(score) '最低分'
from SC,C
where SC.Cid=C.Cid
group by SC.Cid,cname
order by count(*) desc
--6.查询与张爽老师在同一个系工作的老师。
select y.* from T x,T y
where x.department=y.department and x.name='张爽' and y.name<>'张爽'
--7.查询同时选修了2门或2门以上课程的学生学号和姓名。
select distinct S.Sid,sname from S,SC x,SC y
where S.Sid=x.Sid and x.Sid=y.Sid and x.Cid>y.Cid
--8.查询所有课程的选修情况(包括没有选修的课程)。
select * from C left join SC on C.Cid=SC.Cid

不相关子查询

  1. 查询与张爽老师在同一个系工作的老师。
  2. 查询选修了数据结构课程的学生的学号和姓名。
  3. 查询选修了Java语言课程的学生姓名。
  4. 查询江西学生的平均成绩。
  5. 查询选修了103号课程,且成绩高于该课程平均分数的学生信息。
  6. 查询没有选修Java语言的学生姓名。
  7. 查询比江西省所有学生年龄都小的其他省的学生的学号和姓名。
--1.查询与张爽老师在同一个系工作的老师。
select * from T where name!='张爽' and department=(
select department from T where name='张爽')
--2.查询选修了数据结构课程的学生的学号和姓名。
select Sid,sname from S where Sid in(
select Sid from SC where Cid=(
select Cid from C where cname='数据结构'))
--3.查询选修了Java语言课程的学生姓名。
select sname from S where Sid in(
select Sid from SC where Cid=(
select Cid from C where cname='Java语言'))
--4.查询江西学生的平均成绩。
select AVG(score) from SC where Sid in(
select Sid from S where nativeplace like '江西%')
--5.查询选修了103号课程,且成绩高于该课程平均分数的学生信息。
select * from S where Sid in(
select Sid from SC where Cid='103' and score>(
select AVG(score) from SC where Cid='103'))
--6.查询没有选修Java语言的学生姓名。
select sname from S where Sid not in(
select Sid from SC where Cid=(
select Cid from C where cname='Java语言'))
--7.查询比江西省所有学生年龄都小的其他省的学生的学号和姓名。
select Sid,sname from S where nativeplace not like '江西%' and age<(
select min(age) from S where nativeplace like '江西%')

相关子查询

  1. 查询选修了107号课程的学生的学号和姓名。
  2. 查询没有选修Java语言的学生姓名。
  3. 查询每个学生超过他选修课程平均成绩的学号和课程号。
  4. 查询与张爽老师在同一个系工作的老师。
  5. 查询选修了所有课程的学生编号和姓名
  6. 查询张佳同学没有选的课程编号
  7. 查询至少有两位同学都选的课程号
--1.查询选修了107号课程的学生的学号和姓名。
select Sid,sname from S where exists(
select * from SC where Cid='107' and Sid=S.Sid)
--2.查询没有选修Java语言的学生姓名。
select sname from S where not exists(
select * from C where cname='Java语言' and exists(
select * from SC where Sid=S.Sid and Cid=C.Cid))
--3.查询每个学生超过他选修课程平均成绩的学号和课程号。
select Sid,Cid from SC where score>(
select AVG(score) from SC y where y.Sid=SC.Sid)
--4.查询与张爽老师在同一个系工作的老师。
select * from T where name!='张爽' and exists(
select * from T y where y.name='张爽' and y.department=T.department)
--5.查询选修了所有课程的学生编号和姓名
select Sid,sname from Swhere not exists(
select Cid from C where not exists(
select Cid from SC where SC.Cid=C.Cid and SC.Sid=S.Sid))
--6.查询张佳同学没有选的课程编号
select Cid from C where not exists(
select Sid from S where sname='张佳' and exists(
select * from SC where Sid=S.Sid and Cid=C.Cid))
--7.查询至少有两位同学都选的课程号
select distinct Cid from SC where exists(
select * from SC y where y.Cid=SC.Cid and y.Sid<>SC.Sid)

数据库

create database jxgl
go
use jxgl
go
create table T(
Tid nchar(5) primary key,
name nchar(10) not null,
sex nchar(2) default '男',
workingtimedatetime,
plandscape nchar(20),
degree nchar(10),
title nvarchar(6),
department nvarchar(20),
telnchar(11)
)

create table S(
Sidnchar(10) primary key,
sname nchar(10),
sexnchar(2) default '男',
birthday Date,
ageas datediff(yy,birthday,getdate()),
entrydate date,
plandscape nchar(10) default '团员',
nativeplacenvarchar(20),
class as substring(Sid,7,2)
)

create table C(
Cidnchar(3) primary key,
cname nvarchar(20) Not null,
category nchar(4) Not null check(category in('考试','考查')) default '考试',
creditint,
Tidnchar(5) foreign key references T(Tid) on update cascade
)

create table SC(
Sidnchar(10) foreign key references S(Sid) on update cascade on delete cascade,
Cidnchar(3),
score int check(score between 0 and 100),
primary key(Sid,Cid),
foreign key(Cid) references C(Cid) on update cascade on delete cascade
)

INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95011', N'赵西明       ', N'男 ', CAST(0x0000768300000000 AS DateTime), N'群众                  ', N'硕士        ', N'副教授', N'软件', N'13733152369')
INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95012', N'李小平       ', N'男 ', CAST(0x0000775E00000000 AS DateTime), N'党员                  ', N'硕士        ', N'教授', N'计算机系', N'13733152370')
INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95013', N'张爽        ', N'男 ', CAST(0x00007BC700000000 AS DateTime), N'群众                  ', N'本科        ', N'副教授', N'计算机系', N'13733152371')
INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95014', N'李丽宁       ', N'女 ', CAST(0x00007BC700000000 AS DateTime), N'党员                  ', N'硕士        ', N'副教授', N'计算机系', N'13733152372')
INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95015', N'张金明       ', N'男 ', CAST(0x0000812C00000000 AS DateTime), N'群众                  ', N'硕士        ', N'讲师', N'计算机系', N'13733152373')
INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95016', N'李燕        ', N'女 ', CAST(0x000082C800000000 AS DateTime), N'党员                  ', N'硕士        ', N'讲师', N'计算机系', N'13733152374')
INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95017', N'宛平        ', N'女 ', CAST(0x0000840B00000000 AS DateTime), N'群众                  ', N'博士        ', N'副教授', N'软件', N'13733152375')
INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95018', N'陈江川       ', N'男 ', CAST(0x0000883900000000 AS DateTime), N'群众                  ', N'博士        ', N'讲师', N'软件', N'13733152376')
INSERT [dbo].[T] ([Tid], [name], [sex], [workingtime], [plandscape], [degree], [title], [department], [tel]) VALUES (N'95019', N'郭新        ', N'男 ', CAST(0x00008CAA00000000 AS DateTime), N'党员                  ', N'博士        ', N'讲师', N'软件', N'13733152377')
INSERT [dbo].[S] ([Sid], [sname], [sex], [birthday], [entrydate], [plandscape], [nativeplace]) VALUES (N'2017071101', N'张佳        ', N'女 ', CAST(0x3F1D0B00 AS Date), CAST(0x3C3D0B00 AS Date), N'团员        ', N'江西南昌')
INSERT [dbo].[S] ([Sid], [sname], [sex], [birthday], [entrydate], [plandscape], [nativeplace]) VALUES (N'2017071102', N'好生        ', N'男 ', CAST(0xCD1E0B00 AS Date), CAST(0x3C3D0B00 AS Date), N'团员        ', N'广州顺德')
INSERT [dbo].[S] ([Sid], [sname], [sex], [birthday], [entrydate], [plandscape], [nativeplace]) VALUES (N'2017071203', N'徐克        ', N'男 ', CAST(0xBF210B00 AS Date), CAST(0x3C3D0B00 AS Date), N'团员        ', N'江西南昌')
INSERT [dbo].[S] ([Sid], [sname], [sex], [birthday], [entrydate], [plandscape], [nativeplace]) VALUES (N'2017071204', N'叶飞        ', N'女 ', CAST(0xD1210B00 AS Date), CAST(0x3C3D0B00 AS Date), N'党员        ', N'上海')
INSERT [dbo].[S] ([Sid], [sname], [sex], [birthday], [entrydate], [plandscape], [nativeplace]) VALUES (N'2017071205', N'任伟        ', N'男 ', CAST(0x7B220B00 AS Date), CAST(0x3C3D0B00 AS Date), N'团员        ', N'北京顺义')
INSERT [dbo].[S] ([Sid], [sname], [sex], [birthday], [entrydate], [plandscape], [nativeplace]) VALUES (N'2017071206', N'江贺        ', N'男 ', CAST(0x02200B00 AS Date), CAST(0x3D3D0B00 AS Date), N'党员        ', N'福建厦门')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'101', N'计算机基础', N'考试  ', 2, N'95011')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'102', N'C语言', N'考试  ', 3, N'95012')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'103', N'计算机组成原理', N'考试  ', 3, N'95012')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'104', N'汇编语言', N'考试  ', 3, N'95014')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'105', N'Java语言', N'考查  ', 2, N'95015')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'106', N'操作系统', N'考试  ', 3, N'95016')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'107', N'数据结构', N'考试  ', 3, N'95017')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'108', N'编译原理', N'考试  ', 3, N'95017')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'109', N'网络基础', N'考试  ', 3, N'95017')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'110', N'数据库原理', N'考试  ', 3, N'95017')
INSERT [dbo].[C] ([Cid], [cname], [category], [credit], [Tid]) VALUES (N'120', N'SQLServer', N'考查  ', 2, N'95018')
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071101', N'101', 90)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071101', N'102', 70)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071101', N'103', 48)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071101', N'105', 80)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071102', N'102', 90)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071102', N'104', 77)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071102', N'106', 68)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071102', N'107', 85)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071102', N'109', 77)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071102', N'110', 65)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071102', N'120', 48)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071203', N'102', 65)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071203', N'104', 82)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071203', N'105', 79)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071203', N'107', 55)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071203', N'110', 77)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071203', N'120', 67)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071204', N'101', 86)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071204', N'102', 86)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071204', N'104', 77)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071204', N'105', 84)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071204', N'106', 95)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071204', N'108', 91)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071204', N'110', 82)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071205', N'101', 63)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071205', N'102', 84)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071205', N'103', 77)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071206', N'107', 58)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071206', N'108', 74)
INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N'2017071206', N'109', 74)


原文地址:https://blog.csdn.net/qq_74073844/article/details/142964316

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!