1.
(1)
USE Library
GO
SELECT TOP 10 PERCENT *
FROM Readers
GO
(2)
USE Library
GO
SELECT 种类编号 AS 读者种类编号,种类名称 AS 读者种类名称
FROM ReadCateGOry
GO
(3)
USE Library
GO
SELECT *
FROM Readers
WHERE year(登记日期)=2004
GO
(4)
USE Library
GO
SELECT *
FROM Readers
WHERE 姓名 LIKE '李_'
GO
(5)
USE Library
GO
SELECT *
FROM Books
WHERE 图书名 IN('统计学','数据结构')
GO
(6)
USE Library
GO
SELECT *
FROM Books
WHERE 价格 BETWEEN 10 AND 20
ORDER BY 价格
GO
2.
(1)
USE Library
GO
SELECT 读者编号,COUNT(*) AS 数量
FROM Borrow
GROUP BY 读者编号
GO
(2)
USE Library
GO
SELECT 类编号,COUNT(*) AS 数量
FROM Books
GROUP BY 类编号
HAVING COUNT(*)>8
ORDER BY 数量
GO
(3)
USE Library
GO
SELECT 图书名,类编号,价格
FROM Books
ORDER BY 类编号
COMPUTE AVG(价格),MAX(价格),MIN(价格) BY (类编号)
GO
3.
(1)
GO
SELECT 姓名,图书名,借阅日期,应还日期
FROM Readers AS a JOIN Borrow AS b
ON a.读者编号=b.读者编号
JOIN Books AS c
ON b.图书编号=c.图书编号
GO
也可用下面代码实现:
USE Library
GO
SELECT 姓名,图书名,借阅日期,应还日期
FROM Readers AS a ,Borrow AS b, Books AS c
WHERE a.读者编号=b.读者编号 AND b.图书编号=c.图书编号
GO
(2)
USE Library
GO
SELECT a.*
FROM Readers AS a JOIN Readers AS b
ON a.姓名=b.姓名
WHERE a.读者编号<>b.读者编号
ORDER BY a.读者编号
GO
4.
(1)
USE Library
GO
SELECT *
FROM Books
WHERE 类编号=
( SELECT 类编号
FROM BookCateGOry
WHERE 类名称='经济学'
)
GO
(2)
USE Library
GO
SELECT *
FROM books
WHERE 图书编号 in
(
SELECT 图书编号
FROM borrow
WHERE 读者编号=
(
SELECT 读者编号
FROM readers
WHERE 姓名='高红'
)
)
(3)
USE Library
GO
SELECT *
FROM Readers
WHERE 读者编号=
(
SELECT 读者编号
FROM Borrow
WHERE 图书编号=
( SELECT 图书编号
FROM Books
WHERE 图书名='软件工程'
)
)
GO
(4)
USE Library
GO
SELECT *
FROM Readers AS a
WHERE NOT EXISTS
( SELECT *
FROM Borrow AS b
WHERE b.读者编号=a.读者编号
)
AND NOT EXISTS
( SELECT *
FROM BorrowHistory AS c
WHERE c.读者编号=a.读者编号
)
GO
5.
(1)
SELECT * FROM Employees
SELECT employeeID, Lastname, Firstname FROM Employees
(2)
SELECT top 2 * FROM Employees
SELECT top 50 percent * FROM Employees
(3)
USE Northwind
SELECT DISTINCT country
FROM Suppliers
ORDER BY country
GO
(4)
USE Northwind
SELECT productid, productname, categoryid, unitprice
FROM Products
ORDER BY categoryid, unitprice DESC
GO
(5)
USE Northwind
SELECT lastname, city FROM employees WHERE country = 'USA'
GO
USE Northwind
SELECT productname, unitprice FROM Products
WHERE unitprice BETWEEN 10 AND 20
GO
USE Northwind
SELECT companyname, country FROM Suppliers
WHERE country IN ('Japan', 'Italy')
GO
uctid、 productname、 supplierid和 unitprice。
USE Northwind
SELECT productid, productname, supplierid, unitprice FROM Products
WHERE (productname LIKE 'T%' OR productid = 46) AND (unitprice > 16.00)
GO
6.
(1)
SELECT student.学号,student_course.学生姓名,student_course.课程号,course.课程名,student_course.成绩 FROM student, student_course,course
WHERE student.学号=student_course.学号
and course.课程号=student_course.课程号
(2)
SELECT 学号 AS 代码,学生姓名 AS 姓名 FROM student
union
SELECT 教工号,姓名 FROM teacher
(3)
SELECT ‘学号’=student.学号,‘总成绩’=SUM(成绩) FROM student_course
GROUP BY 学号
SELECT ‘学号’=学号,‘总成绩’=SUM(成绩)FROM student_course
GROUP BY 学号 HAVING SUM(成绩)>450
(4)
SELECT ‘学号’=学号,‘成绩’=成绩 FROM student_course
ORDER BY 学号 COMPUTE SUM(成绩)
SELECT ‘学号’=学号,‘成绩’=成绩 FROM student_course
ORDER BY 学号
COMPUTE SUM(成绩)BY 学号
(5)
SELECT student_course.学号, student_course.课程号,student_course.成绩
FROM student_course
WHERE 学号 IN
(SELECT 学号 FROM student
WHERE 班级=’j10011’AND 性别=1)
SELECT student_course.学号,student_course.课程号,student_course.成绩
FROM student_course
WHERE EXIST
(SELECT * FROM student
WHERE student_course.学号=student.学号 AND student.班级=’j10011’)