SQL Exercises Using The Postgresql Dbms
SQL Exercises Using The Postgresql Dbms
Whilst not as well-known or popular among open-source DBMSes as MySQL, PostgreSQL has earned a reputation for
providing extensive functionality, for being at the cutting edge of SQL language innovations, and for being stable,
reliable, and having performance figures very close to that of the “big iron” industrial-strength Oracle DBMS.
PostgreSQL only communicates with the outside world via SQL. SQL goes in; query results (and sometimes error
messages) come out. It is fairly typical to manage the PostgreSQL DBMS and define new databases via SQL
commands entered at a command-line prompt. Whilst graphical and visual DBMS administration tools do exist for
almost every popular DBMS, they tend to be expensive and are frequently eschewed by database purists, who prefer
the absolute control they can only get from a command-line environment.
Therefore, in these practical exercises, you will explore SQL via the command-line.
There are two ways to use the PostgreSQL command-line with the PostgreSQL DBMS on our Computing server. You
can use a desktop database administration application called pgAdmin – which is available on-campus – or a Web-
based administration tool called phpPgAdmin. PhpPgAdmin is installed on our Computing server.
Please note that due to security restrictions, pgAdmin can only be used on-campus from the campus wired
network. It won’t work with the Computing server via Wi-Fi, and it won’t work with the Computing server
outside the university, even if you download pgAdmin (it’s free).
If you wish to use pgAdmin, read the Using_pgAdmin.doc document, then come back here to do the exercises.
If you wish to use phpPgAdmin, read the Using_phpPgAdmin.doc document, then come back here to do the exercises.
If you like, you can use pgAdmin in the labs and use phpPgAdmin from home. Since your work is stored in your
PostgreSQL database, you can access it either way.
The following exercises will help you develop familiarity with SQL in general, and with the PostgreSQL DBMS in
particular. You’ll almost certainly want to refer to the PostgreSQL documentation, which can be found at
http://www.postgresql.org/docs/9.5/interactive/index.html In particular, you should refer to the “The SQL Language”
chapter at http://www.postgresql.org/docs/9.5/interactive/sql.html
1
SQL Exercises
Note: All changes you make to your database are saved as soon as you make them.
There is no need for explicit “save” or “load” operations. This is one of the benefits
of using a database!
2
SQL Exercises
Reading
SQL DATA DEFINITION
Here are some examples of these commands. (Don’t type these in.)
Note that for each column we create in a table, we must specify its data type.
Data types include NUMERIC, TIMESTAMP, DATE, CHAR, TEXT, and VARCHAR.
We may also specify that it is the primary key for the table.
A column may also be specified as NOT NULL. This means that every row in the resulting table must have a value for
this column.
It will immediately delete the table and all data in it (be careful with this command!) This should bring up the message
'Table Dropped' or an error message stating it does not exist.
Exercises
Exercise 1 (do these)
Add to the demo database a table EMP_ADDR that has the following address information. Apart from the
APT and POSTCODE columns, none of the columns may have a null value. EMPNO is to be the primary
key.
3
SQL Exercises
Exercise 2
Use the ALTER TABLE command to add a new column CAR_RT which is 6 characters wide and may accept null
values.
4
SQL Exercises
Reading
SQL DATA MANIPULATION
These commands allow data to be added to, altered or deleted from tables.
Note that in the first command, we have not specified the columns under which the data is to be inserted.
We can only do this when inserting a specified value for every column.
With UPDATE, we can alter or add values for rows that have already been INSERTed.
UPDATE STOCK
SET SLEVEL = 300
WHERE STOCKNO = 1;
UPDATE STOCK
SET DESCRIPTION = 'Trousers'
WHERE STOCKNO = 2;
UPDATE STOCK
SET SLEVEL = 100
WHERE DESCRIPTION = 'Belts';
UPDATE STOCK
SET SLEVEL = SLEVEL + 100
WHERE STOCKNO < 3;
5
SQL Exercises
Exercises
Exercise 3
Use the INSERT command to enter up the following rows into the table EMP_ADDR:
If your result is incorrect, then use the UPDATE and DELETE commands to achieve the desired outcome.
Exercise 4
Use the UPDATE command to set a value 'CR05' for the column CAR_RT for the employee 'Smith', to give the
following table:
6
SQL Exercises
Reading
Querying Data in Tables
The SELECT command is used for retrieving and displaying data in tables.
Here are some examples of this command. Can you explain what each of them do?
7
SQL Exercises
Exercises
Exercise 5
The DEPT table has 3 columns. List the table using '*' to indicate all of the columns, without row ordering to give the
following result:
Exercise 6
Achieve the same result as the previous exercise; only specify all of the columns in your SELECT command. Take care
to spell the names of the columns correctly!
Exercise 7
To make the report easier to read, have the DEPTNO column repeated on the right hand side of the screen. List the
DEPT table as in Exercise 6, but repeat the DEPTNO column after LOC to give the following result:
Exercise 8
In SQL, individual search conditions contained the WHERE clause such as SAL = 2000 are known as predicates.
When a predicate is TRUE for a given row or set of rows, then that row or set of rows is returned. Those rows for
which the predicate is FALSE are not returned.
Write the SELECT command that will return the rows from the DEPT table where the name of the department is
'SALES' i.e.
Exercise 9
Suppose we had the request "Show me information on Clerks who were hired on or after May 1st 1981". We need to
translate this into an SQL query. The relevant information is in the EMP table.
We need to query this table using two predicates, one to find anyone is a clerk and the other to find anyone hired on or
after May 1st 1981.
We combine predicates in the WHERE clause using the AND and OR operators.
8
SQL Exercises
Exercise 10
Let's try a more difficult question: "Which employees in department 20 are clerks or have a salary of at least 1500?"
We now have 3 different predicates to combine : a department of 20, a job of CLERK and a salary >= 1500. The first
one must be true for all rows returned, either the second or the third must also be true for all rows returned. This means
we must use AND and OR in combining our predicates.
When AND and OR are used in the same WHERE clause, AND expressions are evaluated before OR expressions. We
can use parentheses to force a different order of evaluation, in other words by placing a parenthesis around the OR part
of a WHERE clause we can force the OR to be evaluated before the AND e.g.
A AND B OR C ---> A AND B is evaluated first, and the result of this is combined
with C using OR
A AND (B OR C) ---> B OR C is evaluated first, and the result of this is combined
with A using AND.
The result of your SELECT command should return the following table:
Exercise 11
To put the rows of EMP into some useful sequence down the page or screen, list the employee name, job and manager
columns of EMP, and sequence the result by employee name. (Hint: use the ORDER BY clause)
9
SQL Exercises
Exercise 12
Rows can be sequenced in reverse order, Show the employee name, job and salary columns of EMP in order, with the
highest salary first.
Column Expressions
As well as retrieving simple columns from table, in SQL a column may be composed of an expression. Here are some
examples of expressions that perform computations:
Exercise 13
Produce a report that shows each employee's name, their job, salary, commission and total earnings. Total earnings are
the salary plus the commission. The desired result should be:
Note how many of the rows will not have a total earnings value under SAL+COMM. This is because these rows have a
NULL value for COMM. In SQL, when two column values participate in an expression, if either of them has the value
NULL, then the result will be NULL.
(How could we overcome this?).
10
SQL Exercises
Exercise 14
Let's refine Exercise 13. For those who are eligible for a commission (i.e. those whose COMM value is not NULL),
add a column what shows what percentage of their total earnings is commission. The formula is 100 * (COMM / (SAL
+ COMM))
11
SQL Exercises
Reading
Aggregate functions
It is possible to show the result of a function applied to a column. Here are some examples of aggregate functions:
Exercises
Exercise 15
12
SQL Exercises
Reading
Retrieving data from multiple tables
Often we need to generate a report that uses data from more than one table. If we wished to show an employee's name
and address, we would need to join the two tables thusly:
This query shows us employee information from the EMP table combined, or joined, with address information from the
EMP_ADDR table, wherever the employee number in EMP matches the employee number in EMP_ADDR.
In other words, it looks up address information for employees by employee number. Or, looking at it another way, it
looks up employee information for addresses by employee number.
Exercises
Exercise 16
Write the command to display, for the employee James, his name, department number and department name. Like this:
Exercise 17
A join can be used for constraining data for one table. Here is a join that displays EMP data for employees who live in
BOSTON:
Write a query that retrieves a list of all employees whose department is located in Chicago thus:
ENAME JOB
--------------------------------------
ALLEN SALESMAN
BLAKE MANAGER
TURNER SALESMAN
JAMES CLERK
MARTIN SALESMAN
WARD SALESMAN
13
SQL Exercises
Reading
Nested queries
When a join only displays columns that come from a single table, it can be written as a nested query. The previous
result could be obtained using this command:
We would use the IN operator where the result of the nested query could return a set of values (i.e. there could be more
than one DEPT with a LOC of Chicago).
We can use the '=', '<', '>' when we know that the nested query will yield exactly one value. This will always be the
case when an aggregate function is used. For example:
Exercises
Exercise 18
Write a command that will yield a list of names, salaries and departments for those employees whose salary is below
the average for all employees:
Exercise 19
Write the command that shows which analysts earn more than the average salary for managers. The result should be:
Views
A view is a logical table that is based on one or more tables in the database.
Here is a view for all employees in DEPT 20:
This view can now be used in queries as if it were a table. For example:
14
SQL Exercises
Exercise 20
Create a view called EMP_NAD that includes the EMPNO, NAME, ADDRESS, APT, CITY, COUNTY and
POSTCODE for all employees who have an entry in the EMP_ADR table.
END
15