码迷,mamicode.com
首页 > 数据库 > 详细

12道必须掌握的Sql面试题

时间:2020-02-22 22:07:03      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:neu   check   student   ping   掌握   没有   table   where   base   

建库脚本

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#

 

 

12道必须掌握的Sql面试题

标签:neu   check   student   ping   掌握   没有   table   where   base   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!