62
The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Embed Size (px)

Citation preview

Page 1: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

The Carnie_MyCareer Case Study

Peter EbeidDane Harris

Stephanie HoChris Zaharia

Distinction Assignment, Autumn 2007

Presented by

Page 2: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Introduction to Carnie_MyCareer Database

• This database is a simplified version of the 'MyCareer' website.

• It allows prospective job seekers to register and apply for positions. Users may apply for multiple positions, and more than one person may apply for any given job.

Page 3: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

The ERD for Carnie_MyCareerLocIDLocCityLocCountry

Location

CanIDCanFirstNameCanLastNameCanSexCanBirthYear

Candidate

SecIDSecTitleSecDesc

Sector

JobID*CanID*AppID

Application

JobIDJobPostDateJobSalaryJobPositionLocID*SecID*JobDesc

Job

Page 4: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

SQL Queries on a Single Entity/ Table

Page 5: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Project (using “select”)

• Report a job’s title and salary

SELECT jobposition, jobsalary FROM Carnie_MyCareer_Job;

The columns you listbetween “SELECT” and“FROM” are displayed.

jobposition | jobsalary-------------+----------- Senior | 50000 CFO | 120000 Jr | 45000 Reception | 20000 Graduate | 38000 Team Member | 85000 Team Member | 45000 2IC | 60000 Logistics | 75000

Page 6: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Restrict (using “where”)

• Get all jobs with a salary (jobsalary) greater then 70000

SELECT * FROM Carnie_MyCareer_Job WHERE jobsalary > 70000;

jobid | jobpostdate | jobsalary | jobposition | locid | secid | jobdesc-------+-------------+-----------+-------------+-------+-------+----------------------------------- 2 | 01-01-2005 | 120000 | CFO | 1 | 1 | CFO of big four bank 6 | 25-06-2005 | 85000 | Team Member | 4 | 4 | Help rebuild peoples lives in Iraq 9 | 25-06-2005 | 75000 | Logistics | 4 | 3 | Team player with excellent skills

Page 7: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Project and restrict combo

• You can specify any combination of columns and rows

• Example: List all job’s post date, title and salary where salaries are less then 50000

SELECT jobpostdate, jobsalary, jobposition FROM Carnie_MyCareer_Job WHERE jobsalary < 50000;

jobpostdate | jobsalary | jobposition-------------+-----------+------------- 01-03-2005 | 45000 | Jr 05-03-2005 | 20000 | Reception 07-02-2005 | 38000 | Graduate 25-06-2005 | 45000 | Team Member

Page 8: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

IS NULL & IS NOT NULL

• IS NULL

SELECT jobposition, jobsalary FROM Carnie_MyCareer_Job WHERE jobsalary IS NULL;

• IS NOT NULL

SELECT jobposition, jobsalary FROM Carnie_MyCareer_Job

WHERE jobsalary IS NOT NULL;

jobposition | jobsalary ------------+-----------(0 rows)

jobposition | jobsalary-------------+----------- Senior | 50000 CFO | 120000 Jr | 45000 … etc … | … etc …

Page 9: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

IN

• Used with a list of values

• Example: Report all jobs where job title is CEO or Team Member

SELECT * FROM Carnie_MyCareer_Job

where jobposition IN ('CEO','Team Member');

jobid | jobpostdate | jobsalary | jobposition | locid | secid | jobdesc-------+-------------+-----------+-------------+-------+-------+----------------------------------- 6 | 25-06-2005 | 85000 | Team Member | 4 | 4 | Help rebuild peoples lives in Iraq 7 | 25-06-2005 | 45000 | Team Member | 5 | 4 | Anti-Terror proofing parlment house

Page 10: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

NOT IN

• Not in a list of values

• Report all jobs where job title is other then CEO or Team Member

SELECT * FROM Carnie_MyCareer_Job WHERE jobposition NOT IN ('CEO','Team Member');

jobid | jobpostdate | jobsalary | jobposition | locid | secid | jobdesc-------+-------------+-----------+-------------+-------+-------+----------------------------------- 1 | 01-01-2005 | 50000 | Senior | 1 | 1 | Account manager, blue chip company 2 | 01-01-2005 | 120000 | CFO | 1 | 1 | CFO of big four bank 3 | 01-03-2005 | 45000 | Jr | 1 | 2 | Entry level Database position. Great start!! 4 | 05-03-2005 | 20000 | Reception | 2 | 3 | Basic office admin, phones and coffee making 5 | 07-02-2005 | 38000 | Graduate | 3 | 1 | Great start for a fresh graduate 8 | 25-06-2005 | 60000 | 2IC | 2 | 2 | Second in command of well run NZ IT company 9 | 25-06-2005 | 75000 | Logistics | 4 | 3 | Team player with excellent organisation skills

