12道必须掌握的Sql面试题

建库脚本

USE [master]
GO
CREATE DATABASE [Example]
GO
USE [Example]
GO
/****** Object:  Table [dbo].[Course]    Script Date: 2016/12/6 0:16:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Course](
    [C#] [int] IDENTITY(1,1) NOT NULL,
    [Cname] [nvarchar](50) NOT NULL,
    [T#] [int] NOT NULL,
 CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED
(
    [C#] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Sc]    Script Date: 2016/12/6 0:16:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Sc](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [S#] [int] NOT NULL,
    [C#] [int] NOT NULL,
    [score] [int] NOT NULL,
 CONSTRAINT [PK_Score] PRIMARY KEY CLUSTERED
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Student]    Script Date: 2016/12/6 0:16:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student](
    [S#] [int] IDENTITY(1,1) NOT NULL,
    [Sname] [nvarchar](50) NOT NULL,
    [Sage] [int] NOT NULL,
    [Ssex] [nvarchar](1) NOT NULL,
 CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
    [S#] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Teacher]    Script Date: 2016/12/6 0:16:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Teacher](
    [T#] [int] IDENTITY(1,1) NOT NULL,
    [Tname] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Teacher] PRIMARY KEY CLUSTERED
(
    [T#] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[Course] ON 

INSERT [dbo].[Course] ([C#], [Cname], [T#]) VALUES (1, N‘语文‘, 1)
INSERT [dbo].[Course] ([C#], [Cname], [T#]) VALUES (2, N‘数学‘, 1)
INSERT [dbo].[Course] ([C#], [Cname], [T#]) VALUES (4, N‘政治‘, 2)
INSERT [dbo].[Course] ([C#], [Cname], [T#]) VALUES (5, N‘物理‘, 2)
SET IDENTITY_INSERT [dbo].[Course] OFF
SET IDENTITY_INSERT [dbo].[Sc] ON 

INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (1, 1, 1, 90)
INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (2, 1, 2, 80)
INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (3, 2, 1, 100)
INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (6, 2, 2, 50)
INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (7, 1, 4, 90)
INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (8, 1, 5, 90)
SET IDENTITY_INSERT [dbo].[Sc] OFF
SET IDENTITY_INSERT [dbo].[Student] ON 

INSERT [dbo].[Student] ([S#], [Sname], [Sage], [Ssex]) VALUES (1, N‘张三‘, 20, N‘男‘)
INSERT [dbo].[Student] ([S#], [Sname], [Sage], [Ssex]) VALUES (2, N‘李四‘, 21, N‘女‘)
INSERT [dbo].[Student] ([S#], [Sname], [Sage], [Ssex]) VALUES (3, N‘王五‘, 22, N‘男‘)
SET IDENTITY_INSERT [dbo].[Student] OFF
SET IDENTITY_INSERT [dbo].[Teacher] ON 

INSERT [dbo].[Teacher] ([T#], [Tname]) VALUES (1, N‘叶平‘)
INSERT [dbo].[Teacher] ([T#], [Tname]) VALUES (2, N‘我不叫叶平‘)
SET IDENTITY_INSERT [dbo].[Teacher] OFF
ALTER TABLE [dbo].[Course]  WITH CHECK ADD  CONSTRAINT [FK_Course_Teacher] FOREIGN KEY([T#])
REFERENCES [dbo].[Teacher] ([T#])
GO
ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_Teacher]
GO
ALTER TABLE [dbo].[Sc]  WITH CHECK ADD  CONSTRAINT [FK_Sc_Course] FOREIGN KEY([C#])
REFERENCES [dbo].[Course] ([C#])
GO
ALTER TABLE [dbo].[Sc] CHECK CONSTRAINT [FK_Sc_Course]
GO
ALTER TABLE [dbo].[Sc]  WITH CHECK ADD  CONSTRAINT [FK_Sc_Student] FOREIGN KEY([S#])
REFERENCES [dbo].[Student] ([S#])
GO
ALTER TABLE [dbo].[Sc] CHECK CONSTRAINT [FK_Sc_Student]
GO
USE [master]
GO
ALTER DATABASE [Example] SET  READ_WRITE
GO

题目

--1、查询"1"课程比"2"课程成绩高的所有学生的学号;
select d.S# from
(select b.S#,b.score as b_score,c.score as c_score from
(select S#,score from Sc WHERE C#=1) b inner join
(select S#,score  from Sc WHERE C#=2) c on b.S#=c.S#) d
where d.b_score>d.c_score
--2、查询平均成绩大于60分的学号和平均成绩;
select S#,AVG(score) from Sc Group by S# having AVG(score)>60
--3、查询所有同学的学号、姓名、选课数、总成绩;
SELECT a.*,
        CASE
        WHEN Num IS null THEN 0
        ELSE Num END AS ‘课程总数‘,
        CASE
        WHEN Total IS NULL THEN 0
        ELSE Total END AS ‘总成绩‘
FROM
(select S#,Sname from Student) a left join
(select S#,COUNT(*) AS Num,SUM(score) as Total from Sc Group BY S#) b --找所有同学的选课数和总成绩
on a.S#=b.S#
--4、查询姓"李"的老师个数;
select COUNT(*) from Teacher where Tname LIKE ‘李%‘
--5、查询没学过"叶平"老师课的同学的学号、姓名;
--(把学过学生的学号先找出来)
SELECT d.* FROM Student d left join
(
select distinct a.S# from Sc a inner join
(select C# from Course where T#=
(select T# from Teacher where Tname=‘叶平‘)) b on a.C#=b.C#
) c on c.S#=d.S# where c.S# is null
--6、查询学过"1"并且也学过编号"2"课程的同学的学号、姓名;
select b.* from
(select Distinct S# from Sc where C#=1 or C#=2 group by S# having COUNT(*)=2) a inner join
(select S#,Sname from Student) b on a.S#=b.S#
--7、查询学过"叶平"老师所交的所有课程的同学的学号、姓名;
select a.S# from
(select * from Sc) a inner join
(
select C# from Course where T#=
(select T# from Teacher where Tname=‘叶平‘)
) b on a.C#=b.C# group by a.S# having COUNT(*)=
(select COUNT(*) from Course where T#=
(select T# from Teacher where Tname=‘叶平‘))
--9、查询所有课程成绩小于60的同学的学号、姓名;
select b.* from
(select distinct S# from Sc WHERE score<60) a inner join
(select S#,Sname from Student) b on a.S#=b.S#
--10、查询没有学所有课的同学的学号、姓名;
select a.* from
(select S#,Sname from Student) a left join
(select S# from Sc group by S# having COUNT(*)=(select COUNT(*) from Course)) b
on a.S#=b.S# where b.S# is null
--11、查询至少有一门课与"张三"的同学相同的同学的学号和姓名;
select distinct a.S#,c.Sname from
(select S#,C# from Sc where S#!=(select S# from Student where Sname=‘张三‘)) a inner join
(select C# from Sc where S#=(select S# from Student where Sname=‘张三‘)) b on a.C#=b.C# inner join
(select S#,Sname from Student) c on a.S#=c.S#
--12、查询学过学号为"1"同学所有课的其他同学学号和姓名;
select d.* from
(
select S#  from
(select C# from Sc WHERE S#=1) a inner join
(select * from Sc where S#!=1) b on a.C#=b.C#
group by b.S# having COUNT(*)=(select COUNT(*) from Sc WHERE S#=1)
) c inner join
(select  S#,Sname from Student) d on c.S#=d.S#

 

原文地址:https://www.cnblogs.com/gougou1981/p/12347038.html

时间: 2024-11-09 18:53:18

12道必须掌握的Sql面试题的相关文章

12道Java算法与编程面试题

12道Java算法与编程面试题自己整理的面试题,希望可以帮到大家,需要更多资料的可以私信我哦,大家一起学习进步! 算法与编程1.编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔.答:package cn.itcast; import java.io.File;import java.io.FileReader;import java.io.FileWriter; public cla

12道算法与编程面试题

12道Java算法与编程面试题 自己整理的面试题,希望可以帮到大家,需要更多资料的可以私信我哦,大家一起学习进步!  算法与编程 1.编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔. 答: package cn.itcast; import java.io.File; import java.io.FileReader; import java.io.FileWriter; pub

sql面试题(学生表_课程表_成绩表_教师表)

sql面试题(学生表_课程表_成绩表_教师表) 原帖链接:http://bbs.csdn.net/topics/280002741 表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 建表语句 CREATE TABLE student ( s# INT, sname nvarchar(32), sage INT, ssex nvarchar(8)

【转载】经典10道c/c++语言经典笔试题(含全部所有参考答案)

经典10道c/c++语言经典笔试题(含全部所有参考答案) 1. 下面这段代码的输出是多少(在32位机上). char *p; char *q[20]; char *m[20][20]; int (*n)[10]; struct MyStruct { char dda; double dda1; int type ; }; MyStruct k; printf("%d %d %d %d %d",sizeof(p),sizeof(q),sizeof(m),sizeof(n),sizeof(

sql面试题一 学生成绩

sql面试题一 学生成绩 原帖链接:http://topic.csdn.net/u/20081020/15/1ABF54D0-F401-42AB-A75E-DF90027CEBA0.html 表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 建表语句 CREATE TABLE student ( s# INT, sname nvarchar(3

SQL面试题

有3个表S,C,SC S(SNO, SNAME)代表(学号,姓名) C(CNO,CNAME,CTEACHER)代表(课号,课名,教师) SC(SNO, CNO, SCGRADE)代表(学号,课号成绩) 问题: 1.找出没选过'黎明"老师的所有学生姓名. 2.列出两门以上(含2门)不及格学生姓名及平均成绩. 3.即学过1号课程有学过2号课所有学生的姓名. 请用标准SQL语言写出答案,方言也行(请说明是使用什么方言). 1.select sname from s join sc on (s.sno

汽车之家一道SQL 面试题,大家闲来无事都来敲一敲

写在前面 上周去汽车之家面试,拿到这个SQL笔试题顿时感觉到有些陌生,因为好长时间不写SQL语句了,当时只写了表设计,示例数据和SQL语句都没写出来. 汽车之家应该用的SQL Server, 编程题一般都没有固定答案,大家可以按照自己的思路来试试,我在文章底部放出参考的链接. SQL笔试题 参考答案(我个人发在CSDN的帖子) http://bbs.csdn.net/topics/391091585

12道人工智能领域的题目

12道人工智能领域的题目,答题的过程让大家都get到新姿势了吗? 在收到的48个回答中,有两位读者全部答对,真的是非常厉害啦! 今天,就让我们一起回顾一下题目,并看看正确答案的解释吧! 1.人工智能历史上第一个战胜人类的棋类游戏是? A.国际象棋 B.围棋 C.西洋双陆棋 D.四国军棋 1979年,由Hans Berliner(1929-2017)开发的一个西洋双陆棋程序,以7:1的成绩击败了当时的世界冠军Luigi Villa.创下了计算机第一次在智力游戏中击败冠军级别人类竞争对手的历史. 2

【软帝学院】12道java经典入门算法题!

12道java经典入门算法题! [程序1]   题目:将一个数组逆序输出. 程序分析:用第一个与最后一个交换. 其实,用循环控制变量更简单: for(int k=11;k>=1;k--) System.out.print(myarr[k]+","); [程序2]   题目:取一个整数a从右端开始的4-7位. 程序分析:可以这样考虑: (1)先使a右移4位. (2)设置一个低4位全为1,其余全为0的数.可用~(~0 < <4) (3)将上面二者进行&运算. pu