数据库基本查询

use pubs
go
----------------------------------------------------------------
--基本查询
----------------------------------------------------------------
--查询作者表中的所有记录(查询所有字段的值)
select * from authors

--查询pubs数据库中的作者表,列出所有作者的编号和姓、名(只列出指定字段的值)
select au_id,au_fname,au_lname from authors
select au_id ,au_fname+'.'+au_lname from authors

--查询pubs数据库的出版物表,列出所有出版物编号、出版物标题、类型和各个出版物打8折后的价格。
--(查询中带计算列,计算列是字段的值和某个数值做计算)
select title_id,title,type,price*0.8 from titles


--查询作者表中前10行记录(限制返回到结果集中的行数)
select top 10 * from authors


--查询作者表中前10%行的记录(限制返回到结果集中的行数)
select top 10 percent * from authors

--查询所有编写过书的作者ID(限制重复记录的查询)     
select distinct (au_id) from titleauthor

----------------------------------------------------------------
--自定义列标题
----------------------------------------------------------------
--显示所有作者的姓名信息和作者编号,要求所有的字段名称显示为中文(按要求显示列标题)
--写法一:
select au_id  作者编号,au_fname+'.'+au_lname  作者姓名 from authors

--写法二:
select au_id as 作者编号,au_fname+'.'+au_lname as 作者姓名 from authors

--写法三:
select  作者编号=au_id,作者姓名=au_fname+'.'+au_lname from authors


----------------------------------------------------------------
--条件查询
----------------------------------------------------------------
--查询在CA州的作者姓名和城市(使用where条件限制查询的结果集)
select au_fname+'.'+au_lname as 作者姓名,city as 所在城市 from authors where state='CA'


--查询出版社编号为0877,而且价格大于16美元的书的信息(多条件查询)
select * from titles where pub_id = '0877' and price > 16

--查找所有预付款不超过 $5,500 的商业和心理学丛书的信息(多个复杂条件查询)
select title_id, type, advance
from titles
where (type = 'business' or type = 'psychology') and not advance < 5500


--查询出版日期在1991-1-1到1991-12-31之间的书名和出版日期(多条件查询)
--写法一:
select title,pubdate from titles where pubdate>='1991-1-1' and pubdate<='1991-12-31'
--写法二:
select title,pubdate from titles where pubdate between '1991-1-1' and '1991-12-31'


--查询所有居住在CA、IN或MD州的作者信息。(多条件查询)
select * from authors
where state in('CA','IN','MD')

----------------------------------------------------------------
--模糊查询
----------------------------------------------------------------
--查找authors表中所有以415开头的电话号码(模糊查询)
select phone from authors where phone like '415%'

--查找名字为Chery1或Shery1的作者(模糊查询)
select au_fname from authors where au_fname like '[CS]heryl'

--查找名称以A~F以外的字符开头的出版社名(模糊查询)
--写法一:
select * from publishers where pub_name like '[^A-F]%'
--写法二:
select * from publishers where pub_name like '[^ABCDEF]%'

----------------------------------------------------------------
--排序查询
----------------------------------------------------------------
--列出business类图书名称和价格,并按照价格从大到小排列(排序查询)
--写法一:
select title,price from titles where type = 'business' order by price desc
--写法二:
select title,price from titles where type = 'business' order by 2 desc

----------------------------------------------------------------
--统计查询
----------------------------------------------------------------
--统计商业类图书的总数(数据统计查询)
select count(*) as 商业书籍总数 from titles where type = 'business'

--查看作者所分布州的数目(数据统计查询)
select count(distinct state) as 所属州总数 from authors

--查看商业书籍的平均价格(数据统计查询)
Select avg(price) as 商业书籍平均价格 from titles where type = 'business'

--查看商业书籍中价格最高的书籍价格(数据统计查询)
Select max(price) as 商业书的最高价格 from titles where type = 'business'

--查看商业书籍中价格最低的书籍价格(数据统计查询)
Select min(price) as 商业书的最低价格 from titles where type = 'business'

--查看所有商业书籍的总价格(数据统计查询)
Select sum(price) as 商业书的总价格 from titles where type = 'business'

----------------------------------------------------------------
--分组统计查询
----------------------------------------------------------------
--按图书类别分组统计图书的平均价格(分组统计)
select type,avg(price) as 平均价格 from titles group by type

--对版税为10%的图书按类别统计平均价格
select type,avg(price) from titles
where royalty = 10
group by type

--按图书类型统计价格大于19的图书的平均价格
select type,avg(price) from titles
where price>19
group by type

--按图书类型统计平均价格大于19的图书的平均价格
select type,avg(price) from titles
group by type 
having avg(price)>19

----------------------------------------------------------------
--汇总查询
----------------------------------------------------------------
--统计出所有书的总价格和总的预付款项
select price,advance from titles
compute sum(price),sum(advance)

--统计出每类图书的总价格和总的预付款项
select type,price,advance from titles
order by type
compute sum(price),sum(advance) by type

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值