Lab Session 2 - Final Topics on SQL Using the PostgreSQL DBMS

By Mitchell Welch

University of New England


Reading


Summary


Before we Get Started…

[mwelch8@turing ~]$ pg_dump mwelch8_prac_01 > ~/prac_01.sql

[mwelch8@turing ~]$ createdb mwelch8_prac_03
[mwelch8@turing ~]$ psql mwelch8_prac_03
psql (9.4.4)
Type "help" for help.

mwelch8_prac_03=> \i ~/prac_01.sql

...

mwelch8_prac_03=> \dt
             List of relations
 Schema |      Name      | Type  |  Owner  
--------+----------------+-------+---------
 public | department     | table | mwelch8
 public | dependent      | table | mwelch8
 public | dept_locations | table | mwelch8
 public | employee       | table | mwelch8
 public | project        | table | mwelch8
 public | works_on       | table | mwelch8
(6 rows)

String Comparison Operations


-- Retrieve all employees with an address in Houston,TX

SELECT fname,lname
FROM employee
WHERE address LIKE '%Houston, TX%'

-- Find employees that were born in the 1950's

SELECT fname,lname
FROM employee
WHERE bdate LIKE '__5_______'

'abc' LIKE 'abc'    -- true
'abc' LIKE 'a%'     -- true
'abc' LIKE '_b_'    -- true
'abc' LIKE 'c'      -- false

SELECT 'abc' LIKE 'abc' AS answer;
SELECT 'abc' LIKE 'a%' AS answer; 
SELECT 'abc' LIKE '_b_' AS answer;
SELECT 'abc' LIKE 'c' AS answer;
'abc' SIMILAR TO 'abc'      -- true
'abc' SIMILAR TO 'a'        -- false
'abc' SIMILAR TO '%(b|d)%'  -- true
'abc' SIMILAR TO '(b|c)%'   -- false

-- Get the full name in a single returned field (with a space between the names)

SELECT fname||' '||lname AS Full_name
FROM employee;

Arithmetic Operations

For example:


SELECT e.fname, e.lname, 1.1 * e.salary as Increased_salary
FROM employee AS e, works_on AS w , project AS p
WHERE e.ssn = w.essn AND 
    w.pno=p.pnumber AND 
    p.pname = 'ProductX';

http://www.postgresql.org/docs/8.1/static/functions-math.html

Aggregate Functions


-- Find the sum of all salaries, the max of all salaries and the min of all salaries.

SELECT SUM(salary), MAX(salary), MIN(salary)
FROM employee;

-- Find the sum of all salaries, the max of all salaries and the min of all salaries form the 'Research' department

SELECT SUM(salary), MAX(salary), MIN(salary)
FROM employee JOIN department on dno=dnumber
WHERE dname='Research';

-- A Count of all employees

SELECT COUNT(*) AS "Count of Employees"
FROM employee;

-- Count the number of distinct salary values in the database

SELECT COUNT(DISTINCT salary)
FROM employee;

Grouping


-- Retrieve the number of employees and the average salary for each department.

SELECT dno, COUNT(*), AVG(salary)
FROM employee
GROUP BY dno;

-- For each project that has more than 2 emplyees, list the project number, name and number of employees working on the project

SELECT pnumber, pname, COUNT(*)
FROM project, works_on
WHERE pnumber=pno
GROUP BY pnumber, pname
HAVING COUNT(*) > 2;

Views(Virtual Tables)


-- This view directly inherits the names of the SELECTed attributes from the
-- base tables

CREATE VIEW works_on1
AS SELECT fname,lname,pname,hours
FROM employee, project, works_on
WHERE ssn=essn AND pno=pnumber;

-- Look at the contents of your view...

SELECT * FROM works_on1;


-- This view renames the attributes and makes use of a GROUP BY clause to 
-- return the number of employees and the total salary for each department

CREATE VIEW dept_info(dept_name, no_of_emps,total_sal)
AS SELECT dname, COUNT(*),SUM(salary)
FROM department, employee
WHERE dnumber = dno
GROUP BY dname; 

-- Look at the contents of your view...

SELECT * FROM dept_info;
DROP VIEW dept_info;

Exercises For You

Question 1

Specify the following queries on the COMPANY database in SQL. Execute your query and review the results.

a) For each department whose average employee salary is more than $30,000, retrieve the department name and the number of employees working for that department.

b) Retrieve all employees with a last name that starts the the character ’S'.

c) Find all employees that work on a project with the the word ‘Product’ in its name.

Question 2

Specify the following views in SQL on the COMPANY database. Execute your query and review the results.

a) A view that has the department name, manager name, and manager salary for every department.

b) A view that has the employee name, supervisor name, and employee salary for each employee who works in the ‘Research’ department.

c) A view that has the project name, controlling department name, number of employees, and total hours worked per week on the project for each project.

Exercise 3

Using appropriate SQL DDL statements, create an SQL script (i.e. a file with the .sql extension) to implement that following schema:

center-aligned image

center-aligned image

Identify and implement the primary and foreign key values from the data supplied.

Finish your script by implementing a series of INSERT statements to load this data into the database. Run your script within your prac_02 database using the psql client’s \i command.