.NET高级工程师面试题之SQL篇(2)

时间:2024-08-03 02:46:28 学人智库 我要投稿
  • 相关推荐

.NET高级工程师面试题之SQL篇(2)

  GO

.NET高级工程师面试题之SQL篇(2)

  SET ANSI_PADDING ON

  GO

  CREATE TABLE [dbo].[Student](

  [stuID] [int] IDENTITY(1,1) NOT NULL,

  [stuName] [varchar](50) NOT NULL,

  [deptID] [int] NOT NULL,

  PRIMARY KEY CLUSTERED

  (

  [stuID] 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 ANSI_PADDING OFF

  GO

  SET IDENTITY_INSERT [dbo].[Student] ON

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (1, N'计算机张三', 1)

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (2, N'计算机李四', 1)

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (3, N'计算机王五', 1)

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (4, N'生物amy', 2)

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (5, N'生物kity', 2)

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (6, N'生物lucky', 2)

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (7, N'数学_yiming', 3)

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (8, N'数学_haoxue', 3)

  INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (9, N'数学_wuyong', 3)

  SET IDENTITY_INSERT [dbo].[Student] OFF

  /****** Object: Default [DF__Departmen__depNa__5441852A] Script Date: 05/11/2015 23:16:23 ******/

  ALTER TABLE [dbo].[Department] ADD DEFAULT ('') FOR [depName]

  GO

  /****** Object: Default [DF__Score__category__5EBF139D] Script Date: 05/11/2015 23:16:23 ******/

  ALTER TABLE [dbo].[Score] ADD DEFAULT ('') FOR [category]

  GO

  /****** Object: Default [DF__Score__score__5FB337D6] Script Date: 05/11/2015 23:16:23 ******/

  ALTER TABLE [dbo].[Score] ADD DEFAULT ((0)) FOR [score]

  GO

  /****** Object: Default [DF__Student__stuName__59063A47] Script Date: 05/11/2015 23:16:23 ******/

  ALTER TABLE [dbo].[Student] ADD DEFAULT ('') FOR [stuName]

  GO

  /****** Object: ForeignKey [FK__Student__deptID__59FA5E80] Script Date: 05/11/2015 23:16:23 ******/

  ALTER TABLE [dbo].[Student] WITH CHECK ADD FOREIGN KEY([deptID])

  REFERENCES [dbo].[Department] ([depID])

  GO

  准备环境

  3 结果

  面试的时候,没有写出来,当时脑袋昏沉沉的。也确实好久没有写复杂的sql语句了。今天花了2到3个小时,终于试出来了。不知道有没有更好的写法?

  -- 每个系里的最高分的学生信息

  SELECT Department.depID, Department.depName, Student.stuID, stuName, Dscore.scores

  FROM Department

  LEFT JOIN Student

  on department.depID = student.deptID

  LEFT JOIN (SELECT Score.stuId, SUM(Score) AS scores

  FROM Score

  GROUP by stuID

  ) AS Dscore

  on Student.stuID = dScore.stuID

  where exists (

  select *

  from

  (

  SELECT deptID, MAX(scores) AS topScores

  FROM Student

  LEFT JOIN

  (

  SELECT stuID,SUM(score) AS scores

  FROM Score

  GROUP BY stuID) AS newScore

  ON Student.stuID = newScore.stuID

  group by deptID) AS depScore

  where Department.depID = depScore.deptID and Dscore.scores=depScore.topScores

  )

  order by Department.depID,Student.stuID;

http://www.unjs.com/

【.NET高级工程师面试题之SQL篇(2)】相关文章:

考研复试之英语面试题目07-24

德国公司经典面试题(2)07-10

经典台词之《武林外传》(2)07-18

关于net论文的开题报告模板06-09

职业生涯规划之兴趣(2)07-21

Microsoft面试题09-04

iOS面试题07-10

公司面试题09-12

hibernate面试题10-18

英语面试题精选06-13