Page 11: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Ordering columns

SELECT jobsalary, jobposition FROM Carnie_MyCareer_Job WHERE

jobpostdate = '01-01-2005';

SELECT jobposition, jobsalary FROM Carnie_MyCareer_Job WHERE

jobpostdate = '01-01-2005';

jobsalary | jobposition-----------+------------- 50000 | Senior 120000 | CFO

jobposition | jobsalary-------------+----------- Senior | 50000 CFO | 120000

Page 12: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Ordering rows: “order by”

• List all jobs where salary is at least 50000, and order the report in descending order by salary amounts

SELECT * FROM Carnie_MyCareer_Job WHERE jobsalary >= 50000 ORDER BY jobsalary DESC;

Note – DESC is short for Descending

jobid | jobpostdate | jobsalary | jobposition | locid | secid | jobdesc-------+-------------+-----------+-------------+-------+-------+----------------------------------- 2 | 01-01-2005 | 120000 | CFO | 1 | 1 | CFO of big four bank 6 | 25-06-2005 | 85000 | Team Member | 4 | 4 | Help rebuild peoples lives in Iraq 9 | 25-06-2005 | 75000 | Logistics | 4 | 3 | Team player with excellent skills 8 | 25-06-2005 | 60000 | 2IC | 2 | 2 | Second in command of well run NZ IT 1 | 01-01-2005 | 50000 | Senior | 1 | 1 | Account manager, blue chip company

Page 13: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Calculating

• AS

SELECT jobposition, jobsalary, jobsalary*1.3 AS jobsalaryUSD FROM Carnie_MyCareer_Job;

jobposition | jobsalary | jobsalaryusd-------------+-----------+-------------- Senior | 50000 | 65000.0 CFO | 120000 | 156000.0 Jr | 45000 | 58500.0 Reception | 20000 | 26000.0 Graduate | 38000 | 49400.0 Team Member | 85000 | 110500.0 Team Member | 45000 | 58500.0 2IC | 60000 | 78000.0 Logistics | 75000 | 97500.0

Page 14: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

AVG, SUM, MIN, and MAX Functions

• Average salary (AVG)

SELECT AVG(jobsalary) FROM Carnie_MyCareer_Job;

• Sum of salaries (SUM)

SELECT SUM(jobsalary) FROM Carnie_MyCareer_Job;

• Minimum salary (MIN)

SELECT MIN(jobsalary) FROM Carnie_MyCareer_Job;

• Maximum salary (MAX)

SELECT MAX(jobsalary) FROM Carnie_MyCareer_Job;

AVG(jobsalary) ---------------

59777.7777778

SUM(jobsalary) --------------

538000

MIN(jobsalary) --------------

20000 MAX(jobsalary)

-------------- 120000

Page 15: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

COUNT Function

• Determine the total number of rows in the job table

COUNT(*) : the number of rows

SELECT COUNT(*) FROM Carnie_MyCareer_Job;

OR

• COUNT(name of column) : the number of non-null values in that column

SELECT COUNT(jobsalary) FROM Carnie_MyCareer_Job;

count ------- 9

count ------- 9

Page 16: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

LIKE - Pattern matching

• List all job dates starting with the 1st of the month

SELECT jobpostdate FROM Carnie_MyCareer_Job WHERE jobpostdate LIKE '01%';

• List all job dates in March

SELECT jobpostdate FROM Carnie_MyCareer_Job WHERE jobpostdate LIKE '%-03-%';

jobpostdate------------- 01-01-2005 01-01-2005 01-03-2005

jobpostdate------------- 01-03-2005 05-03-2005

Page 17: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

LIKE - Pattern matchingList job titles with the 4th letter as “i”

SELECT jobposition FROM Carnie_MyCareer_Job WHERE jobposition LIKE '___i%';

• List all job titles without an “e” in their name

SELECT jobposition FROM Carnie_MyCareer_Job WHERE jobposition NOT LIKE '%E%' AND jobposition NOT LIKE '%e%';

jobposition------------- Senior Logistics

jobposition------------- CFO Jr 2IC Logistics

Page 18: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

DISTINCT

• Eliminating duplicate rows

Report the different values of job salaries

SELECT DISTINCT jobsalary FROM Carnie_MyCareer_Job;

The value 45000 does not display twice

jobsalary----------- 20000 38000 45000 50000 60000 75000 85000 120000

