SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015 Problem 6.18...

36
SQLDML.1 CSE 4701 Other SQL Query Examples

Transcript of SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015 Problem 6.18...

Page 1: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.1

CSE4701

Other SQL Query Examples

Page 2: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.2

CSE4701

Homework 3 from Spr 2015 Problem 6.18 from the 6th

edition done in SQL and NOT relational expressions

Page 3: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 4: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 5: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 6: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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'

Page 7: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.7

CSE4701

Homework 3 from Spr 2015 Problem 4.10 in 6th edition

Page 8: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.8

CSE4701

Problem 4.10 in 6th edition

Page 9: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 10: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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):

Page 11: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 12: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 13: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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):

Page 14: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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):

Page 15: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 16: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 17: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 18: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 19: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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.

Page 20: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 21: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.21

CSE4701

Chinook EER

Page 23: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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.

Page 24: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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;

Page 25: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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';

Page 26: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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;

Page 27: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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% '

Page 28: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.28

CSE4701

Problem 3.3c

SELECT album.title, track.nameFROM chinook.album, chinook.trackWHERE album.AlbumId = track.AlbumId AND track.Composer LIKE '%Hetfield%';

Page 29: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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;

Page 30: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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

Page 31: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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.

Page 32: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.32

CSE4701

Northwind Schema

Page 33: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.33

CSE4701

Northwind Schema – Key Types

Page 34: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.34

CSE4701

Problem 3.4a

SELECT * FROM NORTHWIND.PRODUCTS WHERE UNITPRICE >= 10 AND UNITPRICE <= 20ORDER BY UNITPRICE DESC;

Page 35: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

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;

Page 36: SQLDML.1 CSE 4701 Other SQL Query Examples. SQLDML.2 CSE 4701 Homework 3 from Spr 2015  Problem 6.18 from the 6 th edition done in SQL and NOT relational.

SQLDML.36

CSE4701

Problem 3.4c

SELECT * FROM NORTHWIND.PRODUCTS WHERE UNITPRICE >= 10 AND UNITPRICE <= 20ORDER BY UNITPRICE DESC;