T-SQL面试题(补充中~)

1.

Write one T-SQL script to get employees with the top 10 in salary amount and whose name is started with character ‘L’ (for example Liu, Li…) from the employee table. (Employee table includes “ID”, “name”, and “Salary” columns)

 

SELECT TOP(10) ID, Name, Salary FROM dbo.employees WHERE Name LIKE 'L%'ORDER BY Salary DESC

 

2.

Here we have below 3 tables.

--Three tables' script:

CREATE TABLE dbo.Students

(

      StudentID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,

      StudentName NVARCHAR(MAX)

)

 

CREATE TABLE dbo.Courses

(

      CourseID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,

      CourseName NVARCHAR(200)

)

 

CREATE TABLE dbo.Scores

(

      StudentID INT NOT NULL,

      CourseID INT NOT NULL,

      Score INT NOT NULL

)

 

(1)Supposing every student has same courses; No record in Table “Sores” for the students who don’t take exams. Questions as below:

Query all students who don’t have scores

SELECT s.StudentID,c.CourseID FROM dbo.Students s CROSS JOIN dbo.Courses c

EXCEPT SELECT sc.StudentID,sc.CourseID FROM dbo.Scores sc

 

(2)Set scores of the students who don’t take exams as “0”, and update Table “scores”

INSERT INTO dbo.Scores(StudentID,CourseID,Score)

SELECT StudentID,CourseID,FROM(

SELECT s.StudentID,c.CourseID FROM dbo.Students s

CROSS JOIN dbo.Courses c

EXCEPT SELECT sc.StudentID,sc.CourseID FROM dbo.Scores sc) AS T1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值