Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5-1 CSE 4701 In Class SQL Queries – Part II n Homework 3 from Spring 2015 n Problem 6.18 from the 6 th edition done in SQL and NOT relational expressions.

Similar presentations


Presentation on theme: "Chapter 5-1 CSE 4701 In Class SQL Queries – Part II n Homework 3 from Spring 2015 n Problem 6.18 from the 6 th edition done in SQL and NOT relational expressions."— Presentation transcript:

1 Chapter 5-1 CSE 4701 In Class SQL Queries – Part II n Homework 3 from Spring 2015 n Problem 6.18 from the 6 th edition done in SQL and NOT relational expressions n Problem 4.10 in 6 th edition

2 Chapter 5-2 CSE 4701 Homework 3 from Spr 2015 n Problem 6.18 from the 6 th edition done in SQL and NOT relational expressions

3 Chapter 5-3 CSE 4701 c. Retrieve the names of all borrowers who do not have any books checked out SELECT Name FROM BORROWER B WHERE NOT EXIST ( SELECT * FROM BOOK_LOANS L WHERE B.CardNo = L.CardNo )

4 Chapter 5-4 CSE 4701 e. For each library branch, retrieve the branch name and the total number of books loaned out from that branch. SELECT L.BranchName, COUNT(*) FROM BOOK_COPIES B, LIBRARY_BRANCH L WHERE B.BranchId = L.BranchId GROUP BY L.BranchName

5 Chapter 5-5 CSE 4701 f. Retrieve the names, addr, and num of books checked out for all borrowers who have more than five books checked out. 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

6 Chapter 5-6 CSE 4701 Homework 3 from Spr 2015 n Problem 4.10 in 6 th edition

7 Chapter 5-7 CSE 4701 Problem 4.10 in 6 th edition

8 Chapter 5-8 CSE 4701 (a) Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project. 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

9 Chapter 5-9 CSE 4701 (e) Retrieve the names of employees who work on every project. 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):

10 Chapter 5-10 CSE 4701 (f) Retrieve the names of employees who do not work on any project. SELECT LNAME, FNAME FROM EMPLOYEE WHERE NOT EXISTS ( SELECT * FROM WORKS_ON WHERE ESSN=SSN ) Result (empty):

11 Chapter 5-11 CSE 4701 (i) Find the names/addrs of employees who work on at least one project in Houston but whose dept has no location in Houston. 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

12 Chapter 5-12 CSE 4701 (j) List the last names of dept managers who have no dependents. SELECT LNAME, FNAME FROM EMPLOYEE WHERE EXISTS ( SELECT * FROM DEPARTMENT WHERE SSN=MGRSSN ) ANDNOT EXISTS ( SELECT * FROM DEPENDENT WHERE SSN=ESSN ) Result: LNAME FNAME Borg James

13 ANN.13 CSE 4701 In Class SQL Queries – Part I 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.

14 ANN.14 CSE 4701 Northwind Schema

15 ANN.15 CSE 4701 Northwind Schema – Key Types

16 ANN.16 CSE 4701 Prob 1: All Products Less that 10 return all columns. SELECT * FROM NORTHWIND.PRODUCTS WHERE UNITPRICE < 10;

17 ANN.17 CSE 4701 Prob 2: Prob 2: List products that is between 10 to 20 dollars by Price in Descending order SELECT * FROM NORTHWIND.PRODUCTS WHERE UNITPRICE >= 10 AND UNITPRICE <= 20 ORDER BY UNITPRICE DESC;

18 ANN.18 CSE 4701 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 FROM NORTHWIND.PRODUCTS, NORTHWIND.CATEGORIESWHERE PRODUCTS.CategoryID = CATEGORIES.CategoryID AND Categories.CategoryName="Beverages“ ORDER BY UNITPRICE;

19 ANN.19 CSE 4701 Find and print the company names and company addresses of all Suppliers that supply the category name Seafood. 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, northwind.products WHERE suppliers.SupplierID = products.SupplierID AND categories.CategoryID = products.CategoryID AND categories.CategoryName = 'Seafood';

20 ANN.20 CSE 4701 Count and print the number of suppliers for each of the eight different categories of food Prob 5: Count and print the number of suppliers for each of the eight different categories of food 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;

21 ANN.21 CSE 4701 In Class SQL Queries 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

22 ANN.22 CSE 4701 Chinook EER

23 ANN.23 CSE 4701 https://chinookdatabase.codeplex.com/wikipage?title=Chi nook_Schema&referringTitle=Documentation https://chinookdatabase.codeplex.com/wikipage?title=Chi nook_Schema&referringTitle=Documentation

24 ANN.24 CSE 4701 What Does Data Look Like? StevesSongs PL12, T3T3, Hello, A1 PL12, T5T4, Bye, A1 PL12, T7T5, MySong, A1 PL13, T3 etc.

25 ANN.25 CSE 4701 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, customer.city, customer.state FROM chinook.employee, chinook.customer WHERE employee.LastName = customer.LastName;

26 ANN.26 CSE 4701 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, chinook.track, chinook.genre WHERE customer.CustomerId = invoice.CustomerId AND invoice.InvoiceId = invoiceline.InvoiceId AND invoiceline.TrackId = track.TrackId AND track.GenreId = genre.GenreId AND genre.name = 'rock';

27 ANN.27 CSE 4701 Prob 8: Album name and tracks of all of the albums by the composer James Hetfield, grouped by Album.  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;

28 ANN.28 CSE 4701 Prob 8: Album name and tracks of all of the albums by the composer James Hetfield, grouped by Album.  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% '

29 ANN.29 CSE 4701 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 WHERE album.AlbumId = track.AlbumId AND track.Composer LIKE '%Hetfield%';

30 ANN.30 CSE 4701 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) FROM chinook.customer, chinook.invoice WHERE customer.Country = 'Canada' AND customer.CustomerId = invoice.CustomerId GROUP BY customer.LastName;


Download ppt "Chapter 5-1 CSE 4701 In Class SQL Queries – Part II n Homework 3 from Spring 2015 n Problem 6.18 from the 6 th edition done in SQL and NOT relational expressions."

Similar presentations


Ads by Google