Sei sulla pagina 1di 2

CMPC1M01 Worksheet 6: SQL

Submission date: 12:00 Tuesday 10 November 2009

The purpose of this worksheet is to develop a set of SQL queries that


retrieve data from the demo database that is defined and populated in the
database definition file demo_def.txt.

For this worksheet you should work individually.

1. Getting started:
1. Download the database definition file and take note of the local
directory in which you save the downloaded file (e.g. U:)
2. Start the local PostgreSQL server (Start Menu > All Programs > CMP
Development Tools > PostgreSQL > Start PostgreSQL Database).
Wait until it tells you it is ready to be used. Minimize the window
because you will need it later to shutdown the database safely.
When you have finished using the database, restore this window and
press Enter (as instructed). This will save and shutdown the database.
3. Start a command window
4. Using "cd", move to the directory in which you saved the file
(e.g. cd U:CMPC1M01)
5. Type "psql postgres User". You should get a prompt
"postgres=> ".
Please note the capital U on User. If that does not happen, cry for help
- you won't get anywhere without the PostgreSQL monitor running and
connected to a database!

2. Now you are connected to the database:


1. First, you need to load the demo database. Type "\i
demo_def.txt". You'll see a bunch of lines saying "DROP TABLE",
"CREATE TABLE", "NOTICE: ..." and "INSERT ...". Errors after the
"DROP TABLE" messages are OK and can be safely ignored.
2. You are now ready to begin issuing SQL commands (see the next
section). Don't forget to terminate them with a semicolon, otherwise
nothing will happen.
3. Use Notepad to keep a record of the queries that successfully answer
a task. After saving these commands, you can run them by typing "\i
<filename>", e.g. to verify that you've saved the variants that work.
4. When you have completed all the queries, and checked your answers,
save the Notepad file,
1. Start a new Postgres command window
2. Run your query script in the command window
3. Save the queries and output in a file called xxxddxxx.txt, where
xxxddxxx is your 8 character username (e.g. emd08nmu)
4. Submit the file via Blackboard. Note that submissions that are not
named in this way or which are not text files will not be considered.

3. Next write SQL queries to:

a) List all employee details.

b) List all employee names and titles.

c) List all employee titles, eliminating duplicates.

d) List employee names and the total cost of each employee for a 37 hour
week (use the hourly rate).

e) As (d) but label the total cost column 'Weekly_Cost'.

f) Display the total number of employees.

g) Display the minimum, maximum and average hourly rate for the
employees.

h) How many separate hourly rates are currently in use?

i) List the title, name and hourly rate of each employee, sorted by title
and within similar titles by name.

j) List the names of all employees with job title ʻManagerʼ.

k) List the names and hourly rates of all employees with hourly rates over
30 and with titles contain ʻStarʼ.

l) List the names of all employees who are managers, consultants or


stars

m) List names, titles, and projects for all employees.

n) As for (m), but sorted by project and title.

o) List names, project description and total hours worked for each
employee

(Queries (a) to (l) use only one table. Queries (m) and onwards use more than
one table and you need to specify a 'join' condition to link the tables.)

Potrebbero piacerti anche