SQL--PUBS
?
在示例數據庫Pubs中執行數據查詢,要求如下:
1使用內聯接查詢出authors和publishers表中位于同一個城市的作者和出版社信息。
2 查詢出作者號以1~5開頭的所有作者,并使用右外聯接在查詢的結果集中列出和作者在同一個城市的出版社名。(authors是作者表,publishers是出版社表)
3 使用自聯接查找居住在 Oakland 相同郵碼區域中的作者。(zip是郵碼)
4 請查詢所有員工的編號、姓、名以及他們所從事工作的名稱。(jobs表是員工職位表,其中job_desc為職位描述,即職位名稱)
5 請查詢出與作者在同一個州的書店的名稱、地址和城市。(stores表為書店表,其中stor_name是書店名稱,stor_address是地址,city是城市)
6 請查詢出所銷售書籍的名稱、總銷售數量以及總銷售額。(qty是銷售數量, 銷售額 =單價price * 銷售數量qty)
7 請查詢出書籍的名稱、類型、價格、撰寫這本書的作者的姓、名、聯系地址、電話以及出版該書的出版社名稱。(authors是作者表,publishers是出版社表)
1.
? select au_fname,address,pub_name,publishers.city
? from authors inner join publishers
? on authors.city=publishers.city
2.
? select au_id,au_fname,pub_name,publishers.city
? from authors right join publishers
? on authors.au_id like'[1-5]%' and authors.city=publishers.city
3
select a.au_fname,a.zip
from authors as a inner join authors as b
on (a.city='Oakland' and a.zip=b.zip)
4.
? select a.emp_id,a.fname,b.job_desc
? from? employee as a inner join jobs as b
? on? a.job_id=b.job_id
5.
? select? distinct a.stor_name,a.stor_address,a.city,a.state
? from stores as a inner join authors as b
? on a.state=b.state
6.(取樣匯總,可用建新表(臨時表)的方法)
? select a.title,a.price,b.qty into #newtable
? from titles as a right join sales as b
? on a.title_id=b.title_id
? select *from #newtable
? select title as '姓名',sum(price*qty) as '總銷售金額',sum(qty) as '總銷售數量'
? from #newtable
? group by title
7.(多表聯接,inner join on 和where 表達式可以達到同樣的效果)
??
? select *from titles
? select *from authors
? select *from titleauthor
? select *from publishers
(1 )
?select t.title,t.type,t.price,a.au_lname,a.au_fname,a.address,a.phone,p.pub_name
? from titles as t inner join titleauthor as b
?? on? t.title_id=b.title_id
?? inner join authors as a
?? on a.au_id=b.au_id
?? inner join publishers as p
?? on t.pub_id=p.pub_id
(2)
? select c.title,c.type,c.price,
?????? a.au_lname,a.au_fname,a.address,a.phone,
?????? d.pub_name
? from authors a ,titleauthor b,
???? titles? c, publishers d
? where a.au_id = b.au_id
?? and b.title_id = c.title_id
?? and c.pub_id = d.pub_id
?
?
?
?
11.?查詢Titles表,返回所有版稅royalty列非空的數據行;
12.?查詢Titles表,返回預付款advance列值大于7000的行數;
13.?查詢Titles表,按照出版物類型type列進行分組,顯示每一組中type值、Price的平均值;
14.?查詢TitleAuthor表中,按照Title_ID進行分組查詢,并顯示每一組中版權費(royaltyper)的最大值;
15.?查詢作者表,限制條件的要求是:au_id的值必須不以”8”開頭,但必須包含字符”8”;
16.?查詢Sales表,要求返回定單日期(ord_date)在1993年到1994年之間,查詢結果按照title_id降序的方式進行顯示;
17.?查詢Sales表,只返回前40%的行;
18.?編寫一個查詢,找出現有圖書的各個類別(不能有重復值)
19.?編寫一個查詢,找出各個作者所著圖書的數量(提示:作者所著圖書在titleauthor表)
20.?編寫一個查詢,顯示各個作者的版權費(royaltyper)的總和
21.?編寫一個查詢,顯示sales表中各本書(title_id 書的編號)的數量(qty)的平均值
11.
?? select *from titles
?? where royalty is not null
12.
?? select count(0)
?? from?? titles
?? where advance>7000
13.
?? select type,avg(price)
?? from?? titles
?? group by type
14.
?? select max(royaltyper) as 最大值,title_id
?? from? titleauthor
?? group by title_id
15.
??
?? select au_id from authors
?? where au_id like '[^8]%[8]%'
16.
?? select *from sales
?? where ord_date between '1993'and'1994'
?? order by title_id desc
17.
?? select top 40 percent *????? /*注意percent的寫法*/
?? from sales
18.
?? select distinct type as 類別 from titles? /*distinct關鍵字可從select查詢中去掉重復的行 */
?? group by type
19.
?? select au_id,count(title_id)?? /*也可以是count(0)*/
?? from?? titleauthor
?? group by au_id
20.
?? select au_id,sum(royaltyper)
?? from? titleauthor
?? group by au_id
21.
?? select title_id,avg(qty) as 平均值
?? from sales
?? group by title_id
?
總結
- 上一篇: php 让浏览器格式化显示XML
- 下一篇: linux cmake编译源码,linu