cse4701inclassSQLs

advertisement
In Class SQL Queries – Part II

CSE
4701


Homework 3 from Spring 2015
Problem 6.18 from the 6th edition done in SQL and
NOT relational expressions
Problem 4.10 in 6th edition
Chapter 5-1
Homework 3 from Spr 2015

CSE
4701
Problem 6.18 from the 6th
edition done in SQL and
NOT relational expressions
Chapter 5-2
c. Retrieve the names of all borrowers who do not have
any books checked out
CSE
4701
SELECT Name
FROM BORROWER B
WHERE NOT EXIST
( SELECT *
FROM BOOK_LOANS L
WHERE B.CardNo = L.CardNo )
Chapter 5-3
e. For each library branch, retrieve the branch name and the total
number of books loaned out from that branch.
CSE
4701
SELECT L.BranchName, COUNT(*)
FROM BOOK_COPIES B, LIBRARY_BRANCH L
WHERE B.BranchId = L.BranchId
GROUP BY L.BranchName
Chapter 5-4
f. Retrieve the names, addr, and num of books checked out for all
borrowers who have more than five books checked out.
CSE
4701
SELECT B.CardNo, B.Name, B.Address, COUNT(*)
FROM BORROWER B, BOOK_LOANS L
WHERE B.CardNo = L.CardNo
GROUP BY B.CardNo
HAVING COUNT(*) > 5
Chapter 5-5
Homework 3 from Spr 2015

Problem 4.10 in 6th edition
CSE
4701
Chapter 5-6
Problem 4.10 in 6th edition
CSE
4701
Chapter 5-7
(a) Retrieve the names of employees in department 5 who
work more than 10 hours per week on the 'ProductX' project
CSE
4701
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE DNO=5 AND SSN IN
( SELECT ESSN
FROM WORKS_ON
WHERE HOURS>10 AND PNO IN
( SELECT PNUMBER
FROM PROJECT
WHERE PNAME='ProductX' ) )
LNAME
FNAME
Smith
John
English Joyce
Chapter 5-8
(e) Retrieve the names of employees who work on every project.
CSE
4701
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE NOT EXISTS
( SELECT PNUMBER
FROM PROJECT
WHERE NOT EXISTS
( SELECT *
FROM WORKS_ON
WHERE PNUMBER=PNO AND ESSN=SSN ) )
Result (empty):
Chapter 5-9
(f) Retrieve the names of employees who do not
work on any project.
CSE
4701
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE NOT EXISTS
( SELECT *
FROM WORKS_ON
WHERE ESSN=SSN )
Result (empty):
Chapter 5-10
(i) Find the names/addrs of employees who work on at least one
project in Houston but whose dept has no location in Houston.
CSE
4701
SELECT LNAME, FNAME, ADDRESS
FROM EMPLOYEE
WHERE EXISTS
( SELECT *
FROM WORKS_ON, PROJECT
WHERE SSN=ESSN AND PNO=PNUMBER
AND PLOCATION='Houston' )
AND NOT EXISTS
( SELECT *
FROM DEPT_LOCATIONS
WHERE DNO=DNUMBER
AND DLOCATION='Houston' )
Result:
LNAME
FNAME
ADDRESS
Wallace Jennifer 291 Berry, Bellaire, TX
Chapter 5-11
(j) List the last names of dept managers who have no dependents.
CSE
4701
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE EXISTS
( SELECT *
FROM DEPARTMENT
WHERE SSN=MGRSSN )
AND NOT EXISTS
( SELECT *
FROM DEPENDENT
WHERE SSN=ESSN )
Result:
LNAME FNAME
Borg James
Chapter 5-12
In Class SQL Queries – Part I
Explaining Northwind Schema


CSE
4701






