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,0 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