数据库查询

来源:互联网 发布:python shelve 编辑:程序博客网 时间:2024/06/07 00:20

代码:

/*********************************查询练习题*************************************/--1.搜索pubs数据库中的titles表,返回书的价格打了8折后仍大于12美元的书的代号、种类、书的原价。select Title_id,Type,Pricefrom Titleswhere Price*0.8>12--2.查询pubs数据库中titles表价格在10-20美元之间且种类为business或popular_comp的所有图书。(两种方法)select *from Titleswhere (Price>=10 and Price<=20) and (Type='business' or Type='popular_comp')select *from Titleswhere Price between 10and 20 and Type in ('business','popular_comp')--3.3.查询pubs数据库中titles表,查找价格小于15,大于20的书的书号,种类,价格。(使用两种方法)select Title_id,Type,Pricefrom Titleswhere Price not between 15 and 20select Title_id,Type,Pricefrom Titleswhere Price<15 or Price>20--4.查询titles表中没有价格的图书。select *from Titleswhere Price is null--5.找出pubs数据库titles表中价格最高的图书的价格。select max(Price)'最高价'from Titles--6.使用Publishers表,按照州进行分类,查找每个州有几名作者、、、、、、、、、、、、、、、、、、、、、、、select authors.state,count(*)from publishers right join authors on Publishers.state=authors.stategroup by authors.state--7.在pubs数据库的titles表中,找出平均价格大于18美元的书的种类select Typefrom Titlesgroup by Typehaving avg(Price)>18--8.查询pubs数据库中每个作者的编号,姓名,所出的书的编号,并对结果排序。----------select Authors.au_id,au_lname+' '+au_fname as [name],Title_idfrom Authors inner join Titleauthor on Authors.au_id=Titleauthor.au_id order by Authors.au_id--9.查找由位于以字母 B 开头的城市中的任一出版商出版的书名------------SELECT t.titleFROM titles t INNER JOIN publishers pON t.pub_id=p.pub_idWHERE p.city LIKE 'B%'--10.找出pubs数据库titles表中计算机类图书中价格最高的图书的价格。select max(Price) maxPricefrom Titleswhere [Type]='计算机'--11.查询titles表中有几类图书。select count(distinct [Type])from Titles--12.查询pub数据库的titles表,按书的种类分类,求出每种类型的书的平均价格select [Type],avg(Price)from Titlesgroup by [Type]--13.查询pubs数据库authors表,要求按照州进行分类,查找每个州有几名作者。select [State],count(au_id) '作者数'from Authorsgroup by [State]--14.查询pubs数据库titles表,要求按照出版商id进行分类,查找每个出版商的书到目前为止的销售额总和(ytd_sales)---------select sum(Ytd_sales) '销售总合'from Titlesgroup by Pub_id--15.查询得到本年度截止到目前的销售额超过 $40,000 的出版商。-----------select Pub_id,ytd_sales=sum(Ytd_sales) from Titlesgroup by Pub_idhaving sum(Ytd_sales)>40000--16.查询价格超过10美元的书的种类,以及他们的平均价格。-------------select [Type],avg(Price) '平均价格'from Titleswhere Price>10group by [Type]--17.查询价格超过10美元的书的种类,以及他们的平均价格并找出平均价格大于18美元的书的种类。select [Type],avg(Price) '平均价格'from Titleswhere Price>10group by [Type]having avg(Price)>18--18.在pubs数据库的titles表中,找出平均价格大于18美元的书的出版商id。select Pub_id,avg(Price)from Titlesgroup by Pub_idhaving avg(Price)>18--19.在pubs数据库的titles表中,找出最高价格大于20美元的书的种类 select [Type],max(Price)from Titlesgroup by [Type]having max(Price)>20--24.查询pubs数据库中,每一本书对应有几个作者。(提示:使用titles表和titleauthor表连接,并进行分组统计)select Title,count(au_id)from Titles inner join Titleauthor on Titles.Title_id=Titleauthor.Title_idgroup by Title--25.找出title_id和pub_name的对应关系。(查询每一本书编号对应的出版商)select Title_id,Pub_namefrom Titles inner join Publishers on Publishers.Pub_id=Titles.Pub_id--26.查询每个作者的编号,姓名,所出的书的编号,并对结果排序。select Titleauthor.au_id,au_lname,au_fname,Title_idfrom Titleauthor inner join Authors on Titleauthor.au_id=Authors.au_idorder by Titleauthor.au_id--27.假设在 city 列上联接 authors 表和 publishers 表。结果只显示在出版商所在城市居住的作者select au_fname,au_lname,Pub_namefrom Authors inner join Publishers on Authors.City=Publishers.City--28.从authors表中选择state,city列,从publisher表中选择state,city列,并把两个查询的结果合并为一个结果集,并对结果集按city列、state列进行排序。------------select [State],Cityfrom Authorsunion select [State],Cityfrom Publishersorder by City,[State]--29.查找所有曾出版过商业书籍的出版商的名称。select Pub_namefrom Publisherswhere Pub_id in (select Pub_id from Titles where [Type]='business')--30.查找所有曾出版过商业书籍的出版商的名称和书籍的书名。select Pub_name,Titlefrom Publishers inner join Titles on Publishers.Pub_id=Titles.Pub_idwhere [Type]='business'--31.显示所有来自CA州的作家的全部作品和作家代号。(使用IN,和连接两种方法)select titles.title,Titleauthor.au_idfrom Titleauthor inner join Authors on Authors.au_id=Titleauthor.au_id                 inner join Titles on Titleauthor.Title_id=Titles.Title_idwhere [State]='CA'select t.title,a.au_idfrom Titles t inner join Titleauthor ta on t.Title_id = ta.Title_idinner join Authors a on ta.au_id = a.au_idwhere a.au_id in(select a.au_id from Authors a where a.State='CA')--32.查找获得某本书 100 % 共享版税 (royaltyper) 的所有作者名。SELECT au_lname, au_fname FROM Authors WHERE 100 IN (SELECT Royaltyper FROM Titleauthor WHERE Titleauthor.au_ID = Authors.au_id)--33.查找不出版商业书籍的出版商的名称SELECT pub_name FROM publishers WHERE NOT EXISTS (SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type ='business') 

原创粉丝点击