原文地址:数据库笔试题(经典select语句的用法)
作者:lily
问题描述:
为管理岗位业务培训信息,建立3个表:
S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
C (C#,CN ) C#,CN 分别代表课程编号、课程名称
SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩
--1. 使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名
SELECT SN,SD FROM S WHERE [S#] IN(SELECT [S#] FROM C,SC WHERE C.[C#]=SC.[C#]AND CN='税收基础')
--2. 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位
SELECT S.SN,S.SD FROM S,SC WHERE S.[S#]=SC.[S#] AND SC.[C#]='C2'
--3. 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位
SELECT SN,SD FROM S WHERE [S#] NOT IN(SELECT [S#] FROM SC WHERE [C#]='C5')
--4. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位
SELECT SN,SD FROM S WHERE [S#] IN(SELECT [S#] FROM SC RIGHT JOIN C ON SC.[C#]=C.[C#] GROUP BY [S#] HAVING COUNT(*)=COUNT([S#]))
--5. 查询选修了课程的学员人数
SELECT 学员人数=COUNT(DISTINCT [S#]) FROM SC
--6. 查询选修课程超过5门的学员姓名和所属单位
SELECT SN,SD FROM S WHERE [S#] IN(SELECT [S#] FROM SC GROUP BY [S#] HAVING COUNT(DISTINCT [C#])>5)
--7.学生所有的成绩表单
SELECT S.S# as 学号,S.SN as 姓名,S.SD AS 职业 , SS.[语文],SS.[英语] ,SS.[数学],SS.[政治],SS.[物理],SS.[化学] FROM S ,
(SELECT SC1.S#,[语文]=SC1.G ,[英语]=SC2.G ,[数学]=SC3.G,[政治]=SC4.G,[物理]=SC5.G,[化学]=SC6.G
from SC SC1,SC SC2,SC SC3,SC SC4,SC SC5,SC SC6,C C1,C C2 ,C C3,C C4,C C5,C C6
WHERE C1.C#=SC1.C# AND C2.C#=SC2.C# AND C3.C#=SC3.C# AND C4.C#=SC4.C# AND C5.C#=SC5.C# AND C6.C#=SC6.C#
AND SC1.S#=SC2.S# and SC2.S#=SC3.S# and SC3.S#=SC4.S# and SC4.S#=SC5.S# and SC5.S#=SC6.S# AND C1.CN='语文' AND C2.CN='英语' AND C3.CN='数学' AND C4.CN='政治' AND C5.CN='物理' AND C6.CN='化学' )SS WHERE S.S#=SS.S# -- and S.S#='1'
--8.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
SELECT S.S#,S.SN,AVG_SCGRADE=AVG(SC.G) FROM S,SC,
(SELECT S# FROM SC WHERE G<60 GROUP BY S# HAVING COUNT(DISTINCT C#)>=2 )A
WHERE S.S#=A.S# AND SC.S#=A.S# GROUP BY S.S#,S.SN
为管理岗位业务培训信息,建立3个表:
S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
C (C#,CN ) C#,CN 分别代表课程编号、课程名称
SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩
--1. 使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名
SELECT SN,SD FROM S WHERE [S#] IN(SELECT [S#] FROM C,SC WHERE C.[C#]=SC.[C#]AND CN='税收基础')
--2. 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位
SELECT S.SN,S.SD FROM S,SC WHERE S.[S#]=SC.[S#] AND SC.[C#]='C2'
--3. 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位
SELECT SN,SD FROM S WHERE [S#] NOT IN(SELECT [S#] FROM SC WHERE [C#]='C5')
--4. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位
SELECT SN,SD FROM S WHERE [S#] IN(SELECT [S#] FROM SC RIGHT JOIN C ON SC.[C#]=C.[C#] GROUP BY [S#] HAVING COUNT(*)=COUNT([S#]))
--5. 查询选修了课程的学员人数
SELECT 学员人数=COUNT(DISTINCT [S#]) FROM SC
--6. 查询选修课程超过5门的学员姓名和所属单位
SELECT SN,SD FROM S WHERE [S#] IN(SELECT [S#] FROM SC GROUP BY [S#] HAVING COUNT(DISTINCT [C#])>5)
--7.学生所有的成绩表单
SELECT S.S# as 学号,S.SN as 姓名,S.SD AS 职业 , SS.[语文],SS.[英语] ,SS.[数学],SS.[政治],SS.[物理],SS.[化学] FROM S ,
(SELECT SC1.S#,[语文]=SC1.G ,[英语]=SC2.G ,[数学]=SC3.G,[政治]=SC4.G,[物理]=SC5.G,[化学]=SC6.G
from SC SC1,SC SC2,SC SC3,SC SC4,SC SC5,SC SC6,C C1,C C2 ,C C3,C C4,C C5,C C6
WHERE C1.C#=SC1.C# AND C2.C#=SC2.C# AND C3.C#=SC3.C# AND C4.C#=SC4.C# AND C5.C#=SC5.C# AND C6.C#=SC6.C#
AND SC1.S#=SC2.S# and SC2.S#=SC3.S# and SC3.S#=SC4.S# and SC4.S#=SC5.S# and SC5.S#=SC6.S# AND C1.CN='语文' AND C2.CN='英语' AND C3.CN='数学' AND C4.CN='政治' AND C5.CN='物理' AND C6.CN='化学' )SS WHERE S.S#=SS.S# -- and S.S#='1'
--8.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
SELECT S.S#,S.SN,AVG_SCGRADE=AVG(SC.G) FROM S,SC,
(SELECT S# FROM SC WHERE G<60 GROUP BY S# HAVING COUNT(DISTINCT C#)>=2 )A
WHERE S.S#=A.S# AND SC.S#=A.S# GROUP BY S.S#,S.SN