Upload
nomaddarcy
View
778
Download
0
Tags:
Embed Size (px)
Citation preview
SQL Exercises
Dr. Dang Tran KhanhDepartment of Information Systems
Faculty of Computer Science and Engineering
Dr. Dang Tran Khanh ([email protected]) 2
Outline
Students’ presentation– Query processing & optimization (theory &
practice)
SQL exercises– SELECT statements
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]
Exercise 1
How many copies of the book titled The Lost Tribe are owned by the library branch whose name is "Sharpstown"?
Exercise 1
SELECT No_Of_CopiesFROM ((BOOK NATURAL JOIN BOOK_COPIES ) NATURAL JOIN LIBRARY_BRANCH )WHERE Title='The Lost Tribe' AND BranchName='Sharpstown'
Exercise 2
How many copies of the book titled The Lost Tribe are owned by each library branch?
Exercise 2
SELECT BranchName, No_Of_CopiesFROM ((BOOK NATURAL JOIN BOOK_COPIES ) NATURAL JOIN LIBRARY_BRANCH )WHERE Title='The Lost Tribe'
Exercise 3
Retrieve the names of all borrowers who do not have any books checked out .
Exercise 3
SELECT NameFROM BORROWER BWHERE NOT EXISTS ( SELECT *
FROM BOOK_LOANS LWHERE B.CardNo = L.CardNo )
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.
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
Exercise 5
For each library branch, retrieve the branch name and the total number of books loaned out from that branch.
Exercise 5
SELECT L.BranchName, COUNT(*)FROM LIBRARY_BRANCH L, BOOK_LOANS BLWHERE BL.BranchId = L.BranchIdGROUP BY L.BranchName
Exercise 6
Retrieve the names, addresses, and number of books checked out for all borrowers who have more than five books checked out.
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
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".
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'
Exercise 8
Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project.
Exercise 8
SELECT LNAME, FNAMEFROM EMPLOYEE, WORKS_ON, PROJECTWHERE DNO=5 AND SSN=ESSN AND PNO=PNUMBER AND PNAME='ProductX'
AND HOURS>10
Exercise 9
For each project, list the project name and the total hours per week (by all employees) spent on that project.
Exercise 9
SELECT PNAME, SUM (HOURS)FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNAME
Exercise 10
Retrieve the names of employees who work on every project.
Exercise 10
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE NOT EXISTS (SELECT PNUMBER
FROM PROJECTWHERE NOT EXISTS (SELECT *
FROM WORKS_ONWHERE PNUMBER=PNO AND ESSN=SSN ) )
Exercise 11
Retrieve the names of employees who do not work on any project.
Exercise 11
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE NOT EXISTS ( SELECT *
FROM WORKS_ON WHERE ESSN=SSN )
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.
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' )
Exercise 13
List the last names of department managers who have no dependents.
Exercise 13
SELECT LNAME, FNAMEFROM EMPLOYEEWHERE EXISTS ( SELECT *
FROM DEPARTMENTWHERE SSN=MGRSSN )ANDNOT EXISTS ( SELECT *FROM DEPENDENTWHERE SSN=ESSN )
Exercise 14
Find details of those employees whose salary is > the average salary for all employees. Output salary in descending order.
Exercise 14
SELECT *FROM EmployeeWHERE Salary > (SELECT AVG (Salary)
FROM Employee )ORDER BY Salary DESC;
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
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)
Dr. Dang Tran Khanh ([email protected]) 34
Q&A