SQL Server中语句的查询基础

首先建立数据库各种语句的查询:



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













































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值