You are on page 1of 3

SQL Exam Section A (2 x 15=30 marks)

Consider the following database.


Location -------Name Sunlight ----------East .28 North .17 West .38 South .45 Plant ----Name Sunlight ----------Carrot .26 Beet .44 Corn .44 Tomato .42 Radish .28 Gardener -------GardenerID Name ---------- ---0 Mother 1 Father 2 Tim 3 Erin

LocationID ---------0 1 2 3

Water ----.80 .84 .48 .66

| | | | | |

Age --36 38 15 12

PlantID ------0 1 2 3 4

Water ----.82 .80 .76 .80 .84

Weight ----.08 .04 .26 .16 .02

PlantFK ------0 0 1 2 2 3 4

Planted ------GardenerFK LocationFK Date ---------- ------------0 0 04-18-2005 1 1 04-14-2005 0 2 04-18-2005 1 3 04-14-2005 2 2 04-19-2005 3 3 04-25-2005 2 0 04-30-2005 Picked -----GardenerFK LocationFK Date ---------- ------------2 0 08-18-2005 3 1 08-16-2005 1 3 08-22-2005 2 2 08-28-2005 3 3 08-22-2005 2 0 07-16-2005

Seeds ----28 14 36 20 12 38 30

PlantFK ------0 0 2 2 3 4

Amount -----28 12 52 18 15 23

Weight -----2.32 1.02 12.96 4.58 3.84 0.52

Some notes on terms: - The database is for a simple garden kept by a small family - They plant their garden in the spring and pick their garden in summer - The sunlight attribute refers to the percentage of a 24 hour day that the location gets sunlight and the plant optimally wants sunlight.

- The water attribute refers to the percentage of average rainfall that makes it to the root level for a location or is optimal for a plant. - The plant (average expected) and picked (actual) weight is in kilograms - The picked amount is the number of items (one carrot, one beet, an ear of corn, one tomato, one radish) picked.

1. For the five tables in the GARDEN database, identify whether each is an entity or relationship (type entity or relationship in each blank): ____________ ____________ ____________ ____________ ____________ Location Gardener Plant Planted Picked

2. For some reason, the beet crop did not succeed in producing edible beets. The family wants to eliminate beets from their garden forever. Write a valid SQL statement to delete the beet plant from the plant table: 3. write a valid SQL statement to delete beets from the planted table: 4. Write a query to find who planted the beets this year? 5. Write a valid SQL statement that calculates the total weight of all corns that were picked from the garden: 6. Using a multiplicative conversion factor of 2.2 to convert kilograms to pounds, write a valid SQL statement that shows the weight of the picked crop in total pounds per plant type. 7. Write a valid SQL statement that would produce a result set like the following: name | name | date | amount ------+--------+------------+-------Tim | Radish | 2005-07-16 | 23 Tim | Carrot | 2005-08-18 | 28 Tim | Corn | 2005-08-28 | 18 (3 rows) 8. Write a valid SQL statement that would produce a result set like the following: name | name | needed | available | variance --------+-------+--------+-----------+---------Carrot | East | 0.82 | 0.8 | -0.02 Carrot | West | 0.82 | 0.48 | -0.34 Carrot | North | 0.82 | 0.84 | 0.02 Carrot | South | 0.82 | 0.66 | -0.16 (4 rows)

9. Write a valid SQL statement that would produce a result set like the following: plantfk | plant_date | pick_date | growing_days ---------+------------+------------+-------------3 | 2005-04-25 | 2005-08-22 | 119 4 | 2005-04-30 | 2005-07-16 | 77 (2 rows) 10. Write a valid SQL statement that calculates the average number of items produced per seed planted for each plant type:

Employee empid empname salary mgrid

Phone Empid Phnumber

11. Select all employees who doesn't have phone. 12. Select the employee names who has more than one phone numbers. 13. Select the details of 3 max salaried employees from employee table. 2 tables Emp City Empid Empid EmpName City Salary 14. Find all employees who are living in the same city. 15. Given an employee table list out all the employees who earn more than an employee adams (using joins). 16. Given an employee table list out all the employees who earn more than the average salary of their department.

Section B (5 x 4=20 marks)


1. What is the Referential Integrity? 2. What is denormalization and when would you go for it? 3. Explain 2NF. 4. List the difference between primary key and unique key.

You might also like