Вы находитесь на странице: 1из 8

Introduction to SQL Labs

Unit 1
1. Select all of the authors
2. Select all of the publishers
3. Select all of the books
4. Create a query that will produce the following:
---------------------------------------------------------------------------------------------Johnson White lives in Menlo Park,CA
Marjorie Green lives in Oakland,CA
Cheryl Carson lives in Berkeley,CA
Michael O'Leary lives in San Jose,CA
Dean Straight lives in Oakland,CA
Meander Smith lives in Lawrence,KS
Abraham Bennet lives in Berkeley,CA
Ann Dull lives in Palo Alto,CA
Burt Gringlesby lives in Covelo,CA
Charlene Locksley lives in San Francisco,CA
Morningstar Greene lives in Nashville,TN
Reginald Blotchet-Halls lives in Corvallis,OR
Akiko Yokomoto lives in Walnut Creek,CA
Innes del Castillo lives in Ann Arbor,MI
Michel DeFrance lives in Gary,IN
Dirk Stringer lives in Oakland,CA
Stearns MacFeather lives in Oakland,CA
Livia Karsen lives in Oakland,CA
Sylvia Panteley lives in Rockville,MD
Sheryl Hunter lives in Palo Alto,CA
Heather McBadden lives in Vacaville,CA
Anne Ringer lives in Salt Lake City,UT
Albert Ringer lives in Salt Lake City,UT
(23 row(s) affected)

5. Create a query that will produce the following:


Name
------------------------------------------------------------Johnson White
Marjorie Green
Cheryl Carson
Michael O'Leary
Dean Straight
Meander Smith
Abraham Bennet
Ann Dull
Burt Gringlesby
Charlene Locksley
Morningstar Greene
Reginald Blotchet-Halls
Akiko Yokomoto
Innes del Castillo
Michel DeFrance
Dirk Stringer
Stearns MacFeather
Livia Karsen
Sylvia Panteley
Sheryl Hunter
Heather McBadden
Anne Ringer
Albert Ringer
(23 row(s) affected)

1.
2.
3.
4.
5.

Unit 2
Retrieve all of the authors who live in California
Retrieve all of the authors who live in either California or Michigan
Retrieve all of the authors with a first name of Dean that live in California and all authors who live in Kansas
Give two methods for retrieving all of the authors with a last name between L and S, but not including those
beginning with S.
Retrive all people with a last name of Smith or any derivative

Unit 3
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

Select all of the titles that do not have a price


Select all titles sorted by type
Select the type and price of each book
Now remove all of the duplicate rows in the result set from #4
Select the number of rows in the titles table
Select the total year to date sales of all titles
Select the average price of a book
Select the number of each type of book
Select the total of the year to date sales for each type of book
Select the average price for each type of book
Select the types of books that had an average price > $13.50
Select the title_id, type, and price from the titles table along with an average price for all tables
Select the title_id, type, and price from the titles table with an average price for each type of book and an
average for the entire table
14. Select all of the author first names and all of the employee first names into a single result set

Unit 4
1. Using both a join and a subquery, select the publishers that publish business books.
2. Using both a join and a subquery, select the number of authors that wrote business books.
3. Select a price list as shown below
Publisher
Title
Price
------------------------- -------------------------------------------------------------------------------New Moon Books
You Can Combat Computer Stress!
$ 2.99
New Moon Books
Is Anger the Enemy?
$ 10.95
New Moon Books
Life Without Fear
$ 7.00
New Moon Books
Prolonged Data Deprivation: Four Case Studies
New Moon Books
Emotional Security: A New Algorithm
$ 7.99
Binnet & Hardley
Silicon Valley Gastronomic Treats
$ 19.99
Binnet & Hardley
The Gourmet Microwave
$ 2.99
Binnet & Hardley
The Psychology of Computer Cooking
$ (null)
Binnet & Hardley
Computer Phobic AND Non-Phobic Individuals: Behavior Variations
Binnet & Hardley
Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean
Binnet & Hardley
Fifty Years in Buckingham Palace Kitchens
$ 11.95
Binnet & Hardley
Sushi, Anyone?
$ 14.99
Algodata Infosystems The Busy Executive's Database Guide
$ 19.99
Algodata Infosystems Cooking with Computers: Surreptitious Balance Sheets
Algodata Infosystems Straight Talk About Computers
$ 19.99
Algodata Infosystems But Is It User Friendly?
$ 22.95
Algodata Infosystems Secrets of Silicon Valley
$ 20.00
Algodata Infosystems Net Etiquette
$ (null)
(18 row(s) affected)
4.
5.