Page 19: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Inserting rows

INSERT INTO Carnie_Mycareer_Job (jobid, jobpostdate, jobsalary, jobposition, locid, secid, jobdesc) VALUES (1, '01-01-2005', 50000, 'Senior', 1, 1,'Account manager, blue chip company');

• OR

INSERT INTO Carnie_Mycareer_Job VALUES (1,

'01-01-2005', 50000, 'Senior', 1, 1,'Account manager, blue chip company');

Page 20: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Foreign Keys and Natural Joins

Page 21: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

The Natural Join (1)

• Create a new table from two existing tables by matching on common columns

SELECT * FROM Carnie_MyCareer_Job NATURAL JOIN Carnie_MyCareer_Location;

locid | jobid | jobpostdate | jobsalary | jobposition | secid | jobdesc | loccity | loccountry-------+-------+-------------+-----------+-------------+-------+-------------------------------------+------------+------------- 1 | 1 | 01-01-2005 | 50000 | Senior | 1 | Account manager, blue chip company | Sydney | Australia 1 | 2 | 01-01-2005 | 120000 | CFO | 1 | CFO of big four bank | Sydney | Australia 1 | 3 | 01-03-2005 | 45000 | Jr | 2 | Entry level Database position. Great| Sydney | Australia 2 | 4 | 05-03-2005 | 20000 | Reception | 3 | Basic office admin, phones | Wellington | New Zealand 3 | 5 | 07-02-2005 | 38000 | Graduate | 1 | Great start for a fresh graduate | New York | USA 4 | 6 | 25-06-2005 | 85000 | Team Member | 4 | Help rebuild peoples lives in Iraq | Baghdad | Iraq 5 | 7 | 25-06-2005 | 45000 | Team Member | 4 | Anti-Terror proofing parliment house| Canberra | Australia 2 | 8 | 25-06-2005 | 60000 | 2IC | 2 | Second command of well run company | Wellington | New Zealand 4 | 9 | 25-06-2005 | 75000 | Logistics | 3 | Team player with excellent skills | Baghdad | Iraq

Page 22: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

The Natural Join (2)

• The same thing as using “natural join” on the previous slide, but using the alternate (“cross product”) notation

SELECT * FROM Carnie_MyCareer_Job, Carnie_MyCareer_Location WHERE Carnie_MyCareer_Job.locid = Carnie_MyCareer_Location.locid;

• Notice the use of the dot ... a table name, followed by “.”, followed by the name of a column in the table. This disambiguates which column we mean.

Page 23: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

The Natural Join (2)

• The dot notation is used to disambiguate between columns of the same name in different tables

• Example: Table 1 (Carnie_MyCareer_Job)

• Table 2 (Carnie_MyCareer_Location)

jobid | jobpostdate | jobsalary | jobposition | locid| secid | jobdesc-------+-------------+-----------+-------------+-------+-------+------------------------------------------------ 1 | 01-01-2005 | 50000 | Senior | 1 | 1 | Account manager, blue chip company 2 | 01-01-2005 | 120000 | CFO | 1 | 1 | CFO of big four bank 3 | 01-03-2005 | 45000 | Jr | 1 | 2 | Entry level Database position. Great start!! 4 | 05-03-2005 | 20000 | Reception | 2 | 3 | Basic office admin, phones and coffee making 5 | 07-02-2005 | 38000 | Graduate | 3 | 1 | Great start for a fresh graduate 6 | 25-06-2005 | 85000 | Team Member | 4 | 4 | Help rebuild peoples lives in Iraq 7 | 25-06-2005 | 45000 | Team Member | 5 | 4 | Anti-Terror proofing parliment house 8 | 25-06-2005 | 60000 | 2IC | 2 | 2 | Second in command of well run NZ IT company 9 | 25-06-2005 | 75000 | Logistics | 4 | 3 | Team player with excellent organisation skills

locid | loccity | loccountry-------+------------+------------- 1 | Sydney | Australia 2 | Wellington | New Zealand 3 | New York | USA 4 | Baghdad | Iraq 5 | Canberra | Australia

Page 24: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

The Natural Join (2)

