In order to run the examples presented through this practical session, you should import the database you created last week into a new database for this week.
Create a backup of your prac_01 database using the pg_dump utility:
[mwelch8@turing ~]$ pg_dump mwelch8_prac_01 > ~/prac_01.sql
\i command from within the psql client:
[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)
Throughout this practical session, you should run each of the example queries and review the results returned from the COMPANY database.
Now you are ready for the prac…
LIKE operator.The string patterns are specified through the use of two reserved characters:
% is used to specify an arbitrary section of 0 or more characters_ is used to specify a single character.The reserved characters are simply inserted into the comparison literal on the right side of the LIKE operator.
Try These:
-- 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_______'
You wish to use the ‘%’ and ‘_’ characters as part of a string literal for matching, you will need to escape them in your string using a ‘\’ character:
A few more interesting examples (source: http://www.postgresql.org/docs/9.4/static/functions-matching.html):
'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;
SIMILAR TOSIMILAR TO supports these pattern-matching meta-characters borrowed from POSIX regular expressions:
A few Simple ones:
'abc' SIMILAR TO 'abc' -- true
'abc' SIMILAR TO 'a' -- false
'abc' SIMILAR TO '%(b|d)%' -- true
'abc' SIMILAR TO '(b|c)%' -- false
|| operator:
-- Get the full name in a single returned field (with a space between the names)
SELECT fname||' '||lname AS Full_name
FROM employee;
+,-,*,/) can be applied to numerical values and attributes with numeric domains.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
COUNT, SUM, MAX, MIN and AVERAGESELECT clause.
-- 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;
`WHERE clause to restrict the records that the aggregate functions are applied to:
-- 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;
GROUP BY operations allows us to partition a relation into a set of non-overlapping groups.GROUP BY operation specifies the attribute(s) to use for creating the subgroups.
-- Retrieve the number of employees and the average salary for each department.
SELECT dno, COUNT(*), AVG(salary)
FROM employee
GROUP BY dno;
HAVING clause.HAVING clause puts a condition on the summary information
-- 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 are used to provide an additional layer of abstraction between the implementation of the database and user applications.
Views can be created by using the CREATE VIEW ... AS ... clause
Examples for you:
-- 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 ... clause:DROP VIEW dept_info;
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.
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.
Using appropriate SQL DDL statements, create an SQL script (i.e. a file with the .sql extension) to implement that following schema:


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.