34
SQL Exercises Dr. Dang Tran Khanh Department of Information Systems Faculty of Computer Science and Engineering [email protected]

Lecture09

Embed Size (px)

Citation preview

Page 1: Lecture09

SQL Exercises

Dr. Dang Tran KhanhDepartment of Information Systems

Faculty of Computer Science and Engineering

[email protected]

Page 2: Lecture09

Dr. Dang Tran Khanh ([email protected]) 2

Outline

Students’ presentation– Query processing & optimization (theory &

practice)

SQL exercises– SELECT statements

Page 3: Lecture09

Dr. Dang Tran Khanh ([email protected]) 3

SELECT statement

SELECT [DISTINCT | ALL]

{* | [columnExpression [AS newName]] [,...] }

FROM TableName [alias] [, ...]

[WHERE condition]

[GROUP BY columnList] [HAVING condition]

[ORDER BY columnList]

Page 4: Lecture09

Exercise 1

How many copies of the book titled The Lost Tribe are owned by the library branch whose name is "Sharpstown"?

Page 5: Lecture09

Exercise 1

SELECT No_Of_CopiesFROM ((BOOK NATURAL JOIN BOOK_COPIES ) NATURAL JOIN LIBRARY_BRANCH )WHERE Title='The Lost Tribe' AND BranchName='Sharpstown'

Page 6: Lecture09

Exercise 2

How many copies of the book titled The Lost Tribe are owned by each library branch?

Page 7: Lecture09

Exercise 2

SELECT BranchName, No_Of_CopiesFROM ((BOOK NATURAL JOIN BOOK_COPIES ) NATURAL JOIN LIBRARY_BRANCH )WHERE Title='The Lost Tribe'

Page 8: Lecture09

Exercise 3

Retrieve the names of all borrowers who do not have any books checked out .

Page 9: Lecture09

Exercise 3

SELECT NameFROM BORROWER BWHERE NOT EXISTS ( SELECT *

FROM BOOK_LOANS LWHERE B.CardNo = L.CardNo )

Page 10: Lecture09

Exercise 4

For each book that is loaned out from the "Sharpstown" branch and whose DueDate is today, retrieve the book title, the borrower's name, and the borrower's address.

Page 11: Lecture09

Exercise 4

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 ANDBL.DueDate='today' AND BL.CardNo=R.CardNo AND BL.BookId=B.BookId

Page 12: Lecture09

Exercise 5

For each library branch, retrieve the branch name and the total number of books loaned out from that branch.

Page 13: Lecture09

Exercise 5

SELECT L.BranchName, COUNT(*)FROM LIBRARY_BRANCH L, BOOK_LOANS BLWHERE BL.BranchId = L.BranchIdGROUP BY L.BranchName

Page 14: Lecture09

Exercise 6

Retrieve the names, addresses, and number of books checked out for all borrowers who have more than five books checked out.

Page 15: Lecture09

Exercise 6

SELECT B.Name, B.Address, COUNT(*)FROM BORROWER B, BOOK_LOANS LWHERE B.CardNo = L.CardNoGROUP BY B.CardNo, B.Name, B.AddressHAVING COUNT(*) > 5

Page 16: Lecture09

Exercise 7

For each book authored (or co-authored) by "Stephen King", retrieve the title and the number of copies owned by the library branch whose name is "Central".

Page 17: Lecture09

Exercise 7

SELECT Title, No_Of_CopiesFROM (((BOOK_AUTHORS NATURAL JOIN BOOK) NATURAL

JOIN BOOK_COPIES) NATURAL JOIN LIBRARY_BRANCH)WHERE Author_Name='Stephen King' AND BranchName='Central'

Page 18: Lecture09

Exercise 8

Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project.

Page 19: Lecture09

Exercise 8

SELECT LNAME, FNAMEFROM EMPLOYEE, WORKS_ON, PROJECTWHERE DNO=5 AND SSN=ESSN AND PNO=PNUMBER AND PNAME='ProductX'

AND HOURS>10

Page 20: Lecture09

Exercise 9

For each project, list the project name and the total hours per week (by all employees) spent on that project.

Page 21: Lecture09

Exercise 9

SELECT PNAME, SUM (HOURS)FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNAME

Page 22: Lecture09

Exercise 10

Retrieve the names of employees who work on every project.

Page 23: Lecture09

Exercise 10

SELECT LNAME, FNAMEFROM EMPLOYEEWHERE NOT EXISTS (SELECT PNUMBER

FROM PROJECTWHERE NOT EXISTS (SELECT *

FROM WORKS_ONWHERE PNUMBER=PNO AND ESSN=SSN ) )

Page 24: Lecture09

Exercise 11

Retrieve the names of employees who do not work on any project.

Page 25: Lecture09

Exercise 11

SELECT LNAME, FNAMEFROM EMPLOYEEWHERE NOT EXISTS ( SELECT *

FROM WORKS_ON WHERE ESSN=SSN )

Page 26: Lecture09

Exercise 12

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.

Page 27: Lecture09

Exercise 12

SELECT LNAME, FNAME, ADDRESS FROM 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' )

Page 28: Lecture09

Exercise 13

List the last names of department managers who have no dependents.

Page 29: Lecture09

Exercise 13

SELECT LNAME, FNAMEFROM EMPLOYEEWHERE EXISTS ( SELECT *

FROM DEPARTMENTWHERE SSN=MGRSSN )ANDNOT EXISTS ( SELECT *FROM DEPENDENTWHERE SSN=ESSN )

Page 30: Lecture09

Exercise 14

Find details of those employees whose salary is > the average salary for all employees. Output salary in descending order.

Page 31: Lecture09

Exercise 14

SELECT *FROM EmployeeWHERE Salary > (SELECT AVG (Salary)

FROM Employee )ORDER BY Salary DESC;

Page 32: Lecture09

Dr. Dang Tran Khanh ([email protected]) 32

Notes

Many other details related to SQL statements can be found in the proposed materials– [1]: Chapters 8, 9– [3]: All– http://www.oracle.com

Page 33: Lecture09

Dr. Dang Tran Khanh ([email protected]) 33

Summary

Students’ presentation– Query processing & optimization (theory & practice)

SQL exercises– SELECT statements

Next Lecture:– Functional dependencies & normalization: students’ talk

([1]: Chapters 10, 11)– Introduction to indexing techniques/structures in DBs ([1]:

Chapters 13, 14)

Page 34: Lecture09

Dr. Dang Tran Khanh ([email protected]) 34

Q&A