Select all of the titles published by Algodata Infosystems


Using an exists, find all of the publishers that publish business books

- --------

$ 19.99

$ 21.59
$ 20.95

$ 11.95

Unit 5
1.

Insert the following data into the authors table

au_id
123-45-6789
123-45-6788
123-45-6787

au_lname
Jordan
Jonson
Miller

au_fname
Gregory
Michael
Jim

comtract
1
0

2. You have found that some of the data is incorrect, correct this data as noted below
au_id
au_lname
au_fname
comtract
123-45-6788
Johnson
Michael
0
123-45-6787
Miller
James
1
3.

You find out that Gregory Jordan died yeaterday and the publisher is pulling all of his books. So, you must
delete him from the authors table.
4. Delete all of the authors

Unit 1 solutions
1.
2.
3.
4.
5.

select * from authors


select * from publishers
select * from titles
select au_fname + ' ' + au_lname + ' lives in ' + city + ',' + state from authors
select au_fname + ' ' + au_lname Name from authors
Unit 2 Solutions

1.
2.
3.
4.
5.

select * from authors where state = CA


select * from authors where state = CA or state = MI
select * from authors where state in (CA,MI)
select * from authors where (au_fname = Dean and state = CA) or state = KS
select * from authors where au_lname >= L and au_lname <= S
select * from authors where au_lname between L and S
select * from authors where au_lname like Sm_th%
Unit 3 Solutions

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

select * from titles where price is null


select title,type from titles order by type
select type, price from titles
select distinct type, price from titles
select count(*) from titles
select sum(ytd_sales) from titles
select avg(price) from titles
select type, count(*) from titles group by type
select type, sum(ytd_sales) from titles group by type
select type, avg(price) from titles group by type
select type, avg(price) from titles group by type having avg(price) > $13.50
select title_id,type,price from titles where type like '%cook%' compute avg(price)
select title_id, type, price from titles where type like '%cook%'
order by type compute avg(price) by type
14. select au_fname from authors union select fname from employee
Unit 4 Solutions
1. select distinct pub_name from publishers, titles where publishers.pub_id = titles.pub_id
and titles.type = 'business'
select pub_name from publishers where pub_id in (select pub_id from titles where type = 'business')
2. select count(*) from authors, titleauthor, titles where authors.au_id = titleauthor.au_id and
titleauthor.title_id = titles.title_id and titles.type = 'business'
select count(*) from authors where au_id in (select au_id from titleauthor where title_id in
(select title_id from titles where type = 'business'))
3. select pub_name Publisher, title Title, '$' , price Price from publishers, titles
where publishers.pub_id = titles.pub_id
4. select title from titles, publishers where titles.pub_id = publishers.pub_id and
publishers.pub_name = 'Algodata Infosystems'
6. select distinct pub_name from publishers where exists (select 1 from titles where pub_id = publishers.pub_id
and type = 'business')

Unit 5 Solutions
1.

insert into authors (au_id, au_lname, au_fname, contract)


values(123-45-6789, Jordan,Gregory,1)
insert into authors (au_id, au_lname, au_fname, contract)
values(123-45-6788, Jonson,Michael,0)
insert into authors (au_id, au_lname, au_fname)
values(123-45-6787, Miller,Jim)
2. update authors set au_lname = Johnson where au_id = 123-45-6788
update authors set au_fname = James where au_id = 123-45-6787
3. delete from authors where au_fname = Gregory and au_lname = Jordan
4. delete from authors

Вам также может понравиться