You are on page 1of 5

LAB 04: SQL Exercises (Queensland Semester 2/2010 - 2011)

References:
http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10759/toc.htm
http://www.java2s.com/Tutorial/Oracle/CatalogOracle.htm
Exercise 1: Give the following tables:

Please write SQL commands according to the requirements as follows:


1.
2.
3.
4.
5.
6.
7.
8.

Select the last name of all employees


Select the last name of all employees, without duplicates
Select all the data of employees whose last name is "Smith"
Select all the data of employees that work in department 14.
Select all the data of employees that work in department 37 or department 77.
Select all the data of employees whose last name begins with an "S".
Select the sum of all the departments' budgets.
Select the number of employees in each department (you only need to show the
department code and the number of employees).
9. Select all the data of employees, including each employee's department's data.
10. Select the name and last name of each employee, along with the name and budget of the
employee's department.
11. Select the name and last name of employees working for departments with a budget
greater than $60,000.
12. Select the departments with a budget larger than the average budget of all the
departments.
13. Select the names of departments with more than two employees.
14. Add a new department called "Quality Assurance", with a budger of $40,000 and
departmental code 11. Add an employee called "Mary Moore" in that department, with
SSN 847-2198-110.
15. Reduce the budget of all departments by 10%.
16. Reassign all employees from the Research department (code 77) to the IT department
(code 14).
17. Fire all employees in the IT department (code 14).
18. Fire all employees who work in departments with a budget greater than or equal to
$60,000.
19. Fire all employees.

Exercise 2: Give a database schema for a library management system as the following
picture:

1. How many copies of the book titled The Lost Tribe are owned by the library branch
whose name is "Sharpstown"?
2. How many copies of the book titled The Lost Tribe are owned by each library branch?
3. Retrieve the names of all borrowers who do not have any books checked out .
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.
5. For each library branch, retrieve the branch name and the total number of books loaned
out from that branch.
6. Retrieve the names, addresses, and number of books checked out for all borrowers who
have more than five books checked out.
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 3: Give a database schema of a company as the following picture.

1. Retrieve the names of employees in department 5 who work more than 10 hours per week
on the 'ProductX' project.
2. For each project, list the project name and the total hours per week (by all employees)
spent on that project.
3. Retrieve the names of employees who work on every project.
4. Retrieve the names of employees who do not work on any project.
5. 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.
6. List the last names of department managers who have no dependents.
7. Find details of those employees whose salary is > the average salary for all employees.
Output salary in descending order.
8. Find details of those employees whose salary is > the average salary for all employees in
his/her department. Output salary in ascending order.

Exercise 4: Give the following database schema, write SQL commands:

1.
2.
3.
4.
5.
6.

Select the name of all the pieces.


Select all the providers' data.
Obtain the average price of each piece (show only the piece code and the average price).
Obtain the names of all providers who supply piece 1.
Select the name of pieces provided by provider with code "HAL".
For each piece, find the most expensive offering of that piece and include the piece name,
provider name, and price (note that there could be two providers who supply the same
piece at the most expensive price).
7. Add an entry to the database to indicate that "Skellington Supplies" (code "TNBC") will
provide sprockets (code "1") for 7 cents each.
8. Increase all prices by one cent.
9. Update the database to reflect that "Susan Calvin Corp." (code "RBT") will not supply
any pieces (the provider should still remain in the database).
10. Update the database to reflect that "Susan Calvin Corp." (code "RBT") will not supply
bolts (code 4).
Exercise 5: Consider the COMPANY relational database schema:

1.
2.
3.
4.
5.

Show all products that have fewer units in stock than the reorder level specifies.
What are the last names of all employees born before Jan 1, 1960?
List the supplier name and city for all suppliers based in the United States.
List the supplier name and URL for all suppliers that have a home page.
Using one of the special operators, list the supplier name and country for all suppliers
based in an English-speaking county.
6. Using one of the special operators, list the product name and price for all products that
sell for as little as $10 and as much as $20.
7. List the company name for all customers whose company name falls between 'L' and 'O',
inclusive.
8. List the last name and title for all employees who work in sales.
9. Get supplier names and order ID for all orders.
10. Get supplier name, order ID and product name for all orders supplied by "Tokyo
Traders."
11. Get supplier name, order ID, product name, and dollar value of the amount of that
product ordered for each order supplied by "Tokyo Traders."
12. Get the shipper name and supplier name for all orders shipped by Speedy Express.
13. List the shipper name and the number of orders shipped by each shipper.
14. Display the quantity ordered of each product.
15. Write a query that shows how many orders are associated with each customer ID. Note
that the customerID field is replaced with the Customers name. Why is that?
16. Total inventory (dollar value) on hand by category.
17. Show the company names of all customers that have ordered the product "Aniseed
Syrup."
18. Show the company names of all suppliers who have discontinued products.
19. Show the total dollar value of all the orders we have for each customer (one dollar
amount for each customer). Order the list in descending order.

You might also like