Suppliers: A Suppliers Contact Info and web link.
Products: Names, suppliers, and Prices
Categories: Categories of Northwind products such as
Beverages, Condiments, Confections, Dairy Products,
Grains/Cereals, Meat/Poultry, Produce, Seafood
Orders: For each Customer with dates &Shipping
Order Details: Products, quantities, and price.
Employees: Typical Info.
Customers: Typical Info.
Shippers: Typical Info.
ANN.13
Northwind Schema
CSE
4701
ANN.14
Northwind Schema – Key Types
CSE
4701
ANN.15
Prob 1: All Products Less that 10 return all
columns.
CSE
4701
SELECT *
FROM NORTHWIND.PRODUCTS
WHERE UNITPRICE < 10;
ANN.16
Prob 2: List products that is between 10 to
20 dollars by Price in Descending order
CSE
4701
SELECT *
FROM NORTHWIND.PRODUCTS
WHERE UNITPRICE >= 10 AND UNITPRICE <= 20
ORDER BY UNITPRICE DESC;
ANN.17
Prob 3: List all the products in Beverages category,
with its ProductID, Product Name, and Price, and
CATGORYNAME by Price.
SELECT PRODUCTID, PRODUCTNAME, UNITPRICE, CATEGORYNAME
NORTHWIND.PRODUCTS, NORTHWIND.CATEGORIESWHERE
PRODUCTS.CategoryID = CATEGORIES.CategoryID AND
Categories.CategoryName="Beverages“
ORDER BY UNITPRICE;
CSE
FROM
4701
ANN.18
Prob 4: Find and print the company names and
company addresses of all Suppliers that supply the
category name Seafood.
SELECT DISTINCT suppliers.CompanyName, suppliers.Address
FROM northwind.suppliers, northwind.categories,
CSE
northwind.products
4701
WHERE suppliers.SupplierID = products.SupplierID
AND categories.CategoryID = products.CategoryID
AND categories.CategoryName = 'Seafood';
ANN.19
Prob 5: Count and print the number of suppliers
for each of the eight different categories of food
SELECT categories.CategoryName,
COUNT(suppliers.SupplierID)
CSE
FROM northwind.categories, northwind.products,
4701
northwind.suppliers
WHERE suppliers.SupplierID = products.SupplierID
AND products.categoryID = categories.CategoryID
GROUP BY categories.CategoryName;
ANN.20
In Class SQL Queries
Explaining Chinook Schema


CSE
4701




Employees and Customers are Two Main DB Users
Repository Track Artists and Their Albums
Each Album has Multiple Tracks
Tracks are of a Particular Media and Genre
Customers can Also Create Playlists of Tracks
Customers have Invoices
 Each Invoice has Multiple Lines
 Each Line References one Track
ANN.21
CSE
4701
Chinook
EER
ANN.22
https://chinookdatabase.codeplex.com/wikipage?title=Chi
nook_Schema&referringTitle=Documentation
CSE
4701
ANN.23
What Does Data Look Like?
CSE
4701
StevesSongs
PL12,
PL12,
PL12,
PL13,
T3
T5
T7
T3
T3, Hello, A1
T4, Bye, A1
T5, MySong, A1
etc.
ANN.24
Prob 6: Customers and Employees that have the same last
name and print out the Last Name (only once), Address,
City, and State for each.
SELECT employee.LastName, employee.Address,
employee.City, employee.State, customer.Address,
CSE
customer.city, customer.state
4701
FROM chinook.employee, chinook.customer
WHERE employee.LastName = customer.LastName;
ANN.25
Prob 7: Customer Names and Company Name of all
Customers that have purchased a Rock Album. (where
Name is ‘Rock’ in Genre table).
SELECT DISTINCT customer.FirstName, customer.LastName,
customer.Company
FROM chinook.customer, chinook.invoice, chinook.invoiceline,
CSE
chinook.track, chinook.genre
4701
WHERE customer.CustomerId = invoice.CustomerId
AND invoice.InvoiceId = invoiceline.InvoiceId
AND invoiceline.TrackId = track.TrackId
AND track.GenreId = genre.GenreId AND genre.name = 'rock';
ANN.26
Prob 8: Album name and tracks of all of the
albums by the composer James Hetfield, grouped
by Album.

CSE
4701

Find all of print album name and tracks of all of the albums by
the composer James Hetfield, grouped by Album
What Does Data for Track Look Like?
SELECT NAME, COMPOSER FROM CHINOOK.TRACK;
ANN.27
Prob 8: Album name and tracks of all of the
albums by the composer James Hetfield, grouped
by Album.

CSE
4701


There are other Spellings to Consider:
 J. Hetfield
There are some Hetfields without First Name or Initial
What SQL Command is Needed for Searching
Composer?
 LIKE is Used to Compare Partial Strings
 '%' (or '*') Replaces an Arbitrary # of characters
'_' replaces a single arbitrary character
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE ADDRESS LIKE '%Houston,TX% '
ANN.28
Prob 8: Album name and tracks of all of the
albums by the composer James Hetfield, grouped
by Album.
SELECT album.title, track.name
FROM chinook.album, chinook.track
CSE
WHERE album.AlbumId = track.AlbumId AND track.Composer
4701
LIKE '%Hetfield%';
ANN.29
Prob 9: customer in Canada find all invoices and for each,
print Customer Name, number of invoices for customer,
and the total amount paid for all invoices.
SELECT customer.LastName, COUNT(invoice.InvoiceId),
SUM(invoice.total)
CSE
FROM chinook.customer, chinook.invoice
4701
WHERE customer.Country = 'Canada'
AND customer.CustomerId = invoice.CustomerId
GROUP BY customer.LastName;
ANN.30
Download