Upload
jasmine-woods
View
245
Download
2
Embed Size (px)
Citation preview
SQLDML.1
CSE4701
Other SQL Query Examples
SQLDML.2
CSE4701
Homework 3 from Spr 2015 Problem 6.18 from the 6th
edition done in SQL and NOT relational expressions
SQLDML.3
CSE4701
Problem 6.18 in 6th edition
a. How many copies of the book titled The Lost Tribe are owned by the library branch whose name is ‘Sharpstown’?
SELECT NoOfCopiesFROM ( (BOOK NATURAL JOIN BOOK_COPIES )
NATURAL JOIN LIBRARY_BRANCH )WHERE Title='The Lost Tribe' AND BranchName='Sharpstown’
Ans = No_Of_Copies( (BranchName=‘Sharpstown’ (LIBRARY-BRANCH)) * BaB)
BranchID
BaB= (BOOKCOPIES *(Title=‘The Lost Tribe’
(BOOK))) )
BookId
SQLDML.4
CSE4701
Problem 6.18 in 6th edition
b. How many copies of the book titled The Lost Tribe are owned by eachlibrary branch?
SELECT BranchName, NoOfCopiesFROM ( (BOOK NATURAL JOIN BOOK_COPIES )
NATURAL JOIN LIBRARY_BRANCH )WHERE Title='The Lost Tribe'
Ans = BranchName, No_Of_Copies( (Title=‘The Lost Tribe’ (BOOK)) * CaB)
BookId
CaB = BOOKCOPIES * LIBRARY_BRANCH)BranchId
SQLDML.5
CSE4701
Problem 6.18 in 6th edition
c. Retrieve the names of all borrowers who do not have any books checked out
SELECT NameFROM BORROWER BWHERE NOT EXIST ( SELECT *
FROM BOOK_LOANS LWHERE B.CardNo = L.CardNo )
d. For each book that is loaned out from the Sharpstown branch and whoseDue_date is today, retrieve the book title, the borrower’s name, and theborrower’s address.
SELECT B.Title, R.Name, R.AddressFROM BOOK B, BORROWER R, BOOK_LOANS BL, LIBRARY_BRANCH LBWHERE LB.BranchName='Sharpstown' AND LB.BranchId=BL.BranchId AND BL.DueDate='today' AND BL.CardNo=R.CardNo AND BL.BookId=B.BookId
SQLDML.6
CSE4701
Problem 8.11/6.18 in 6th edtione. For each library branch, retrieve the branch name and the total numberof books loaned out from that branch.SELECT L.BranchName, COUNT(*)FROM BOOK_COPIES B, LIBRARY_BRANCH LWHERE B.BranchId = L.BranchIdGROUP BY L.BranchNamef. Retrieve the names, addresses, and number of books checked out for allborrowers who have more than five books checked out.SELECT B.CardNo, B.Name, B.Address, COUNT(*)FROM BORROWER B, BOOK_LOANS LWHERE B.CardNo = L.CardNoGROUP BY B.CardNoHAVING COUNT(*) > 5g. For each book authored (or coauthored) by Stephen King, retrieve thetitle and the number of copies owned by the library branch whose nameis Central.SELECT TItle, NoOfCopiesFROM ( ( (BOOK_AUTHORS NATURAL JOIN BOOK)
NATURAL JOIN BOOK_COPIES)NATURAL JOIN LIBRARY_BRANCH)
WHERE Author_Name = 'Stephen King' and BranchName = 'Central'
SQLDML.7
CSE4701
Homework 3 from Spr 2015 Problem 4.10 in 6th edition
SQLDML.8
CSE4701
Problem 4.10 in 6th edition
SQLDML.9
CSE4701
Problem 4.10 in 6th edition
(a) Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project.
SELECT LNAME, FNAMEFROM EMPLOYEE, WORKS_ON, PROJECTWHERE DNO=5 AND SSN=ESSN AND PNO=PNUMBER AND PNAME='ProductX' AND HOURS>10
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE DNO=5 AND SSN IN
( SELECT ESSNFROM WORKS_ONWHERE HOURS>10 AND PNO IN
( SELECT PNUMBERFROM PROJECTWHERE PNAME='ProductX' ) )
LNAME FNAMESmith JohnEnglish Joyce
SQLDML.10
CSE4701
Problem 4.10 in 6th edition(b) List the names of employees who have a dependent with the same first name as themselves.
SELECT LNAME, FNAMEFROM EMPLOYEE, DEPENDENTWHERE SSN=ESSN AND FNAME=DEPENDENT_NAME
Another possible SQL query uses nesting as follows:
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE EXISTS
( SELECT *FROM DEPENDENTWHERE FNAME=DEPENDENT_NAME
AND SSN=ESSN )Result (empty):
SQLDML.11
CSE4701
Problem 4.10 in 6th edition
(c) Find the names of employees that are directly supervised by 'Franklin Wong'.
SELECT E.LNAME, E.FNAMEFROM EMPLOYEE E, EMPLOYEE SWHERE S.FNAME='Franklin' AND
S.LNAME='Wong' AND E.SUPERSSN=S.SSN
Another possible SQL query uses nesting as follows:
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE SUPERSSN IN
( SELECT SSNFROM EMPLOYEEWHERE FNAME='Franklin' AND LNAME='Wong' )
LNAME FNAMESmith JohnNarayan RameshEnglish Joyce
SQLDML.12
CSE4701
Problem 4.10 in 6th edition
(d) For each project, list the project name and the total hours per week (by all employees) spent on that project.
SELECT PNAME, SUM (HOURS)FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNAME
Result:PNAME SUM(HOURS)ProductX 52.5ProductY 37.5ProductZ 50.0Computerization55.0Reorganization 25.0Newbenefits 55.0
SQLDML.13
CSE4701
Problem 4.10 in 6th edition
(e) Retrieve the names of employees who work on every project.
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE NOT EXISTS
( SELECT PNUMBERFROM PROJECTWHERE NOT EXISTS
( SELECT *FROM WORKS_ONWHERE PNUMBER=PNO AND ESSN=SSN ) )
Result (empty):
SQLDML.14
CSE4701
Problem 4.10 in 6th edition
(f) Retrieve the names of employees who do not work on any project.
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE NOT EXISTS
( SELECT *FROM WORKS_ONWHERE ESSN=SSN )
Result (empty):
SQLDML.15
CSE4701
Problem 4.10 in 6th edition
(g) For each department, retrieve the department name, and the average salary of employees working in that department.
SELECT DNAME, AVG (SALARY)FROM DEPARTMENT, EMPLOYEEWHERE DNUMBER=DNOGROUP BY DNAME
Result:DNAME AVG(SALARY)Research 33250Administration 31000Headquarters 55000
SQLDML.16
CSE4701
Problem 4.10 in 6th edition
(h) Retrieve the average salary of all female employees.
SELECT AVG (SALARY)FROM EMPLOYEEWHERE SEX='F’
Result:AVG(SALARY)31000
SQLDML.17
CSE4701
Problem 4.10 in 6th edition
(i) Find the names and addresses of employees who work on at least one project located in Houston but whose department has no location in Houston.
SELECT LNAME, FNAME, ADDRESSFROM EMPLOYEEWHERE EXISTS
( SELECT *FROM WORKS_ON, PROJECTWHERE SSN=ESSN AND PNO=PNUMBER
AND PLOCATION='Houston' ) AND NOT EXISTS
( SELECT *FROM DEPT_LOCATIONSWHERE DNO=DNUMBER
AND DLOCATION='Houston' )Result:LNAME FNAME ADDRESSWallace Jennifer 291 Berry, Bellaire, TX
SQLDML.18
CSE4701
Problem 4.10 in 6th edition
(j) List the last names of department managers who have no dependents.
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE EXISTS
( SELECT *FROM DEPARTMENTWHERE SSN=MGRSSN )
AND NOT EXISTS ( SELECT *FROM DEPENDENTWHERE SSN=ESSN )
Result:LNAME FNAMEBorg James
SQLDML.19
CSE4701
Homework 4 from Spring 2015Problem 3.3 for the Chinook Database Schemaa. Find the list of all Customers and Employees that have the
same last name and print out the Last Name (only once), Address, City, and State for each.
b. Find and print the Customer Names (First and Last) and Company Name of all Customers that have purchased a Rock Album. (where the Name is ‘Rock’ in the Genre table).
c. Find all of print album name and tracks of all of the albums by the composer James Hetfield, grouped by Album.
d. For each customer that lives in Canada (CA- Country attribute of Customer), find all invoices and for each result, and print Customer Last Name, number of invoices for customer, and the total amount paid for all invoices.
SQLDML.20
CSE4701
Explaining Chinook Schema 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
SQLDML.21
CSE4701
Chinook EER
SQLDML.22
CSE4701
https://chinookdatabase.codeplex.com/wikipage?title=Chinook_Schema&referringTitle=Documentation
SQLDML.23
CSE4701
What Does Data Look Like?
StevesSongs PL12, T3 T3, Hello, A1 PL12, T5 T4, Bye, A1 PL12, T7 T5, MySong, A1
PL13, T3 etc.
SQLDML.24
CSE4701
Problem 3.3a
SELECT employee.LastName, employee.Address, employee.City, employee.State, customer.Address, customer.city, customer.state
FROM chinook.employee, chinook.customer WHERE employee.LastName = customer.LastName;
SQLDML.25
CSE4701
Problem 3.3b
SELECT DISTINCT customer.FirstName, customer.LastName, customer.CompanyFROM chinook.customer, chinook.invoice, chinook.invoiceline,
chinook.track, chinook.genreWHERE customer.CustomerId = invoice.CustomerId
AND invoice.InvoiceId = invoiceline.InvoiceId AND invoiceline.TrackId = track.TrackId AND track.GenreId = genre.GenreId AND genre.name = 'rock';
SQLDML.26
CSE4701
Problem 3.3c 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;
SQLDML.27
CSE4701
Problem 3.3c
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, LNAMEFROM EMPLOYEEWHERE ADDRESS LIKE '%Houston,TX% '
SQLDML.28
CSE4701
Problem 3.3c
SELECT album.title, track.nameFROM chinook.album, chinook.trackWHERE album.AlbumId = track.AlbumId AND track.Composer LIKE '%Hetfield%';
SQLDML.29
CSE4701
Problem 3.3d.
SELECT customer.LastName, COUNT(invoice.InvoiceId), SUM(invoice.total)FROM chinook.customer, chinook.invoiceWHERE customer.Country = 'Canada'
AND customer.CustomerId = invoice.CustomerIdGROUP BY customer.LastName;
SQLDML.30
CSE4701
Homework 4 from Spring 2015Problem 3.4 for the Northwind Database Schemaa. Find and print the company names and company addresses of
all Suppliers that supply the category name Seafood.b. Count and print the number of suppliers for each of the eight
different categories of food which by name are: Beverages, Condiments, Confections, Dairy Products, Grains/Cereals, Meat/Poultry, Produce, Seafood.
c. For each country (ShipCountry in Orders), total the Freight charges. The countries are: France, Germany, Brazil, Belgium, Switzerland, Venezuela, Austria, Mexico, USA, Sweden, Finland, Italy, Spain, UK, Ireland, Portugal, Canada, Denmark, Poland, Norway, Argentina
SQLDML.31
CSE4701
Explaining Northwind Schema 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.
SQLDML.32
CSE4701
Northwind Schema
SQLDML.33
CSE4701
Northwind Schema – Key Types
SQLDML.34
CSE4701
Problem 3.4a
SELECT * FROM NORTHWIND.PRODUCTS WHERE UNITPRICE >= 10 AND UNITPRICE <= 20ORDER BY UNITPRICE DESC;
SQLDML.35
CSE4701
Problem 3.4b
SELECT categories.CategoryName, COUNT(suppliers.SupplierID) FROM northwind.categories, northwind.products, northwind.suppliers WHERE suppliers.SupplierID = products.SupplierID
AND products.categoryID = categories.CategoryID GROUP BY categories.CategoryName;
SQLDML.36
CSE4701
Problem 3.4c
SELECT * FROM NORTHWIND.PRODUCTS WHERE UNITPRICE >= 10 AND UNITPRICE <= 20ORDER BY UNITPRICE DESC;