HBC-chap03 - unix.eng.ua.edu

advertisement
Michael S McDaniel
CS 472 – Henry Books Case Chapter Three
1. SQL Query: Author Number and Last Name for every author:
SELECT AuthorLast, AuthorFirst
FROM Author;
01-AuthorQuery
AuthorLast
Morrison
Solotaroff
Vintage
Francis
Straub
King
Pratt
Chase
Collins
Heller
Wills
Hofstadter
Lee
Ambrose
Rowling
Salinger
Heaney
Camus
Collins, Jr.
Steinbeck
Castelman
Owen
O'Rourke
Kidder
Schleining
AuthorFirst
Toni
Paul
Vernor
Dick
Peter
Stephen
Philip
Truddi
Bradley
Joseph
Gary
Douglas R.
Harper
Stephen E.
J.K.
J.D.
Seamus
Albert
Bradley
John
Riva
Barbara
Randy
Tracy
Lon
1
2. SQL Query: Complete Branch Table:
SELECT *
FROM Branch;
02-BranchQuery
BranchNum
1
2
3
4
BranchName
Henry Downtown
Henry On The Hill
Henry Brentwood
Henry Eastshore
BranchLocation NumEmployees
16 Riverview
10
1289 Bedford
6
Brentwood Mall
15
Eastshore Mall
9
3. SQL Query: Every publisher in Boston:
SELECT PublisherName
FROM Publisher
WHERE City=”Boston”;
03-BostonPublishers
PublisherName
City
Berkley Publishing Boston
Course Technology Boston
2
4. SQL Query: Every publisher not located in Boston:
SELECT PublisherName
FROM Publisher
WHERE City<>"Boston";
04-NonBostonPublishers
PublisherName
Arkham House
Arcade Publishing
Basic Books
Back Bay Books
Fawcett Books
Farrar Straus & Giroux
HarperCollins Publishers
Jove Publications
Jeremy P. Tarcher
Lb Books
McPherson and Co.
Penguin USA
Plume
Putnam Publishing Group
Random House
Schoken Books
Scribner
Simon & Schuster
Scholastic Trade
Taunton Press
Tor Books
Thames and Hudson
Touchstone Books
Vintage Books
W.W. Norton
Westview Press
City
Sauk City WI
New York
Boulder CO
New York
New York
New York
New York
New York
Los Angeles
New York
Kingston
New York
New York
New York
New York
New York
New York
New York
New York
Newtown CT
New York
New York
Westport CT
New York
New York
Boulder CO
3
5. SQL Query: Every Branch that has at least nine employees:
SELECT BranchName, NumEmployees
FROM Branch
WHERE NumEmployees>=9;
05-BranchWith9Employ
BranchName NumEmployees
Henry Eastshore
9
Henry Downtown
10
Henry Brentwood
15
6. SQL Query: Book code and Title of every book type SFI:
SELECT BookCode, Title
FROM Book
WHERE Type="SFI";
06-BooksTypeSFI
BookCode
Title
0180
A Deepness in the Sky
2226
Harry Potter and the Prisoner of Azkaban
7443
Harry Potter and the Goblet of Fire
7.
Type
SFI
SFI
SFI
SQL Query: Book Code and Title of every book type SFI & Paperback:
SELECT BookCode, Title
From Book
WHERE Type="SFI" AND Paperback=Yes;
07-BooksTypeSFIandPaperback
BookCode
Title
Type Paperback
0180
A Deepness in the Sky SFI
Yes
4
8.
SQL Query: Book Code and Title of every book type SFI OR publisher PE:
SELECT BookCode, Title
FROM Book
WHERE Type="SFI" OR PublisherCode IN
( SELECT PublisherCode
FROM Publisher
WHERE PublisherCode="PE");
08-TypeSFIorPubPE
BookCode
0180
2226
2766
5163
7405
7443
9701
Title
A Deepness in the Sky
Harry Potter and the Prisoner of Azkaban
Of Mice and Men
Travels with Charley
East of Eden
Harry Potter and the Goblet of Fire
The Grapes of Wrath
Type
SFI
SFI
FIC
TRA
FIC
SFI
FIC
PublisherCode
TB
ST
PE
PE
PE
ST
PE
9. SQL Query: Book Code, Title, and Price for each book with a price that is greater
than $5 but less than $10:
SELECT BookCode, Title, Price
FROM Book
WHERE Price>5 AND Price<10;
09-BooksBetween5and10dollars
BookCode
0180
0189
0200
0808
2766
3743
5163
6328
6908
7559
8720
9882
9883
Title
A Deepness in the Sky
Magic Terror
The Stranger
The Edge
Of Mice and Men
Nine Stories
Travels with Charley
Band of Brothers
Franny and Zooey
The Fall
When Rabbit Howls
Slay Ride
The Catcher in the Rye
Price
7.19
7.99
8.00
6.99
6.95
5.99
7.95
9.60
5.99
8.00
6.29
6.99
5.99
5
10. SQL Query: Book Code, Title of every book that has the type FIC, price less than
$10:
SELECT BookCode, Title
FROM Book
WHERE Type="FIC" AND Price <10;
10-TypeFICandPriceLT10
BookCode
0200
2766
3743
6908
7559
9883
Title
The Stranger
Of Mice and Men
Nine Stories
Franny and Zooey
The Fall
The Catcher in the Rye
Type
FIC
FIC
FIC
FIC
FIC
FIC
Price
8.00
6.95
5.99
5.99
8.00
5.99
6
11. (SQL Query, Calculated Field): Add 15% discount (85% of the current price),
show Book Code, Title, and Discounted price of every book.
SELECT BookCode, Title, (Price*0.85) AS DiscountPrice
FROM Book;
11-DiscountPrices
BookCode
0180
0189
0200
0378
079X
0808
1351
1382
138X
2226
2281
2766
2908
3350
3743
3906
5163
5790
6128
6328
669X
6908
7405
7443
7559
8092
8720
9611
9627
9701
9882
9883
9931
Title
DiscountPrice
A Deepness in the Sky
$6.11
Magic Terror
$6.79
The Stranger
$6.80
Venice
$20.83
Second Wind
$21.21
The Edge
$5.94
Dreamcatcher: A Novel
$16.66
Treasure Chests
$20.79
Beloved
$11.01
Harry Potter and the Prisoner of Azkaban
$11.87
Van Gogh and Gauguin
$17.85
Of Mice and Men
$5.91
Electric Light
$11.90
Group: Six People in Search of a Life
$8.84
Nine Stories
$5.09
The Soul of a New Machine
$9.49
Travels with Charley
$6.76
Catch-22
$10.20
Jazz
$11.01
Band of Brothers
$8.16
A Guide to SQL
$32.26
Franny and Zooey
$5.09
East of Eden
$11.01
Harry Potter and the Goblet of Fire
$15.44
The Fall
$6.80
Godel, Escher, Bach
$11.90
When Rabbit Howls
$5.35
Black House
$15.99
Song of Solomon
$11.90
The Grapes of Wrath
$11.05
Slay Ride
$5.94
The Catcher in the Rye
$5.09
To Kill a Mockingbird
$15.30
7
12. SQL Query: Book code and Title of every book that has the type SFI, HOR, or
ART.
SELECT BookCode, Title, Type
FROM Book
WHERE Type="SFI" Or Type="HOR" Or Type="ART";
12-TypeHORARTorSFI
BookCode
0180
0189
0378
1351
1382
2226
2281
7443
9611
Title
A Deepness in the Sky
Magic Terror
Venice
Dreamcatcher: A Novel
Treasure Chests
Harry Potter and the Prisoner of Azkaban
Van Gogh and Gauguin
Harry Potter and the Goblet of Fire
Black House
Type
SFI
HOR
ART
HOR
ART
SFI
ART
SFI
HOR
8
13. SQL Query: List Book Code, Title, and Publisher code for all books. Sort the
results by title within the publisher code.
SELECT BookCode, Title, PublisherCode
FROM Book
ORDER BY PublisherCode, Title;
13
BookCode
8092
3350
3906
669X
0189
2908
9931
9882
0808
8720
6908
3743
9883
7405
2766
9701
5163
138X
6128
9627
079X
9611
5790
1351
0378
7443
2226
1382
0180
6328
7559
0200
2281
Title
Godel, Escher, Bach
Group: Six People in Search of a Life
The Soul of a New Machine
A Guide to SQL
Magic Terror
Electric Light
To Kill a Mockingbird
Slay Ride
The Edge
When Rabbit Howls
Franny and Zooey
Nine Stories
The Catcher in the Rye
East of Eden
Of Mice and Men
The Grapes of Wrath
Travels with Charley
Beloved
Jazz
Song of Solomon
Second Wind
Black House
Catch-22
Dreamcatcher: A Novel
Venice
Harry Potter and the Goblet of Fire
Harry Potter and the Prisoner of Azkaban
Treasure Chests
A Deepness in the Sky
Band of Brothers
The Fall
The Stranger
Van Gogh and Gauguin
PublisherCode
BA
BP
BY
CT
FA
FS
HC
JP
JP
JP
LB
LB
LB
PE
PE
PE
PE
PL
PL
PL
PU
RH
SC
SC
SS
ST
ST
TA
TB
TO
VB
VB
WP
9
14. SQL Query (Aggregate Function): How many books have the type SFI?
SELECT COUNT(*) AS NumBooksTypeSFI
FROM Book
WHERE Type="SFI";
14-NumBooksTypeSFI
CountofTypeSFI
3
15. SQL Query (Grouping Aggregate Function): Calculate Average price for each
Type of book:
SELECT Type, AVG(Price) AS AvgOfPrice
FROM Book
GROUP BY Type;
15-AvgPricebyBookType
Type
ART
CMP
FIC
HIS
HOR
MYS
PHI
POE
PSY
SCI
SFI
TRA
AvgOfPrice
$23.32
$37.95
$10.52
$9.60
$15.47
$12.98
$14.00
$14.00
$8.35
$11.16
$13.10
$7.95
10
16. SQL Query: every book, list the book code, book title, publisher code, and
publisher name:
SELECT BookCode, Title, Publisher.PublisherCode,
Publisher.PublisherName
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode;
15-BookAndPublisher
BookCode
0180
0189
0200
0378
079X
0808
1351
1382
138X
2226
2281
2766
2908
3350
3743
3906
5163
5790
6128
6328
669X
6908
7405
7443
7559
8092
8720
9611
9627
9701
9882
9883
9931
Title
A Deepness in the Sky
Magic Terror
The Stranger
Venice
Second Wind
The Edge
Dreamcatcher: A Novel
Treasure Chests
Beloved
Harry Potter and the Prisoner of Azkaban
Van Gogh and Gauguin
Of Mice and Men
Electric Light
Group: Six People in Search of a Life
Nine Stories
The Soul of a New Machine
Travels with Charley
Catch-22
Jazz
Band of Brothers
A Guide to SQL
Franny and Zooey
East of Eden
Harry Potter and the Goblet of Fire
The Fall
Godel, Escher, Bach
When Rabbit Howls
Black House
Song of Solomon
The Grapes of Wrath
Slay Ride
The Catcher in the Rye
To Kill a Mockingbird
PublisherCode
TB
FA
VB
SS
PU
JP
SC
TA
PL
ST
WP
PE
FS
BP
LB
BY
PE
SC
PL
TO
CT
LB
PE
ST
VB
BA
JP
RH
PL
PE
JP
LB
HC
PublisherName
Tor Books
Fawcett Books
Vintage Books
Simon & Schuster
Putnam Publishing Group
Jove Publications
Scribner
Taunton Press
Plume
Scholastic Trade
Westview Press
Penguin USA
Farrar Straus & Giroux
Berkley Publishing
Lb Books
Back Bay Books
Penguin USA
Scribner
Plume
Touchstone Books
Course Technology
Lb Books
Penguin USA
Scholastic Trade
Vintage Books
Basic Books
Jove Publications
Random House
Plume
Penguin USA
Jove Publications
Lb Books
HarperCollins Publishers
11
17. SQL Query: List the book title and book price for every book with publisher name
Taunton Press:
SELECT Title, Price, Publisher.PublisherName
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode
AND Publisher.PublisherName="Taunton Press";
17-BooksByTauntonPress
Title
Treasure Chests
Price
$24.46
18. SQL Query: Book Title and Book Code for every book with publisher Putnum
Publishing Group and has price greater than $15.00:
SELECT Title, BookCode
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode
AND Publisher.PublisherName="Putnam Publishing Group";
18-PutnamPublishGrp-GT15
Title
Second Wind
BookCode
079X
12
19. (SQL-Query to create Table): Create a new table named Fiction using the data in
the BookCode, Title, PublisherCode, and Price columns in the Book table for those
books that have the type FIC.
SELECT Book.BookCode, Book.Title, Publisher.PublisherCode,
Book.Price
INTO FictionTable FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode
AND Book.Type="FIC";
19-Fiction
BookCode
0200
138X
2766
3743
5790
6128
6908
7405
7559
9627
9701
9883
9931
Title
The Stranger
Beloved
Of Mice and Men
Nine Stories
Catch-22
Jazz
Franny and Zooey
East of Eden
The Fall
Song of Solomon
The Grapes of Wrath
The Catcher in the Rye
To Kill a Mockingbird
PublisherCode
VB
PL
PE
LB
SC
PL
LB
PE
VB
PL
PE
LB
HC
Price
$8.00
$12.95
$6.95
$5.99
$12.00
$12.95
$5.99
$12.95
$8.00
$14.00
$13.00
$5.99
$18.00
Type
FIC
FIC
FIC
FIC
FIC
FIC
FIC
FIC
FIC
FIC
FIC
FIC
FIC
20. (SQL-Update Query): Use an update query to change the price of any book in the
Fiction table with a current price of 14.00 to 14.50.
UPDATE Book
SET Price=14.5 WHERE Price=14;
21. (SQL-Remove Query): Use a delete query to delete all books in the Fiction Table
that have the publisher code VB:
DELETE
FROM 19-Fiction
WHERE PublisherCode=”VB”;
13
Download