SQL第二周

3、①查询Customer表中的所有图书的信息

Use BookStore
SELECT *
From Customer

②查询Customer表中所有客户的信息,要求显示CustomerCode、Name、Hometown和Telephone字段信息

Use BookStore
SELECT CustomerCode,Name,Hometown,Telephone
From Customer

③查询客户等级(VIPClass)为A的客户编号(CustomerCode)和评价时间(Evaluate Data),要求显示的字段标题分别为“客户编号”和评价时间

Use BookStore
SELECT CustomerCode AS ["客户编号"],EvaluateDate AS ["评价时间"]
From CustomerEvaluation
WHERE VIPClass = 'A'

④查询2009年出版的少儿类图书,要求显示图书名称、作者、出版时间、价格、折扣,并且字段标题分别为“图书名称”、“作者”、“出版时间”、“价格”、“折扣”

Use BookStore
SELECT BookName AS ["图书名称"],Author AS ["作者"],PublishTime AS["出版时间"],
Price AS ["价格"],Discount AS ["折扣"]
From Book
WHERE BookSort ='少儿' AND PublishTime IN ('2009') 

Use BookStore
SELECT DISTINCT BookSort From Book

Use BookStore
SELECT TOP 3 OrderCode,OrderTime,OrderStatus FROM Order

USE BookStore
SELECT OrderCode,TotalPrice,OrderTime,OrderStatus FROM Order
WHERE TotalPrice>100 AND TotalPrice<200

⑧查询出版社代号为03,、21、31的出版社出版的图书信息

USE BookStore
SELECT BookCode,BookName,Author,PublisherCode FROM Book
WHERE PublisherCode IN ('03','21','31')

⑨查询图书名称中含有“程序”的图书信息

USE BookStore
SELECT BookCode,BookName,Author,Price,Discount FROM Book
WHERE BookName LIKE '%程序%'

4.①显示图书名称、原价、折扣及打折后的价格,并显示一列“说明”:未享客户等级优惠

USE BookStore
SELECT BookName,Price,Discount,Price*Discount AS 折后价格,'未享客户等级优惠' AS 说明 FROM Book

USE BookStore
SELECT MAX(PRICE) AS 最高价,MIN(PRICE) AS 最低价,AVG(PRICE) AS 平均价,
MAX(PRICE)-MIN(PRICE) AS 最高价与最低价的差值
FROM Book

这两问当时赶得急不会写

③查询销售总量为前20%的图书,并按总销量降序排列

USE BookStore
SELECT TOP 20 Percent Bookcode AS 书号,SUM(Amount) AS 总销量
From OrderDetail
GROUP BY Bookcode
Order by SUM(Amount) DESC

学习后仍会困惑GROUP BY的必要用处 

USE BookStore
SELECT BookName,Author  FROM Book
Where LEN(Author)>=8

题目之后也懒得补

你可能感兴趣的:(sql,数据库)