我们都知道面试的时候通常都会考数据库部分的知识,所以在此整理了下我们常用的面试中常用的一些查询语句SQL,本人测试数据库为SQL Server2008
首先在数据库中建立一个测试库,此处用DavidTest代替
USE [DavidTest] GO /****** Object: Table [dbo].[Teacher] Script Date: 02/19/2013 01:09:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Teacher]([TId] [varchar](10) NULL,[Tname] [nvarchar](10) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[Teacher] ([TId], [Tname]) VALUES (N'01', N'张三') INSERT [dbo].[Teacher] ([TId], [Tname]) VALUES (N'02', N'李四') INSERT [dbo].[Teacher] ([TId], [Tname]) VALUES (N'03', N'王五') /****** Object: Table [dbo].[Student] Script Date: 02/19/2013 01:09:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Student]([SId] [varchar](10) NULL,[Sname] [nvarchar](10) NULL,[Sage] [datetime] NULL,[Ssex] [nvarchar](10) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[Student] ([SId], [Sname], [Sage], [Ssex]) VALUES (N'01', N'赵雷', CAST(0x0000806800000000 AS DateTime), N'男') INSERT [dbo].[Student] ([SId], [Sname], [Sage], [Ssex]) VALUES (N'02', N'钱电', CAST(0x000081CA00000000 AS DateTime), N'男') INSERT [dbo].[Student] ([SId], [Sname], [Sage], [Ssex]) VALUES (N'03', N'孙风', CAST(0x000080F300000000 AS DateTime), N'男') INSERT [dbo].[Student] ([SId], [Sname], [Sage], [Ssex]) VALUES (N'04', N'李云', CAST(0x0000814100000000 AS DateTime), N'男') INSERT [dbo].[Student] ([SId], [Sname], [Sage], [Ssex]) VALUES (N'05', N'周梅', CAST(0x0000832300000000 AS DateTime), N'女') INSERT [dbo].[Student] ([SId], [Sname], [Sage], [Ssex]) VALUES (N'06', N'吴兰', CAST(0x0000837E00000000 AS DateTime), N'女') INSERT [dbo].[Student] ([SId], [Sname], [Sage], [Ssex]) VALUES (N'07', N'郑竹', CAST(0x00007FB000000000 AS DateTime), N'女') INSERT [dbo].[Student] ([SId], [Sname], [Sage], [Ssex]) VALUES (N'08', N'王菊', CAST(0x0000807B00000000 AS DateTime), N'女') /****** Object: Table [dbo].[SC] Script Date: 02/19/2013 01:09:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[SC]([SId] [varchar](10) NULL,[CId] [varchar](10) NULL,[score] [decimal](18, 1) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'01', N'01', CAST(59.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'01', N'02', CAST(89.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'01', N'03', CAST(99.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'02', N'01', CAST(70.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'02', N'02', CAST(60.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'02', N'03', CAST(80.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'03', N'01', CAST(80.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'03', N'02', CAST(80.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'03', N'03', CAST(80.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'04', N'01', CAST(50.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'04', N'02', CAST(30.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'04', N'03', CAST(20.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'05', N'01', CAST(76.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'05', N'02', CAST(87.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'06', N'01', CAST(31.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'06', N'03', CAST(34.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'07', N'02', CAST(89.0 AS Decimal(18, 1))) INSERT [dbo].[SC] ([SId], [CId], [score]) VALUES (N'07', N'03', CAST(98.0 AS Decimal(18, 1))) /****** Object: Table [dbo].[Course] Script Date: 02/19/2013 01:09:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Course]([CId] [varchar](10) NULL,[Cname] [nvarchar](10) NULL,[TId] [varchar](10) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[Course] ([CId], [Cname], [TId]) VALUES (N'01', N'语文', N'02') INSERT [dbo].[Course] ([CId], [Cname], [TId]) VALUES (N'02', N'数学', N'01') INSERT [dbo].[Course] ([CId], [Cname], [TId]) VALUES (N'03', N'英语', N'03')
接下来使我们常用的一些面试查询SQL
--1. 查询"01"课程比"02"课程成绩高的学生的信息及课程分数 SELECT dbo.Student.SId, dbo.SC.CId, dbo.Course.Cname,sc.score FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId WHERE dbo.Student.SId IN (SELECT DISTINCT a.SId FROM dbo.SC a INNER JOIN dbo.SC b ON a.SId = b.SId WHERE a.CId='01' AND b.CId='02' AND a.score>b.score) AND dbo.Course.CId IN ('01', '02') ORDER BY dbo.SC.SId--2 查询同时存在"01"课程和"02"课程的情况 SELECT * FROM dbo.Student WHERE SId IN (SELECT SId FROM dbo.SC WHERE CId IN ('01','02') GROUP BY SId HAVING COUNT(CId)=2)--3. 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 SELECT SC.SId, dbo.Student.Sname, ROUND(CAST(SUM(SC.score) AS FLOAT)/3,4) avg_score FROM dbo.SC LEFT JOIN dbo.Student ON dbo.SC.SId = dbo.Student.SId GROUP BY SC.SId,Student.Sname HAVING ROUND(CAST(SUM(SC.score) AS FLOAT)/3,4)>=60 ORDER BY dbo.SC.SId--4. 查询在sc表存在成绩的学生信息的SQL语句。 SELECT dbo.Student.SId, dbo.Student.Sname, dbo.Student.Ssex FROM dbo.Student WHERE EXISTS (SELECT 1 FROM sc WHERE dbo.Student.SId = dbo.SC.SId)--5. 查询在sc表中不存在成绩的学生信息的SQL语句。 SELECT dbo.Student.SId, dbo.Student.Sname, dbo.Student.Ssex FROM dbo.Student WHERE NOT EXISTS (SELECT 1 FROM sc WHERE dbo.Student.SId = dbo.SC.SId)--6. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 SELECT dbo.Student.SId, dbo.Student.Sname, COUNT(dbo.SC.CId) Course_Count, ISNULL(SUM(dbo.SC.score),0) Total_Score FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId GROUP BY dbo.Student.SId, dbo.Student.Sname ORDER BY dbo.Student.SId--7. 查询所有有成绩的学生SQL SELECT * FROM dbo.Student WHERE EXISTS (SELECT 1 FROM dbo.SC WHERE dbo.Student.SId = dbo.SC.SId)--8. 查询"李"姓老师的数量 SELECT COUNT(1) Count FROM dbo.Teacher WHERE Tname LIKE '李%'--9. 查询学过"张三"老师授课的同学的信息SELECT dbo.Student.SId, dbo.Student.Sname,dbo.Teacher.Tname FROM dbo.StudentLEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SIdLEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CIdLEFT JOIN dbo.Teacher ON dbo.Course.TId = dbo.Teacher.TIdWHERE dbo.Teacher.Tname = '张三'ORDER BY dbo.Student.SId--10. 查询没学过"张三"老师授课的同学的信息 SELECT * FROM dbo.Student WHERE SId NOT IN (SELECT dbo.Student.SId FROM dbo.StudentLEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SIdLEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CIdLEFT JOIN dbo.Teacher ON dbo.Course.TId = dbo.Teacher.TIdWHERE dbo.Teacher.Tname = '张三')--11. 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 SELECT * FROM dbo.Student WHERE EXISTS (SELECT 1 FROM dbo.SC WHERE SC.CId='01' AND dbo.SC.SId=dbo.Student.SId) AND EXISTS (SELECT 1 FROM dbo.SC WHERE dbo.SC.CId='02' AND dbo.SC.SId=dbo.Student.SId)--12. 查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 SELECT * FROM dbo.Student WHERE EXISTS (SELECT 1 FROM dbo.SC WHERE SC.CId='01' AND dbo.SC.SId=dbo.Student.SId) AND NOT EXISTS (SELECT 1 FROM dbo.SC WHERE dbo.SC.CId='02' AND dbo.SC.SId=dbo.Student.SId) --另一种写法 SELECT * FROM dbo.Student WHERE dbo.Student.Sid IN (SELECT dbo.SC.Sid FROM dbo.SC WHERE Cid = '01') AND dbo.Student.Sid NOT IN (SELECT dbo.SC.Sid FROM dbo.SC WHERE Cid = '02')--13. 查询没有学全所有课程的同学的信息 SELECT * FROM dbo.Student WHERE SId NOT IN ( SELECT SId FROM dbo.Student WHERE EXISTS (SELECT 1 FROM SC WHERE dbo.SC.CId='01' AND dbo.Student.SId=SId) AND EXISTS (SELECT 1 FROM SC WHERE dbo.SC.CId='02' AND dbo.Student.SId=SId) AND EXISTS (SELECT 1 FROM SC WHERE dbo.SC.CId='03' AND dbo.Student.SId=SId)) ORDER BY dbo.Student.SId--另一种写法 SELECT * FROM dbo.Student WHERE SId IN ( SELECT dbo.Student.SId FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId GROUP BY dbo.Student.SId HAVING COUNT(dbo.SC.CId)<> 3)--14. 查询至少有一门课与学号为"01"的同学所学相同的同学的信息 SELECT * FROM dbo.Student WHERE SId IN (SELECT SId FROM dbo.SC WHERE CId IN (SELECT cid FROM dbo.SC WHERE SId = '01') AND dbo.Student.SId = dbo.SC.SId)--15. 查询和"01"号的同学学习的课程完全相同的其他同学的信息 SELECT * FROM dbo.Student WHERE dbo.Student.SId IN ( SELECT dbo.SC.SId FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId GROUP BY dbo.SC.SId HAVING COUNT(dbo.SC.CId) = (SELECT Count(CId) FROM dbo.SC WHERE SId = '01'))--16. 查询没学过"张三"老师讲授的任一门课程的学生姓名 SELECT * FROM dbo.Student WHERE NOT EXISTS (SELECT 1 FROM SC WHERE dbo.Student.SId = dbo.SC.SId AND CId = (SELECT CId FROM dbo.Course WHERE TId = (SELECT TId FROM dbo.Teacher WHERE Tname='张三'))) --另一种写法 SELECT * FROM dbo.Student WHERE SId NOT IN (SELECT SId FROM SC WHERE CId = (SELECT CId FROM dbo.Course WHERE TId = (SELECT TId FROM dbo.Teacher WHERE Tname='张三')))--17.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 SELECT dbo.Student.SId,dbo.Student.Sname,AVG(dbo.SC.score) AVG_Score FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId WHERE Student.SId IN ( SELECT SId FROM dbo.SC WHERE score<60 GROUP BY SId HAVING COUNT(CId)>=2) GROUP BY dbo.Student.SId,dbo.Student.Sname ORDER BY dbo.Student.SID--16、检索"01"课程分数小于60,按分数降序排列的学生信息 SELECT dbo.Student.*,dbo.SC.score FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId AND CId='01' WHERE Student.SId IN ( SELECT SId FROM dbo.SC WHERE CId='01' AND score<60) ORDER BY dbo.Student.SId DESC--另一种 SELECT dbo.Student.SId,dbo.Student.Sname,dbo.SC.score FROM dbo.SC LEFT JOIN dbo.Student ON dbo.SC.SId = dbo.Student.SId WHERE CId='01' AND score<60 ORDER BY dbo.Student.SId DESC--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 SELECT avg_tab.SId,dbo.Student.Sname,dbo.Course.CId,dbo.Course.Cname,dbo.SC.score,avg_tab.Avg_Score FROM ( SELECT dbo.SC.SId, AVG(dbo.SC.score) Avg_Score FROM dbo.SC GROUP BY dbo.SC.SId) avg_tab LEFT JOIN dbo.SC ON avg_tab.SId = dbo.SC.SId LEFT JOIN dbo.Student ON avg_tab.SId = dbo.Student.SId LEFT JOIN dbo.Course ON dbo.SC.CId=dbo.Course.CId ORDER BY avg_tab.Avg_Score DESC--另一种方法 select a.Sid 学生编号, a.Sname 学生姓名,max(case c.Cname when N'语文' then b.score else null end) [语文],--max(case WHEN c.Cname LIKE N'语文' then b.score else null end) [语文], 此种写法也OKmax(case c.Cname when N'数学' then b.score else null end) [数学],max(case c.Cname when N'英语' then b.score else null end) [英语],cast(avg(b.score) as decimal(18,2)) 平均分 from Student a left join SC b on a.Sid = b.Sid left join Course c on b.Cid = c.Cid group by a.Sid , a.Sname order by 平均分 DESC--18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 --及格为>=60,中等为:-80,优良为:-90,优秀为:>=90 (SELECT dbo.SC.CId,dbo.Course.Cname, MAX(CASE dbo.Course.Cname WHEN N'语文' THEN dbo.SC.score ELSE NULL END) 最高分, MIN(CASE dbo.Course.Cname WHEN N'语文' THEN dbo.SC.score ELSE NULL END) 最低分, Avg(CASE dbo.Course.Cname WHEN N'语文' THEN dbo.SC.score ELSE NULL END) 平均分 FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId WHERE dbo.Course.CId='01' GROUP BY sc.CId, dbo.Course.Cname) UNION ( SELECT dbo.SC.CId,dbo.Course.Cname, MAX(CASE dbo.Course.Cname WHEN N'数学' THEN dbo.SC.score ELSE NULL END) 最高分, MIN(CASE dbo.Course.Cname WHEN N'数学' THEN dbo.SC.score ELSE NULL END) 最低分, Avg(CASE dbo.Course.Cname WHEN N'数学' THEN dbo.SC.score ELSE NULL END) 平均分 FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId WHERE dbo.Course.CId='02' GROUP BY sc.CId, dbo.Course.Cname ) UNION ( SELECT dbo.SC.CId,dbo.Course.Cname, MAX(CASE dbo.Course.Cname WHEN N'英语' THEN dbo.SC.score ELSE NULL END) 最高分, MIN(CASE dbo.Course.Cname WHEN N'英语' THEN dbo.SC.score ELSE NULL END) 最低分, Avg(CASE dbo.Course.Cname WHEN N'英语' THEN dbo.SC.score ELSE NULL END) 平均分 FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId WHERE dbo.Course.CId='03' GROUP BY sc.CId, dbo.Course.Cname ) --另一种方法 select m.Cid [课程编号], m.Cname [课程名称], max(n.score) [最高分],min(n.score) [最低分],cast(avg(n.score) as decimal(18,2)) [平均分],cast((select count(1) from SC where Cid = m.Cid and score >= 60)*100.0 / (select count(1) from SC where Cid = m.Cid) as decimal(18,2)) [及格率(%)],cast((select count(1) from SC where Cid = m.Cid and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where Cid = m.Cid) as decimal(18,2)) [中等率(%)],cast((select count(1) from SC where Cid = m.Cid and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where Cid = m.Cid) as decimal(18,2)) [优良率(%)],cast((select count(1) from SC where Cid = m.Cid and score >= 90)*100.0 / (select count(1) from SC where Cid = m.Cid) as decimal(18,2)) [优秀率(%)] from Course m, SC n where m.Cid = n.Cid group by m.Cid, m.Cname order by m.Cid--19. 查询不同老师所教不同课程平均分从高到低显示 SELECT dbo.SC.CId CourseId, dbo.Course.Cname CourseName, dbo.Teacher.Tname TeacherName, AVG(dbo.SC.score) Avg_Score FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId LEFT JOIN dbo.Teacher ON dbo.Course.TId = dbo.Teacher.TId GROUP BY dbo.SC.CId,dbo.Course.Cname,dbo.Teacher.Tname ORDER BY AVG(dbo.SC.score) DESC--20.查询如下课程平均成绩第3 名到第6 名的学生成绩单 SELECT SCInfo.RowNumber,SCInfo.SId,SCInfo.Sname,SCInfo.Ssex,SCInfo.Avg_Score FROM (SELECT ROW_NUMBER() OVER (ORDER BY AVG(dbo.SC.score)) RowNumber, dbo.SC.SId, dbo.Student.Sname,dbo.Student.Ssex, AVG(dbo.SC.score) Avg_Score FROM dbo.SCLEFT JOIN dbo.Student ON dbo.SC.SId = dbo.Student.SIdGROUP BY dbo.SC.SId,dbo.Student.SId,dbo.Student.Sname,dbo.Student.Ssex) SCInfo WHERE SCInfo.RowNumber >=3 AND SCInfo.RowNumber<=6--21. 统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] (横向显示-列转行) SELECT dbo.SC.CId, dbo.Course.Cname, SUM(CASE WHEN dbo.SC.score >=85 AND dbo.SC.score<=100 THEN 1 ELSE 0 END) '分数段85-100', SUM(CASE WHEN dbo.SC.score >=70 AND dbo.SC.score<85 THEN 1 ELSE 0 END) '分数段70-85', SUM(CASE WHEN dbo.SC.score >=60 AND dbo.SC.score<70 THEN 1 ELSE 0 END) '分数段60-70', SUM(CASE WHEN dbo.SC.score <60 THEN 1 ELSE 0 END) '分数段<60' FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId GROUP BY dbo.SC.CId,dbo.Course.Cname--21. 统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] (横向显示-行转列) SELECT dbo.SC.CId, dbo.Course.Cname, 分数段= (CASE WHEN dbo.SC.score >=85 AND dbo.SC.score<=100 THEN '85-100'WHEN dbo.SC.score >=70 AND dbo.SC.score<85 THEN '70-85'WHEN dbo.SC.score >=60 AND dbo.SC.score<70 THEN '60-70'WHEN dbo.SC.score <60 THEN '<60'END ), COUNT(1) Partial_Count FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId GROUP BY dbo.SC.CId,dbo.Course.Cname, (CASE WHEN dbo.SC.score >=85 AND dbo.SC.score<=100 THEN '85-100'WHEN dbo.SC.score >=70 AND dbo.SC.score<85 THEN '70-85'WHEN dbo.SC.score >=60 AND dbo.SC.score<70 THEN '60-70'WHEN dbo.SC.score <60 THEN '<60'END ) ORDER BY dbo.SC.CId,dbo.Course.Cname--22. 查询学生平均成绩及其名次 SELECT Student_RankInfo.* FROM (SELECT dbo.SC.SId, dbo.Student.Sname,dbo.Student.Ssex,ROW_NUMBER() OVER (ORDER BY AVG(dbo.SC.score)) RankNumber, AVG(dbo.SC.score) Avg_Score FROM dbo.SCLEFT JOIN dbo.Student ON dbo.SC.SId = dbo.Student.SIdGROUP BY dbo.SC.SId,dbo.Student.Sname,dbo.Student.Ssex ) Student_RankInfo ORDER BY Student_RankInfo.SId--23. 查询各科成绩前三名的记录考虑成绩并列情况 --23.1 分数重复时保留名次空缺Rank实现,PARTITION BY分区分组,Group By整体分组一般与Sum,Avg,等统计函数一起使用 SELECT ScoreRankInfo.* FROM (SELECT dbo.SC.SId,dbo.Student.Sname,dbo.SC.CId,dbo.SC.score, RankNumber = RANK() OVER (PARTITION BY dbo.SC.CId ORDER BY dbo.SC.score DESC) FROM dbo.SCLEFT JOIN dbo.Student ON dbo.SC.SId = dbo.Student.SId ) ScoreRankInfo--23.2 分数重复不保留名次空缺DENSE_RANK实现 SELECT ScoreRankInfo.* FROM (SELECT dbo.SC.SId,dbo.Student.Sname,dbo.SC.CId,dbo.SC.score, RankNumber = DENSE_RANK() OVER (PARTITION BY dbo.SC.CId ORDER BY dbo.SC.score DESC) FROM dbo.SCLEFT JOIN dbo.Student ON dbo.SC.SId = dbo.Student.SId ) ScoreRankInfo--23.3 rownumber实现不区分分数相同时的rank SELECT ScoreRankInfo.* FROM (SELECT dbo.SC.SId,dbo.Student.Sname,dbo.SC.CId,dbo.SC.score, RankNumber = ROW_NUMBER() OVER (PARTITION BY dbo.SC.CId ORDER BY dbo.SC.score DESC) FROM dbo.SCLEFT JOIN dbo.Student ON dbo.SC.SId = dbo.Student.SId ) ScoreRankInfo--24. 查询每门课程被选修的学生数 SELECT dbo.SC.CId, dbo.Course.Cname, COUNT(dbo.SC.SId) PersonCount FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId GROUP BY dbo.SC.CId,dbo.Course.Cname--分区分组 SELECT dbo.SC.CId, dbo.Course.Cname, dbo.SC.SId, COUNT(dbo.SC.SId) OVER(PARTITION BY dbo.SC.SId) PersonCount FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId ORDER BY dbo.SC.CId--25. 查询出只选修了两门课程的全部学生的学号和姓名 SELECT dbo.Student.SId,dbo.Student.Sname,dbo.Student.Ssex FROM dbo.Student WHERE dbo.Student.SId IN (SELECT dbo.SC.SId FROM dbo.SC GROUP BY SId HAVING COUNT(dbo.SC.CId)=2) ORDER BY dbo.Student.SId--26. 查询男生、女生人数 SELECT dbo.Student.Ssex, COUNT(dbo.Student.SId) StudentCount FROM dbo.Student GROUP BY dbo.Student.Ssex--列转行显示 SELECT SUM((CASE dbo.Student.Ssex WHEN '男' THEN 1 ELSE 0 END)) 男生人数, SUM((CASE dbo.Student.Ssex WHEN '女' THEN 1 ELSE 0 END)) 女生人数 FROM dbo.Student--27. 查询姓“张”的学生名单 SELECT * FROM dbo.Student WHERE dbo.Student.Sname LIKE '钱%'--28. 查询课程名称为"数学",且分数低于60的学生姓名和分数 SELECT dbo.Student.SId,dbo.Student.Sname,dbo.Course.CId,dbo.Course.Cname,dbo.SC.score FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId WHERE dbo.Course.Cname='数学' AND dbo.SC.score<60--30. 查询所有学生的选课情况及各个课程排名情况 SELECT dbo.Student.*,dbo.Course.CId,dbo.Course.Cname, dbo.SC.score, RANK() OVER(PARTITION BY dbo.Student.SId ORDER BY dbo.SC.score DESC) Rank FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId ORDER BY dbo.Student.SId--31. 查询任何一门课程成绩在70分以上的姓名、课程名称和分数 SELECT dbo.Student.SId,dbo.Student.Sname,dbo.Course.Cname,dbo.SC.score FROM dbo.Student LEFT JOIN dbo.SC ON dbo.Student.SId = dbo.SC.SId LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId WHERE EXISTS (SELECT 1 FROM dbo.SC WHERE score>=70 AND SId=dbo.Student.SId)--32. 查询每位学生成绩前2名的课程及分数 SELECT DISTINCT tempTable.SId,dbo.Student.Sname,tempTable.CId,dbo.Course.Cname,tempTable.score FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY dbo.SC.SId ORDER BY dbo.SC.score DESC) RowNumber, dbo.SC.SId,dbo.SC.CId,sc.score FROM dbo.SC ) tempTable LEFT JOIN dbo.Student ON tempTable.SId = dbo.Student.SId LEFT JOIN dbo.Course ON tempTable.CId = dbo.Course.CId WHERE RowNumber BETWEEN 1 AND 2--33. 查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 SELECT TOP 1 dbo.SC.SId,dbo.Student.Sname, dbo.SC.score FROM dbo.SC LEFT JOIN dbo.Course ON dbo.SC.CId = dbo.Course.CId LEFT JOIN dbo.Teacher ON dbo.Course.TId = dbo.Teacher.TId LEFT JOIN dbo.Student ON dbo.Student.SId = dbo.SC.SId WHERE dbo.Teacher.Tname='张三' ORDER BY dbo.SC.score DESC
在整理和巩固这些东西的过程中也重新学到了以前一些不大注意的地方和理解有误的地方:
1. 以前没有关注过SQL的具体执行顺序,后来在博客园里面也找到一片别人整理的比较好的文章,http://blog.csdn.net/gyc1105/article/details/8074545
2. 以前没仔细研究查看过的over partition by与group by的区别,http://blog.csdn.net/gyc1105/article/details/8074545
整理学习的越多越能发现自己还有许多地方需要补充~ 在此先共享下,希望对园子内的朋友有帮助喽~