首先建立数据库各种语句的查询:
--1、语法格式
select * from authors --*代表所有列,没有where子句代表所有行select authorname, email, age --多个列名间用逗隔开
from authors
where age>20
order by age desc,authorname desc --按多列排序
--2、使用where字句来过滤部分行
--任务:查询35岁以上的女性作者的信息
use BooksManager
SELECT *
FROM Authors
WHERE age>35 AND sex=0
--3、查询40岁以上的男性作者的姓名和居住的城市
use BooksManager
SELECT AuthorName, City
FROM Authors
WHERE age>40 AND sex=1
--4、使用重命名列名(as字句,=)
use BooksManager
SELECT AuthorName as'作者姓名', City as'出生城市'
FROM Authors
WHERE age>40 AND sex=1
--5、使用+连接符连接多个字段
select '姓名:'+authorname+',年龄:'+convert(varchar,age)
from authors
select authorname+' '+city as 居住城市
from authors
--6、使用top关键字在查询中限制行数
--任务:订单金额最高的三个订单的会员编号与金额。
use BooksManager
SELECT top 3 CustomerID ,Upoint
FROM Customers
order by Upoint DESC
--7、查询30岁以上作者的姓名和生日
use BooksManager
SELECT AuthorName,Birthday
FROM Authors
WHERE age>30
--8、查询没有填写生日信息的作者有哪些
use BooksManager
SELECT AuthorName,Birthday, City
FROM Authors
WHERE Birthday IS NULL
--9、使用union关键字连接查询结果()
select customername as 姓名, city as 居住城市
from customers union
select authorname as 姓名, city as 居住城市
from authors
--10、查询单价在50元以上的图书信息,要求价格以5折显示,并按价格降序排列
use BooksManager
SELECT BookName as 图书名, Description as 描述,
UnitPrice *0.5 as 单价
From Books
WHERE UnitPrice <50
ORDER By UnitPrice DESC