• Final Table (joining Table Job with Table Location

SELECT * FROM Carnie_MyCareer_Job, Carnie_MyCareer_Location WHERE Carnie_MyCareer_Job.locid = Carnie_MyCareer_Location.locid;

Common Column

locid | jobid | jobpostdate | jobsalary | jobposition | secid | jobdesc | loccity | loccountry-------+-------+-------------+-----------+-------------+-------+-------------------------------------+------------+------------- 1 | 1 | 01-01-2005 | 50000 | Senior | 1 | Account manager, blue chip company | Sydney | Australia 1 | 2 | 01-01-2005 | 120000 | CFO | 1 | CFO of big four bank | Sydney | Australia 1 | 3 | 01-03-2005 | 45000 | Jr | 2 | Entry level Database position. Great| Sydney | Australia 2 | 4 | 05-03-2005 | 20000 | Reception | 3 | Basic office admin, phones | Wellington | New Zealand 3 | 5 | 07-02-2005 | 38000 | Graduate | 1 | Great start for a fresh graduate | New York | USA 4 | 6 | 25-06-2005 | 85000 | Team Member | 4 | Help rebuild peoples lives in Iraq | Baghdad | Iraq 5 | 7 | 25-06-2005 | 45000 | Team Member | 4 | Anti-Terror proofing parliment house| Canberra | Australia 2 | 8 | 25-06-2005 | 60000 | 2IC | 2 | Second command of well run company | Wellington | New Zealand 4 | 9 | 25-06-2005 | 75000 | Logistics | 3 | Team player with excellent skills | Baghdad | Iraq

Page 25: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Entities & Relationships

Page 26: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

One-to-Many Relationship 1:m

• One to many relationship (1:m) occurs when one record in a table is related to multiple records in another table.

• Example: a job seeking candidate can have many job applications.

• Hence, a candidate table has a 1 to many relationship with the application table.

Page 27: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

One-to-Many Relationship 1:m• Example (con’t)

• Entity Relation Diagram:

CanIDCanFirstNameCanLastNameCanSexCanBirthYear

Candidate

JobID*CanID*AppID

Application

1 m

Page 28: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

One-to-Many Relationship 1:m• Example (con’t)

• Tables:

canid | canfirstname | canlastname | cansex | canbirthyear

-------+--------------+-------------+--------+--------------

1 | Chris | Carnie | M | 08-01-1981

2 | David | Saddington | M | 21-06-1980

3 | Katie | Lenehan | F | 04-04-1978

4 | Sussie | Kelly | F | 24-11-1969

5 | Ron | Howard | M | 01-01-2000

Candidate (carnie_mycareer_candidate)

canid | appid | jobid-------+-------+------- 1 | 1 | 1 1 | 6 | 5 1 | 12 | 9 2 | 5 | 3 2 | 7 | 5 2 | 10 | 8 3 | 3 | 1 3 | 8 | 5 3 | 11 | 8 3 | 13 | 9 4 | 2 | 1 4 | 4 | 2 4 | 9 | 5

Applicartion (carnie_mycareer_application)

1 m(3)

Page 29: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Many-to-Many Relationship m:m• Many to many relationship (1:m) occurs when one

record in a table a is related to multiple records in another table b; at the same time one record in table b is related to many tables in table a.

• Example: One candidate applies for many jobs; at the same time each of the jobs that candidate has applied for has many other candidates applying for it as well.

• Hence, a candidate table has a many to many (indirect) relationship with the job table.

Page 30: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Many-to-Many Relationship m:m• Example (con’t)

• Entity Relation Diagram:

CanIDCanFirstNameCanLastNameCanSexCanBirthYear

Candidate

JobID*CanID*AppID

Application

1 m

JobIDJobPostDateJobSalaryJobPositionLocID*SecID*Job Desc

Job

1m

Associative Entity

m:m• Many-to-many relationships in the second normal form and

onwards have associative entities (such as application) to preserve the relationship between two entities while preventing the replication of data.

Page 31: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Many-to-Many Relationship m:m• Example (con’t)

• Tables:

jobid | jobpostdate | jobsalary | jobposition | locid | secid-------+-------------+-----------+-------------+-------+------- 1 | 01-01-2005 | 50000 | Senior | 1 | 1 2 | 01-01-2005 | 120000 | CFO | 1 | 1 3 | 01-03-2005 | 45000 | Jr | 1 | 2 4 | 05-03-2005 | 20000 | Reception | 2 | 3 5 | 07-02-2005 | 38000 | Graduate | 3 | 1 6 | 25-06-2005 | 85000 | Team Member | 4 | 4 7 | 25-06-2005 | 45000 | Team Member | 5 | 4 8 | 25-06-2005 | 60000 | 2IC | 2 | 2 9 | 25-06-2005 | 75000 | Logistics | 4 | 3

canid | canfirstname | canlastname | cansex | canbirthyear

-------+--------------+-------------+--------+--------------

1 | Chris | Carnie | M | 08-01-1981

2 | David | Saddington | M | 21-06-1980

3 | Katie | Lenehan | F | 04-04-1978

4 | Sussie | Kelly | F | 24-11-1969

5 | Ron | Howard | M | 01-01-2000

canid | appid | jobid-------+-------+------- 1 | 1 | 1 1 | 6 | 5 1 | 12 | 9 2 | 5 | 3 2 | 7 | 5 2 | 10 | 8 3 | 3 | 1 3 | 8 | 5 3 | 11 | 8 3 | 13 | 9 4 | 2 | 1 4 | 4 | 2 4 | 9 | 5

Candidate (carnie_mycareer_candidate)

Applicartion (carnie_mycareer_application)

Job (carnie_mycareer_job)

1

m(3)

1

m(2)

One candidate has many applications

Each application is for one job

One job has many applications

Each application is for one candidate

Page 32: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Group by, Sub-queries & Complex Joins

Page 33: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Group by• The group by clause allows the records in a table

to be cascaded into groups where their fields in a certain column are equal.

• Example: If you wanted to view the number of applications for each position, you would select all the columns of the application table, grouped by the job ID.

Page 34: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Group by• Example (con’t)

• Code & Tables:

jobid | appid | canid-------+-------+------- 1 | 1 | 1 1 | 2 | 4 1 | 3 | 3 2 | 4 | 4 3 | 5 | 2 5 | 6 | 1 5 | 7 | 2 5 | 8 | 3 5 | 9 | 4 8 | 10 | 2 8 | 11 | 3 9 | 12 | 1 9 | 13 | 3

Applicartion (carnie_mycareer_application)

SELECT jobid, count(appid) FROM carnie_mycareer_application GROUP BY jobid;

• Without group by, the database would not know how to view the count for each jobid.

jobid | count-------+------- 1 | 3 2 | 1 3 | 1 5 | 4 8 | 2 9 | 2

Query Result

Page 35: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Having• Adding the having clause to a group by allows the

user to add an aggregate condition that works similarly to the where clause.

• Example: If you wanted to view the number of applications for positions with more than one applicant then you would add the having after the group by statement.

Page 36: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Group by• Example (con’t)

• Code & Tables:

jobid | appid | canid-------+-------+------- 1 | 1 | 1 1 | 2 | 4 1 | 3 | 3 2 | 4 | 4 3 | 5 | 2 5 | 6 | 1 5 | 7 | 2 5 | 8 | 3 5 | 9 | 4 8 | 10 | 2 8 | 11 | 3 9 | 12 | 1 9 | 13 | 3

Applicartion (carnie_mycareer_application)

SELECT jobid, count(appid) FROM carnie_mycareer_application GROUP BY jobid HAVING count(jobid)count(jobid) > 1;

• the having statement eliminates any jobs with one or less applicants ( i.e. > 1).

Query Result

jobid | count-------+------- 1 | 3 5 | 4 8 | 2 9 | 2

Page 37: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Subqueries• List the Job position and Salary of all Jobs with a salary above the average.

jobsalary | jobposition-----------+------------- 120000 | CFO 85000 | Team Member 60000 | 2IC 75000 | Logistics

avg-------------------- 59777.777777777778

SELECT jobsalary,jobposition FROM carnie_mycareer_job

WHERE jobsalary >

(SELECT AVG(jobsalary) FROM carnie_mycareer_job);

Page 38: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Using subqueries to find the maximum (or minimum)• List the jobposition and job description of the lowest paid job (the job with the minimum salary)

min------- 20000

SELECT jobposition, jobdescFROM carnie_mycareer_jobWHERE jobsalary <= ( SELECT min(jobsalary) FROM carnie_mycareer_job);

jobposition | jobdesc-------------+---------------------------------------------- Reception | Basic office admin, phones and coffee making

Page 39: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Using subqueries to find the maximum (or minimum) “ALL”• List all the job positions and their job descriptions which have salary less or equal to ALL the salary.

jobposition | jobdesc-------------+---------------------------------------------- Reception | Basic office admin, phones and coffee making

jobsalary----------- 50000 120000 45000 20000 38000 85000 45000 60000 75000

SELECT jobposition, jobdescFROM carnie_mycareer_jobWHERE jobsalary <= ALL( SELECT jobsalary FROM carnie_mycareer_job);

Page 40: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

ANY operator

SELECT jobposition,jobsalary FROM carnie_mycareer_job WHERE locid != 1 AND jobsalary = ANY

(SELECT jobsalary FROM carnie_mycareer_job WHERE locid =1);

• This query lists all the positions and salaries of jobs not in Sydney where the salary is equal to any of the jobs that are in Sydney

jobposition | jobsalary-------------+----------- Team Member | 45000

jobsalary----------- 50000 120000 45000

Page 41: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

In: an Alternate to ANY

jobposition | jobsalary-------------+----------- Team Member | 45000

jobsalary----------- 50000 120000 45000

SELECT jobposition,jobsalary FROM carnie_mycareer_job WHERE locid != 1 and jobsalary IN

(SELECT jobsalary FROM carnie_mycareer_job WHERE locid =1);

• This query lists all the positions and salaries of jobs not in Sydney where the salary is equal to any of the jobs that are in Sydney

Page 42: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Left Outer Join

SELECT * FROM carnie_mycareer_candidate LEFT JOIN carnie_mycareer_application USING (CanID);

canid | canfirstname | canlastname | cansex | canbirthyear | appid | jobid-------+--------------+-------------+--------+--------------+-------+------- 1 | Chris | Carnie | M | 08-01-1981 | 1 | 1 1 | Chris | Carnie | M | 08-01-1981 | 6 | 5 1 | Chris | Carnie | M | 08-01-1981 | 12 | 9 2 | David | Saddington | M | 21-06-1980 | 5 | 3 2 | David | Saddington | M | 21-06-1980 | 7 | 5 2 | David | Saddington | M | 21-06-1980 | 10 | 8 3 | Katie | Lenehan | F | 04-04-1978 | 3 | 1 3 | Katie | Lenehan | F | 04-04-1978 | 8 | 5 3 | Katie | Lenehan | F | 04-04-1978 | 11 | 8 3 | Katie | Lenehan | F | 04-04-1978 | 13 | 9 4 | Sussie | Kelly | F | 24-11-1969 | 2 | 1 4 | Sussie | Kelly | F | 24-11-1969 | 4 | 2 4 | Sussie | Kelly | F | 24-11-1969 | 9 | 5 5 | Ron | Howard | M | 01-01-2000 | |

appid | jobid | canid-------+-------+------- 1 | 1 | 1 2 | 1 | 4 3 | 1 | 3 4 | 2 | 4 5 | 3 | 2 6 | 5 | 1 7 | 5 | 2 8 | 5 | 3 9 | 5 | 4 10 | 8 | 2 11 | 8 | 3 12 | 9 | 1 13 | 9 | 3

canid | canfirstname | canlastname | cansex | canbirthyear-------+--------------+-------------+--------+-------------- 1 | Chris | Carnie | M | 08-01-1981 2 | David | Saddington | M | 21-06-1980 3 | Katie | Lenehan | F | 04-04-1978 4 | Sussie | Kelly | F | 24-11-1969 5 | Ron | Howard | M | 01-01-2000

Page 43: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Right Outer Join

SELECT * FROM carnie_mycareer_candidate RIGHT JOIN carnie_mycareer_application USING (CanID); canid | canfirstname | canlastname | cansex | canbirthyear | appid | jobid-------+--------------+-------------+--------+--------------+-------+------- 1 | Chris | Carnie | M | 08-01-1981 | 1 | 1 4 | Sussie | Kelly | F | 24-11-1969 | 2 | 1 3 | Katie | Lenehan | F | 04-04-1978 | 3 | 1 4 | Sussie | Kelly | F | 24-11-1969 | 4 | 2 2 | David | Saddington | M | 21-06-1980 | 5 | 3 1 | Chris | Carnie | M | 08-01-1981 | 6 | 5 2 | David | Saddington | M | 21-06-1980 | 7 | 5 3 | Katie | Lenehan | F | 04-04-1978 | 8 | 5 4 | Sussie | Kelly | F | 24-11-1969 | 9 | 5 2 | David | Saddington | M | 21-06-1980 | 10 | 8 3 | Katie | Lenehan | F | 04-04-1978 | 11 | 8 1 | Chris | Carnie | M | 08-01-1981 | 12 | 9 3 | Katie | Lenehan | F | 04-04-1978 | 13 | 9

canid | canfirstname | canlastname | cansex | canbirthyear-------+--------------+-------------+--------+-------------- 1 | Chris | Carnie | M | 08-01-1981 2 | David | Saddington | M | 21-06-1980 3 | Katie | Lenehan | F | 04-04-1978 4 | Sussie | Kelly | F | 24-11-1969 5 | Ron | Howard | M | 01-01-2000

appid | jobid | canid-------+-------+------- 1 | 1 | 1 2 | 1 | 4 3 | 1 | 3 4 | 2 | 4 5 | 3 | 2 6 | 5 | 1 7 | 5 | 2 8 | 5 | 3 9 | 5 | 4 10 | 8 | 2 11 | 8 | 3 12 | 9 | 1 13 | 9 | 3

Page 44: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Self Join• Show jobs posted on same day as the job

position 'Logistics‘

jobid | jobpostdate | jobposition-------+-------------+------------- 6 | 25-06-2005 | Team Member 7 | 25-06-2005 | Team Member 8 | 25-06-2005 | 2IC

SELECT job1.JobID, job1.JobPostDate, job1.JobPositionFROM Carnie_MyCareer_Job job1, Carnie_MyCareer_Job job2WHERE job1.JobPostDate = job2.JobPostDateAND job2.JobPosition = 'Logistics'AND job1.JobPosition <> 'Logistics';

Page 45: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Data Integrity with SQL

Page 46: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Data Integrity

locid | loccity | loccountry-------+------------+------------- 1 | Sydney | Australia 2 | Wellington | New Zealand 3 | New York | USA 4 | Baghdad | Iraq 5 | Canberra | Australia

jobid | jobpostdate | jobsalary | jobposition | locid | secid | jobdesc-------+-------------+-----------+-------------+-------+-------+--------------------------------------- 1 | 01-01-2005 | 50000 | Senior | 1 | 1 | Account manager, blue chip company 2 | 01-01-2005 | 120000 | CFO | 1 | 1 | CFO of big four bank 3 | 01-03-2005 | 45000 | Jr | 1 | 2 | Entry level Database position. 4 | 05-03-2005 | 20000 | Reception | 2 | 3 | Basic office admin, phone 5 | 07-02-2005 | 38000 | Graduate | 3 | 1 | Great start for a fresh graduate 6 | 25-06-2005 | 85000 | Team Member | 4 | 4 | Help rebuild peoples lives in Iraq 7 | 25-06-2005 | 45000 | Team Member | 5 | 4 | Anti-Terror proofing parliment 8 | 25-06-2005 | 60000 | 2IC | 2 | 2 | Second in command of well run company 9 | 25-06-2005 | 75000 | Logistics | 4 | 3 | Team player with excellent skills

Primary Key

Foreign Key

Page 47: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Creating the linked foreign keyCREATE TABLE CARNIE_MYCAREER_JOB( JobID JobPostDate JobSalary JobPosition LocID SecID JobDesc CONSTRAINT PKJobID PRIMARY KEY (JobID), CONSTRAINT FKLocID FOREIGN KEY (LocID) REFERENCES

Carnie_Mycareer_Location ON DELETE RESTRICT);

CREATE TABLE CARNIE_MYCAREER_LOCATION( LocID LocCity LocCountry CONSTRAINT PKLocID PRIMARY KEY (LocID));

Page 48: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Check Constraints ExampleCREATE TABLE CARNIE_MYCAREER_APPLICATION( AppID INTEGER NOT NULL, JobID INTEGER NOT NULL, CanID INTEGER NOT NULL,CONSTRAINT PKAppID PRIMARY KEY (AppID, JobID, CanID)CONSTRAINT ValidAppID CHECK (AppID > 0)CONSTRAINT ValidJobID CHECK (JobID > 0)CONSTRAINT ValidCanID CHECK (CanID > 0));CREATE TABLE CARNIE_MYCAREER_CANDIDATE( CanID INTEGER NOT NULL, CanFirstName VARCHAR NOT NULL, CanLastName VARCHAR NOT NULL, CanSex CHAR (1) CHECK (CanSex IN

('M','F')), CanBirthYear VARCHAR NOT NULL, CONSTRAINT PKCanID PRIMARY KEY (CanID));

Page 49: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

SQL Syntax for ActionsCREATE TABLE CARNIE_MYCAREER_APPLICATION( AppID INTEGER NOT NULL, JobID INTEGER NOT NULL, CanID INTEGER NOT NULL,CONSTRAINT PKAppID PRIMARY KEY (AppID, JobID, CanID)CONSTRAINT FKJobID FOREIGN KEY (JobID) references

CARNIE_MYCAREER_JOB ON DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT FKCanID FOREIGN KEY (CanID) references CARNIE_MYCAREER_CANDIDATE ON DELETE CASCADE ON UPDATE CASCADE );

Page 50: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

SQL Syntax for Actions• For example trying to delete CanID = 1

from the Candidate table:

DELETE FROM carnie_mycareer_candidate WHERE CanID = 1;

Results in:DELETE 1 (A successful deletion)

The delete action has cascaded to the Application table as observed by doing the following:SELECT * FROM carnie_mycareer_application;

appid | jobid | canid-------+-------+------- 2 | 1 | 4 3 | 1 | 3 4 | 2 | 4 5 | 3 | 2 7 | 5 | 2 8 | 5 | 3 9 | 5 | 4 10 | 8 | 2 11 | 8 | 3 13 | 9 | 3

No CanID = 1 !!!

Page 51: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

SQL Syntax for Actions (2)CREATE TABLE CARNIE_MYCAREER_JOB( JobID INTEGER NOT NULL, JobPostDate VARCHAR NOT NULL, JobSalary INTEGER NOT NULL, JobPosition VARCHAR NOT NULL, LocID INTEGER NOT NULL, SecID INTEGER NOT NULL, JobDesc VARCHAR NOT NULL,

CONSTRAINT PKJobID PRIMARY KEY (JobID), CONSTRAINT FKLocID FOREIGN KEY (LocID) REFERENCES

Carnie_Mycareer_Location ON DELETE RESTRICT ON UPDATE CASCADE,

CONSTRAINT FKSecID FOREIGN KEY (SecID) REFERENCES Carnie_Mycareer_Sector

ON DELETE RESTRICT ON UPDATE CASCADE );

Page 52: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

SQL Syntax for Actions (2)• For example trying to delete a LocID

from the Location table:DELETE FROM carnie_mycareer_location WHERE LocID = 1;

Will produce the error:update or delete on "carnie_mycareer_location" violates foreign key constraint "fklocid" on "carnie_mycareer_job“

As the foreign key LocID in the Job table restricts the data sharing this key’s value (1) from being deleted in the Job table.

Page 53: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Normalization

Page 54: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

1st Normal Form

SectorID JobID JobPosition SecTitle LocCity

1 1 Reception IT Sydney

1 2 Senior IT Melbourne

2 3 CFO Finance Sydney

2 4 Logistics Finance Melbourne

Composite Primary Key

SectorID SectorTitleJobID JobPosition, LocCity

•DATA REDUNDANCY!•Solution : Split into 2 or more tables

• … see the next slide Composite Primary Key

Page 55: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Improving on 1st Normal Form

JobID JobPosition LocCity

1 Reception Sydney

2 Senior Melbourne

3 CFO Sydney

4 Logistics Melbourne

SectorID SecTitle

1 IT

1 IT

2 Finance

2 Finance

The respective primary keys for each table

• Fixes the problem shown in the previous slide

Page 56: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Problem with 2nd Normal Form

CanID CanName CanSuburb CanPostCode

1 Peter Ebeid XXX 111

2 Dane Harris XXX 111

3 Stephanie Ho YYY 222

4 Chris Zaharia YYY 222

Primary Key

CanSuburb CanPostCode

• MORE DATA REDUNDANCY!• Solution: Split into 2 or more tables

• … see the next slide

• Imagine if we had a “candidate” table like this …

Page 57: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Improving on 2nd Normal Form

CanSuburb CanPostCode

XXX 111

YYY 222

The respective primary key for each table

CanID CanName CanSuburb

1 Peter Ebeid XXX

2 Dane Harris XXX

3 Stephanie Ho YYY

4 Chris Zaharia YYY

• Fixes the problem shown in the previous slide

Page 58: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Problem with 3rd Normal Form

CanID CanName CanTaxFileNo

1 Peter Ebeid 111111

2 Dane Harris 222222

3 Stephanie Ho 333333

4 Chris Zaharia 444444

Primary Key

CanID CanTaxFileNo andCanTaxFileNo CanID

• MORE DATA REDUNDANCY!• Solution: Split into 2 or more tables

• … see the next slide

• Imagine if we had a “candidate” table like this …

Or instead of tax file number, maybe email address, ormobile no.

Page 59: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Improving on 3rd Normal Form

The respective primary key for each table

CanID CanName

1 Peter Ebeid

2 Dane Harris

3 Stephanie Ho

4 Chris Zaharia

• Fixes the problem shown in the previous slide• It’s Boyce Codd Normal Form (BCNF)

CanID CanTaxFileNo

1 111111

2 222222

3 333333

4 444444

Page 60: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

An Example of Creating a View

CREATE VIEW joblocation(jobID, position, salary, locID, City, Country)AS SELECT jobID, jobposition, jobsalary, locid, locCity, locCountry

FROM Carnie_MyCareer_Job natural join Carnie_MyCareer_Location;

Page 61: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

An Example of Creating a View

position | city-------------+---------- Team Member | Canberra

• Query exactly as if a table

SELECT position, city FROM joblocation WHERE city = 'Canberra';

Page 62: The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by

Thank You