常见SQL面试题

SQL面试题(1)
表结构 :

S(S#,SN,SD,SA)S#,SN,SD,SA分别代表学号,学员姓名,所属单位,学员年龄
C(C#,CN)C#,CN分别代表课程编号,课程名称
SC(S#,C#,G) S#,C#,G分别代表学号,所选的课程编号,学习成绩
(1)使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名?

select s# ,sn from s where S# in(select S# from c,sc where c.c#=sc.c# and cn=’税收基础’)

(2) 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位?

select sn,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)查询选修了课程的学员人数

select 学员人数=count(distinct s#) from sc

(5) 查询选修课程超过5门的学员学号和所属单位?

select sn,sd from s where s# in(select s# from sc group by s# having count(distinct c#)>5)
SQL面试题(2)

(1)查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下:

select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by A ) T) order by A

(2)查询表A中存在ID重复三次以上的记录,完整的查询语句如下:

select * from(select count(ID) as count from table group by ID)T where T.count>3

猜你喜欢

转载自blog.csdn.net/lwl2014100338/article/details/80716029