Sei sulla pagina 1di 104

0.

SQL revisited

7/24/13 6:31 PM

SQL revisited

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

SQL
Data Definition Language: CREATE, ALTER, DROP
statements, e.g. CREATE TABLE, ALTER TABLE, DROP
TABLE, CREATE INDEX, etc
Data Manipulation Language: INSERT, UPDATE,
DELETE, TRUNCATE statements
Data Retrieval Language: SELECT statement

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

Sample database
REQUIRED
slevel
Requires
*
APPLICANT
anumber
ID
fname
lname
dob
city
state
phone
fax [0..1]
email [0..1]
*

Possesses
*

SKILL
sname

Needs
ID

SPOSSESSED
slevel

*
SNEEDED
slevel

Applies-for
1..*

Offered-by

APPLIES
appdate

Employed-by

fromdate
EMPLBY
todate[0..1]

Janusz R. Getta

POSITION
pnumber
ID
title
salary
extras [0..1]
bonus [0..1]
specification

EMPLOYER
ename
ID
city
state
phone
fax [0..1]
email[0..1]
web[0..1]

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

CREATE TABLE statement


Create a relational table that contains information about the
total number of base skills needed for each position
CREATE TABLE TOTSKILL (
pnumber
NUMBER(8)NOT NULL,
total_skill NUMBER(3) DEFAULT 0 NOT NULL,
CONSTRAINT TOTSKILL_PKEY PRIMARY KEY (pnumber),
CONSTRAINT TOTSKILL_FKEY FOREIGN KEY (pnumber)
REFERENCES POSITION(pnumber),
CONSTRAINT TOTSKILL_CHECK CHECK(total_skill>=0) );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

ALTER TABLE statement


Remove a column bonus from a relational table POSITION
ALTER TABLE POSITION DROP COLUMN bonus;

Add a column bonus to a relational table POSITION, add


constraint bonus > 0
ALTER TABLE POSITION ADD ( bonus NUMBER(7,2) NULL
CONSTRAINT POSITION_CHECK1 CHECK ( bonus > 0 ) );

Add a constraint bonus < 10000 to a column bonus in a


relational table POSITION
ALTER TABLE POSITION MODIFY ( bonus NUMBER(7,2)
CONSTRAINT POSITION_CHECK2 CHECK ( bonus < 10000 ) );

Modify a state of check constraint SPOSSESSED_CHECK1 on


SPOSSESSED table to DISABLE
ALTER TABLE SPOSSESSED
MODIFY CONSTRAINT SPOSSESSED_CHECK1 DISABLE;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

DROP TABLE statement


Drop a relational table SNEDED and place it in a recycle bin
DROP TABLE SNEEDED;

Permanently drop a relational table SREQUIRED


DROP TABLE SREQUIRED PURGE;

Drop a relational table EMPLOYER together with all referential


integrity constraints
DROP TABLE EMPLOYER CASCADE CONSTRAINTS;

Restore the relational tables EMPLOYER and SNEEDED


FLASHBACK TABLE EMPLOYER TO BEFORE DROP;
FLASHBACK TABLE SNEEDED TO BEFORE DROP;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

INSERT statement
Insert into a relational table SNEEDED information that position
number 1 needs a skill SQL programming at level 9
INSERT INTO SNEEDED VALUES( 1, 'SQL programming', 9);

Insert into a relational table SPOSSESSED information that


applicant number 1 has a skill SQL programming at a default
level
INSERT INTO SPOSSESSED VALUES( 1, 'SQL programming',
DEFAULT);

Insert into a relational table EMPLBY information that applicant


number 18 has been employed today by RMIT
INSERT INTO EMPLBY VALUES( 18, 'RMIT', SYSDATE, NULL);

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

INSERT statement
Copy a relational table EMPLOYER into two tables EMP and
EMP_NULL, a table EMP must include the values of NOT NULL
attribute and a table EMP_NULL the values of other attributes
INSERT ALL
INTO EMP(ename, state, city, phone)
VALUES(ename, state, city, phone)
INTO EMP_NULL(ename, fax, email, web)
VALUES(ename, fax, email, web)
SELECT * FROM EMPLOYER;
SELECT * FROM EMP;
SELECT * FROM EMP_NULL;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

INSERT statement
Copy a relational table EMPLOYER into two tables EMP_SYDNEY
and EMP_OTHERS, a table EMP_SYDNEY must include
information about employers from Sydney and a table
EMP_OTHER information about employers from other cities,
record only NOT NULL attributes
INSERT ALL
WHEN city = 'Sydney' THEN
INTO EMP_SYDNEY
ELSE
INTO EMP_OTHERS
SELECT ename, city, state, phone
FROM EMPLOYER;
SELECT * FROM EMP_SYDNEY;
SELECT * FROM EMP_OTHERS;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

0. SQL revisited

7/24/13 6:31 PM

DELETE statement
A skill reading is no longer required for network
administration
DELETE SREQUIRED
WHERE sname = 'network administration' AND
requires = 'reading';

Delete position number 1


DELETE POSITION
WHERE pnumber = 1;

Delete employers from Sydney


DELETE EMPLOYER
WHERE city = 'Sydney';
ERROR at line 1:
ORA-02292: integrity constraint (CERT.EMPLBY_FKEY2)
violated - child record found
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

10

0. SQL revisited

7/24/13 6:31 PM

UPDATE statement
Increase by 10% salary of position number 1
UPDATE POSITION
SET salary = salary + 0.1 * salary
WHERE pnumber = 1;

Change the values of an attribute email to NULL for all


employers from Sydney
UPDATE ( SELECT city, email
FROM EMPLOYER
WHERE city = 'Sydney' )
SET email = NULL;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

11

0. SQL revisited

7/24/13 6:31 PM

SELECT statement
SELECT statement consists of the following clauses:
select clause:
from clause:
where clause:
join clause:
group by clause:
having clause:
order by clause:
subquery factoring clause:
for update clause:
hierachical query clause:
model clause:
search clause:

Janusz R. Getta

SELECT
FROM
WHERE
JOIN
GROUP BY
HAVING
ORDER BY
WITH
FOR UPDATE
START WITH CONNECT BY
MODEL
SEARCH

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

12

0. SQL revisited

7/24/13 6:31 PM

SELECT statement
SELECT statement consists of the following clauses:

cycle clause:
flushback query clause:
pivot clause:
unpivot clause:
unpivot in clause:
partition extension clause:
subquery restriction clause:
query partition clause:
rollup cube clause:
grouping sets clause:
return rows clause:

Janusz R. Getta

CYCLE
VERSIONS/AS OF
PIVOT
UNPIVOT
UNPIVOT IN
PARTITION/SUBPARTITION
WITH
PARTITION BY
ROLLUP/CUBE
GROUPING SETS
RETURN

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

13

0. SQL revisited

7/24/13 6:31 PM

SELECT statement
SELECT statement consists of the following clauses:

model column clause:


model rules clause:
model iterate clause:

Janusz R. Getta

PARTITION BY/
DIMENSION BY
RULES
ITERATE

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

14

0. SQL revisited

7/24/13 6:31 PM

Selecting all columns


Select all columns from one relational table
SELECT *
FROM APPLICANT;

Select all columns from many relational tables


SELECT APPLICANT.*, APPLIES.*
FROM APPLICANT, APPLIES
WHERE APPLICANT.anumber = APPLIES.anumber;

Select all columns using table alias (correlation variable)


SELECT A.*, APPLIES.*
FROM APPLICANT A, APPLIES;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

15

0. SQL revisited

7/24/13 6:31 PM

Selecting all columns


Select all columns from a table in another schema (jrg)
SELECT *
FROM jrg.APPLICANT;

Select all columns from the result of a query


WITH APPNUM AS (SELECT anumber
FROM APPLICANT)
SELECT *
FROM APPNUM;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

16

0. SQL revisited

7/24/13 6:31 PM

Selecting columns
Select a column from a relational table
SELECT anumber
FROM APPLICANT;

Select few columns from a relational table


SELECT fname, lname
FROM APPLICANT;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

17

0. SQL revisited

7/24/13 6:31 PM

Selecting expressions on the columns


Select a result of expression on the columns
SELECT 'Full name: ' || fname ||' ' || lname
FROM APPLICANT;

Select a result of expression on the columns and add an alias


name to a result
SELECT 'Full name: ' ||fname ||' ' || lname FULL_NAME
FROM APPLICANT;

Select a result of a function applied to a column and add an


alias name to a result
SELECT fname, TO_UPPER(lname) AS UPPER_LNAME
FROM APPLICANT;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

18

0. SQL revisited

7/24/13 6:31 PM

Creating alias names for selected columns


Select a result of expression on a column and add an alias
name to a result
SELECT dob + 1 A_DAY_AFTER_DOB
FROM APPLICANT;

Select the current date as many times as many rows are in a


relational table APPLICANT;
SELECT 'Today is: ', SYSDATE
FROM APPLICANT;

Select the current date just one time


SELECT 'Today is: ', SYSDATE
FROM DUAL;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

19

0. SQL revisited

7/24/13 6:31 PM

Select all values and distinct/unique values


Select all values from the columns no matter if they are
repeated or not, e.g. select all pairs of values from the
columns CITY, STATE in a relational table APPLICANT
SELECT city, state
FROM APPLICANT;

Select all distinct/unique values from the columns e.g. select


all distinct/unique pairs of values from the columns CITY,
STATE in a relational table APPLICANT
SELECT DISTINCT city, state
FROM APPLICANT;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

20

0. SQL revisited

7/24/13 6:31 PM

WHERE clause with a simple condition


Find the names and cities of employers located in Sydney
SELECT ename, city
FROM EMPLOYER
WHERE city = 'Sydney';
Find the names and cities of employers not located in Sydney
SELECT ename, city
FROM EMPLOYER
WHERE city <> 'Sydney';
Find the titles, salaries of the positions with a salary > 10000
SELECT title, salary
FROM POSITION
WHERE salary > 10000;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

21

0. SQL revisited

7/24/13 6:31 PM

WHERE clause with a simple condition


Find the names and cities of employers located in Sydney
or in Perth
SELECT ename, city
FROM EMPLOYER
WHERE city IN ('Sydney', 'Perth');
Find the names and cities of employers not located in
Sydney and not located in Perth
SELECT ename, city
FROM EMPLOYER
WHERE city NOT IN ('Sydney', 'Perth');

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

22

0. SQL revisited

7/24/13 6:31 PM

WHERE clause with a simple condition


Find the names and cities of employers located in a city
whose name starts from a letter 'S'
SELECT ename, city
FROM EMPLOYER
WHERE city LIKE 'S%';
Find the names and cities of employers not located in a city
whose name ends with 'ne'
SELECT ename, city
FROM EMPLOYER
WHERE city NOT LIKE '%ne';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

23

0. SQL revisited

7/24/13 6:31 PM

WHERE clause with a simple condition


Find the first and the last names and emails of applicants
who do not have an email address
SELECT fname, lname, email
FROM APPLICANT
WHERE email IS NULL;
Find the first and the last names and emails of applicants
who have an email address
SELECT fname, lname, email
FROM APPLICANT
WHERE email IS NOT NULL;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

24

0. SQL revisited

7/24/13 6:31 PM

WHERE clause with a Boolean expression


Find the names and cities of employers located in Sydney
or in Perth
SELECT ename, city
FROM EMPLOYER
WHERE city = 'Sydney' OR city = 'Perth';
Find the names and cities of employers not located in
Sydney and not located in Perth
SELECT ename, city
FROM EMPLOYER
WHERE city <>'Sydney' AND city <> 'Perth';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

25

0. SQL revisited

7/24/13 6:31 PM

Queries with string comparisons


Find the names, and cities of employers located in Sydney
while we do not know which form 'Sydney' or 'SYDNEY' is
stored in a database.
SELECT ename, city
FROM EMPLOYER
WHERE UPPER(city) = 'SYDNEY';
Find the names of employers located in a city whose name
lexicographically follow a name 'Sydney'.
SELECT ename, city
FROM EMPLOYER
WHERE city > 'Sydney';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

26

0. SQL revisited

7/24/13 6:31 PM

Sorting by an attribute
Find the names of employers sorted in ascending order
SELECT ename
FROM EMPLOYER
ORDER BY ename;
Find the names of employers sorted in descending order
SELECT ename
FROM EMPLOYER
ORDER BY 1 DESC;
Find the names and cities of employers sorted by cities
SELECT ENAME, city
FROM EMPLOYER
ORDER BY city ASC;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

27

0. SQL revisited

7/24/13 6:31 PM

Sorting by an attribute
Find the full information about of employers sorted in
descending order of emails; employers who have no emails
must be listed last
SELECT *
FROM EMPLOYER
ORDER BY email NULLS LAST;
Find the first and last names and emails of applicants
sorted in ascending order of emails; applicants who have
no emails must be listed first
SELECT fname, lname, email
FROM APPLICANT
ORDER BY 3 NULLS FIRST;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

28

0. SQL revisited

7/24/13 6:31 PM

Sorting by the attributes


Find the names and cities of employers sorted in
descending order by city and in ascending order by name
SELECT ename, city
FROM EMPLOYER
ORDER BY city DESC, ename ASC;
Find the last and first names and emails of applicants
sorted in ascending order of last and first names and
descending order of emails; applicants who have no emails
must be listed last
SELECT lname, fname, email
FROM APPLICANT
ORDER BY 1 ASC, 2 ASC,3 DESC NULLS LAST;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

29

0. SQL revisited

7/24/13 6:31 PM

Simple GROUP BY clause


Find the distinct cities of all applicants
SELECT city
FROM APPLICANT
GROUP BY city;
Find and sort in ascending order distinct cities of all
applicants
SELECT city
FROM APPLICANT
GROUP BY city
ORDER BY city;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

30

0. SQL revisited

7/24/13 6:31 PM

GROUP BY clause with COUNT(*)


Find the cities together with the total number of applicants
living in each city
SELECT city, COUNT(*)
FROM APPLICANT
GROUP BY CITY;
Find the years of birth and the total number of applicants
born in each year, order the results in descending order of
years
SELECT TO_CHAR(dob,'YYYY'), COUNT(*)
FROM APPLICANT
GROUP BY TO_CHAR(dob,'YYYY')
ORDER BY TO_CHAR(dob,'YYYY') DESC;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

31

0. SQL revisited

7/24/13 6:31 PM

GROUP BY clause with SUM


Find a title and total salaries for all positions with the same
title
SELECT title, SUM(salary)
FROM POSITION
GROUP BY title;
Find the total number of distinct skills possessed by the
applicants
SELECT SUM(COUNT(DISTINCT sname))
FROM SPOSSESSED
GROUP BY sname;
SELECT COUNT(DISTINCT sname)
FROM SPOSSESSED;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

32

0. SQL revisited

7/24/13 6:31 PM

GROUP BY clause with MAX


Find the cities and the total number of applicants from each
city
SELECT city, COUNT(*)
FROM APPLICANT
GROUP BY city;
Find the largest number of applicants from a city
SELECT MAX(COUNT(*))
FROM APPLICANT
GROUP BY city;
Find the cities with the largest number of applicants
SELECT MAX( TO_CHAR(COUNT(*))|| ' ' ||city )
FROM APPLICANT
GROUP BY city;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

33

0. SQL revisited

7/24/13 6:31 PM

Simple HAVING clause


Find if total number of applicants is greater than 15;
SELECT COUNT(*), 'Yes'
FROM APPLICANT
HAVING COUNT(*) > 15;
Find if an average salary for all positions is greater than
1000
SELECT AVG(salary), 'Yes'
FROM POSITION
HAVING AVG(salary) > 1000;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

34

0. SQL revisited

7/24/13 6:31 PM

HAVING clause with GROUP BY clause


Find numbers of positions with salary greater than 1000
SELECT pnumber
FROM POSITION
GROUP BY pnumber, salary
HAVING salary > 1000;
Of course SELECT statement below is much better ;)
SELECT pnumber
FROM POSITION
WHERE salary > 1000;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

35

0. SQL revisited

7/24/13 6:31 PM

HAVING clause with GROUP BY clause


Find the cities with more than 2 applicants
SELECT city
FROM APPLICANT
GROUP BY city
HAVING COUNT(*) > 2;
Find the names of skills possessed by at least 3 applicants
at level 9 or higher
SELECT sname, count(*), slevel
FROM SPOSSESSED
GROUP BY sname, slevel
HAVING COUNT(*) >= 3 and slevel >= 9;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

36

0. SQL revisited

7/24/13 6:31 PM

GROUP BY and HAVING clause with SUM


Find the titles of positions with the total salary greater than
5000
SELECT title, SUM(salary)
FROM POSITION
GROUP BY title
HAVING SUM(salary) > 5000;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

37

0. SQL revisited

7/24/13 6:31 PM

Comparison of join clauses


Find the titles of all positions offered by employers from
Sydney
SELECT POSITION.title
FROM POSITION JOIN EMPLOYER
ON POSITION.ename = EMPLOYER.ename
WHERE EMPLOYER.city = 'Sydney';
SELECT POSITION.title
FROM POSITION JOIN EMPLOYER USING (ename)
WHERE EMPLOYER.city = 'Sydney';
SELECT POSITION.title
FROM POSITION NATURAL JOIN EMPLOYER
WHERE EMPLOYER.city = 'Sydney';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

38

0. SQL revisited

7/24/13 6:31 PM

[INNER] JOIN ON clause


Find the applicants who in 1999 applied for a position with
salary greater than 1000, list the names of applicants and
titles of positions
SELECT APPLICANT.fname, APPLICANT.lname,POSITION.title
FROM APPLICANT JOIN APPLIES
ON APPLICANT.anumber = APPLIES.anumber
JOIN POSITION
ON APPLIES.pnumber = POSITION.pnumber
WHERE TO_CHAR(APPLIES.appdate, 'YYYY') = '1999' AND
POSITION.salary > 1000;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

39

0. SQL revisited

7/24/13 6:31 PM

[INNER] JOIN ON clause


Find the first and the last name of applicants who where
employed at least one time by an employer who offers a
position with salary greater than 1000
SELECT DISTINCT APPLICANT.fname, APPLICANT.lname
FROM APPLICANT JOIN EMPLBY
ON APPLICANT.anumber = EMPLBY.anumber
JOIN EMPLOYER
ON EMPLBY.ename = EMPLOYER.ename
JOIN POSITION
ON EMPLOYER.ename = POSITION.ename
WHERE POSITION.salary > 1000;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

40

0. SQL revisited

7/24/13 6:31 PM

CROSS JOIN clause


Find all possible combinations of applicants and positions
on offer, list the full names of applicants and titles of
positions
SELECT APPLICANT.fname, APPLICANT.lname,
POSITION.title
FROM APPLICANT CROSS JOIN POSITION;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

41

0. SQL revisited

7/24/13 6:31 PM

Comparison of join clauses


Find the titles of all positions offered by employers from
Sydney
SELECT POSITION.title
FROM POSITION JOIN EMPLOYER
ON POSITION.ename = EMPLOYER.ename
WHERE EMPLOYER.city = 'Sydney';
SELECT POSITION.title
FROM POSITION, EMPLOYER
WHERE POSITION.ename = EMPLOYER.ename
EMPLOYER.city = 'Sydney';

Janusz R. Getta

AND

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

42

0. SQL revisited

7/24/13 6:31 PM

FROM clause join


Find the applicants who in 1999 applied for a position with
salary greater than 1000, list the names of applicants and
titles of positions
SELECT APPLICANT.fname, APPLICANT.lname,POSITION.title
FROM APPLICANT, APPLIES, POSITION
WHERE APPLICANT.anumber = APPLIES.anumber AND
APPLIES.pnumber = POSITION.pnumber AND
TO_CHAR(APPLIES.appdate, 'YYYY') = '1999' AND
POSITION.salary > 1000;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

43

0. SQL revisited

7/24/13 6:31 PM

FROM clause join


Find the applicants who apply for a position offered by their
former employer, list the first and last name of each
applicant
SELECT APPLICANT.fname, APPLICANT.lname
FROM APPLICANT, APPLIES, POSITION, EMPLOYER, EMPLBY
WHERE APPLICANT.anumber = APPLIES.anumber AND
APPLIES.pnumber = POSITION.pnumber AND
EMPLOYER.ename = POSITION.ename AND
EMPLOYER.ename = EMPLBY.ename AND
EMPLBY.anumber = APPLICANT.anumber AND
EMPLBY.todate IS NOT NULL;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

44

0. SQL revisited

7/24/13 6:31 PM

FROM clause join - abbreviations


Find the applicants who apply for a position offered by their
former employer, list the first and last name of each
applicant
SELECT APPLICANT.fname, APPLICANT.lname
FROM APPLICANT at, APPLIES as, POSITION p, EMPLOYER er,
EMPLBY ey
WHERE at.anumber = as.anumber AND
as.pnumber = p.pnumber AND
er.ename = p.ename AND
er.ename = ey.ename
ey.anumber = at.anumber AND
ey.todate IS NOT NULL;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

45

0. SQL revisited

7/24/13 6:31 PM

FROM clause join - GROUP BY/HAVING


Find the numbers and titles of positions that have at least 4
applicants
SELECT POSITION.pnumber, POSITION.title
FROM POSITION, APPLIES
WHERE POSITION.pnumber = APPLIES.pnumber
GROUP BY POSITION.pnumber, POSITION.title
HAVING COUNT(*) >= 4;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

46

0. SQL revisited

7/24/13 6:31 PM

Comparison of join clauses


Find the names of employers from Sydney and the titles of all
positions offered by the employers, include the names of
employers who at the moment offer no positions
SELECT EMPLOYER.ename, POSITION.title
FROM EMPLOYER LEFT OUTER JOIN POSITION
ON POSITION.ename = EMPLOYER.ename
WHERE EMPLOYER.city = 'Sydney';
SELECT EMPLOYER.ename, POSITION.title
FROM POSITION RIGHT OUTER JOIN EMPLOYER
ON POSITION.ename = EMPLOYER.ename
WHERE EMPLOYER.city = 'Sydney';
SELECT EMPLOYER.ename, POSITION.title
FROM POSITION FULL OUTER JOIN EMPLOYER
ON POSITION.ename = EMPLOYER.ename
WHERE EMPLOYER.city = 'Sydney';
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

47

0. SQL revisited

7/24/13 6:31 PM

LEFT OUTER JOIN ON clause


Find the applicants who applied for a position, list the
names of applicants and titles of positions, include the
names applicants that submitted no applications
SELECT APPLICANT.fname, APPLICANT.lname,POSITION.title
FROM APPLICANT LEFT OUTER JOIN
( APPLIES JOIN POSITION
ON APPLIES.pnumber = POSITION.pnumber)
ON APPLICANT.anumber = APPLIES.anumber;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

48

0. SQL revisited

7/24/13 6:31 PM

RIGHT OUTER JOIN ON clause


Find the applicants who applied for a position, list the
names of applicants and titles of positions, include the
titles of positions that have no applicants
SELECT APPLICANT.fname, APPLICANT.lname,POSITION.title
FROM (APPLICANT JOIN APPLIES
ON APPLICANT.anumber = APPLIES.anumber)
RIGHT OUTER JOIN POSITION
ON APPLIES.pnumber = POSITION.pnumber;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

49

0. SQL revisited

7/24/13 6:31 PM

FULL OUTER JOIN ON clause


Find the applicants and employers located in the same
states and in the same cities, list the full names applicants,
names of employers, and names of cities and states, list the
names of applicants living in the cities and states that host
no employers, list the the names of cities and states that
host no applicants
SELECT APPLICANT.fname, APPLICANT.lname,
EMPLOYER.ename, EMPLOYER.city, EMPLOYER. state
FROM APPLICANT FULL OUTER JOIN EMPLOYER
ON APPLICANT.city = EMPLOYER.city AND
APPLICANT.state = EMPLOYER.state;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

50

0. SQL revisited

7/24/13 6:31 PM

Self join of one relational table


Find the pairs of applicants who live in the same state and in
the same city, list the full names of applicants, and do not list
reverse duplicates of the pairs found,
SELECT a1.fname, a1.lname, a2.fname, a2.lname
FROM APPLICANT a1 JOIN APPLICANT a2
ON a1.state = a2.state AND
a1.city = a2. City AND
a1.anumber > a2.anumber;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

51

0. SQL revisited

7/24/13 6:31 PM

Self join of one relational table


Find the names and levels of all skills required for a skill Java
programming
SELECT SREQUIRED.requires, SREQUIRED.slevel
FROM SREQUIRED
WHERE sname = 'Java programming';

Find the names and levels of skills required by the skills


required for a skill Java programming
SELECT s2.requires, s2.slevel
FROM SREQUIRED s1 JOIN SREQUIRED s2
ON s1.requires = s2.sname
WHERE s1.sname = 'Java programming';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

52

0. SQL revisited

7/24/13 6:31 PM

Self join of few relational tables


Find the triples of applicants who apply for the same position,
list the numbers of applicants, do not list the same applicants
more than once in a triple
SELECT s1.anumber, s2.anumber, s3.anumber
FROM APPLIES s1 JOIN APPLIES s2
ON s1.pnumber = s2.pnumber AND
s1.anumber > s2.anumber
JOIN APPLIES s3
ON s2.pnumber = s3.pnumber AND
s2.anumber > s3.anumber;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

53

0. SQL revisited

7/24/13 6:31 PM

Single-row, single column subquery


Find a location of employer who offers a position number 7,
list a city and state
SELECT city, state
FROM EMPLOYER
WHERE ename = ( SELECT ename
FROM POSITION
WHERE pnumber = 7 );

Find the first and the last name of an applicant number 7 who
applied for a position 7 in 2000
SELECT fname, lname
FROM APPLICANT
WHERE anumber = ( SELECT anumber
FROM APPLIES
WHERE pnumber = 7 AND anumber = 7 AND
TO_CHAR(appdate, 'YYYY') = 2000 );
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

54

0. SQL revisited

7/24/13 6:32 PM

Single-row, single column subquery


Find the numbers, titles, and salaries of all positions with the
largest salary
SELECT pnumber, title, salary
FROM POSITION
WHERE salary = ( SELECT MAX(salary)
FROM POSITION );

Find the numbers, titles, and salaries of all positions with the
salaries lower that the largest salary
SELECT pnumber, title, salary
FROM POSITION
WHERE salary < ( SELECT MAX(salary)
FROM POSITION );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

55

0. SQL revisited

7/24/13 6:32 PM

Single-row, single column subquery


Find the numbers, titles, and salaries of all positions with the
second largest salary
SELECT pnumber, title, salary
FROM POSITION
WHERE salary = ( SELECT MAX(salary)
FROM POSITION
WHERE salary < ( SELECT MAX(salary)
FROM POSITION ) );

Find the locations of all employers other than employer which


offers position number 7, list the names of cities and states
SELECT DISTINCT city, state
FROM EMPLOYER
WHERE ename != ( SELECT ename
FROM POSITION
WHERE pnumber = 7 );
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

56

0. SQL revisited

7/24/13 6:32 PM

Single-row, single column subquery


Find the employers who offer the largest number of positions,
list the names of employers and the total number of positions
offered by each one of them
SELECT ename, COUNT(*)
FROM POSITION
GROUP BY ename
HAVING COUNT(*) = ( SELECT MAX(COUNT(*))
FROM POSITION
GROUP BY ename );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

57

0. SQL revisited

7/24/13 6:32 PM

Single-row, single column subquery


Find the employers who offer the positions with the largest
total salary, list the names of employers and the total salaries
SELECT ename, SUM(salary)
FROM POSITION
GROUP BY ename
HAVING SUM(salary) = ( SELECT MAX(SUM(salary))
FROM POSITION
GROUP BY ename );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

58

0. SQL revisited

7/24/13 6:32 PM

Multiple-row, single column subquery


Find the titles of all positions offered by employers from
Sydney
SELECT title
FROM POSITION
WHERE ename IN ( SELECT ename
FROM EMPLOYER
WHERE city = 'Sydney' );

Find the titles of all positions offered by employers from


Sydney
SELECT title
FROM POSITION
WHERE ename = ANY ( SELECT ename
FROM EMPLOYER
WHERE city = 'Sydney' );
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

59

0. SQL revisited

7/24/13 6:32 PM

Transition from join to subqueries


Find the titles of all positions offered by employers from
Sydney
SELECT POSITION.title
FROM POSITION JOIN EMPLOYER
ON POSITION.ename = EMPLOYER.ename
WHERE EMPLOYER.city = 'Sydney';
SELECT POSITION.title
FROM POSITION
WHERE POSITION.ename IN
( SELECT EMPLOYER.ename
FROM EMPLOYER
WHERE EMPLOYER.city = 'Sydney' );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

60

0. SQL revisited

7/24/13 6:32 PM

Multiple-row, single column subquery


Find the applicants who in 1999 applied for a position with
salary greater than 1000, list the first and last names of
applicants
SELECT fname, lname
FROM APPLICANT
WHERE anumber IN
( SELECT anumber
FROM APPLIES
WHERE TO_CHAR(appdate, 'YYYY') = '1999' AND
pnumber IN
( SELECT pnumber
FROM POSITION
WHERE salary > 1000 ) );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

61

0. SQL revisited

7/24/13 6:32 PM

Multiple-row, single column subquery


Find the numbers, titles, and salaries of all positions with the
largest salary
SELECT pnumber, title, salary
FROM POSITION
WHERE salary >= ALL ( SELECT salary
FROM POSITION );

Find the numbers, titles, and salaries of all positions with the
smallest salary
SELECT pnumber, title, salary
FROM POSITION
WHERE salary <= ALL ( SELECT salary
FROM POSITION );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

62

0. SQL revisited

7/24/13 6:32 PM

Multiple-column subquery
Find the applicants and employers located in the same
states and in the same cities, list the full names applicants,
and names of states and cities
SELECT fname, lname, state, city
FROM APPLICANT
WHERE (state, city) IN ( SELECT state, city
FROM EMPLOYER );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

63

0. SQL revisited

7/24/13 6:32 PM

Multiple-column subquery
Find the applicants such that there exist another applicant
which is living in the same city and in the same state, list
applicant number and full name, city and state
SELECT anumber, fname, lname, city, state
FROM APPLICANT
WHERE (city, state) IN ( SELECT city, state
FROM APPLICANT
GROUP BY city, state
HAVING COUNT(*) > 1 );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

64

0. SQL revisited

7/24/13 6:32 PM

Subqueries in expressions
Find the numbers, titles, and salaries of positions whose
salary is less then 2 minimal salaries
SELECT pnumber, title, salary
FROM POSITION
WHERE salary < 2 * ( SELECT MIN(salary) FROM POSITION );

Find the total number employers and the total number of


positions, list both values in one row
SELECT ( SELECT COUNT(*) FROM EMPLOYER ) Employers,
( SELECT COUNT(*) FROM POSITION ) Positions
FROM DUAL;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

65

0. SQL revisited

7/24/13 6:32 PM

Correlation variable
Find the names employers who offer more than 1 position
SELECT ename
FROM POSITION
GROUP BY ename
HAVING COUNT(*) > 1;
SELECT DISTINCT ename
FROM POSITION P
WHERE 1 < ( SELECT COUNT(*)
FROM POSITION
WHERE POSITION.ename = P.ename);

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

66

0. SQL revisited

7/24/13 6:32 PM

Correlation variable
Find the numbers, titles, and salaries of positions that have
another position with the same title and different salary
SELECT pnumber, title
FROM POSITION P
WHERE title IN ( SELECT title
FROM POSITION
WHERE POSITION.salary != P.salary );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

67

0. SQL revisited

7/24/13 6:32 PM

EXISTS clause
Find the numbers, titles, and salaries of positions that have
another position with the same title and different salary
SELECT pnumber, title
FROM POSITION P
WHERE EXISTS ( SELECT pnumber
FROM POSITION
WHERE POSITION.title = P.title AND
POSITION.salary != P.salary );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

68

0. SQL revisited

7/24/13 6:32 PM

Correlation variable
Find the applicants such that there exist another applicant
which is living in the same city and in the same state, list
applicant number and full name, city and state
SELECT anumber, fname, lname, city, state
FROM APPLICANT A
WHERE (city, state) IN
( SELECT city, state
FROM APPLICANT
WHERE APPLICANT.anumber != A.anumber );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

69

0. SQL revisited

7/24/13 6:32 PM

EXISTS clause
Find the applicants such that there exist another applicant
which is living in the same city and in the same state, list
applicant number and full name, city and state
SELECT anumber, fname, lname, city, state
FROM APPLICANT A
WHERE EXISTS ( SELECT 1
FROM APPLICANT
WHERE APPLICANT.anumber != A.anumber
APPLICANT.city = A.city AND
APPLICANT.state = A.state );

Janusz R. Getta

AND

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

70

0. SQL revisited

7/24/13 6:32 PM

NOT EXISTS clause


Find the employers who do not offer any positions, list the
names of employers, cities and states
SELECT ename, city, state
FROM EMPLOYER
WHERE NOT EXISTS
( SELECT *
FROM POSITION
WHERE POSITION.ename = EMPLOYER.ename );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

71

0. SQL revisited

7/24/13 6:32 PM

Query decomposition
Find the numbers and titles of the positions offered by
employers from Sydney
Find employers from Sydney
( SELECT ename
FROM EMPLOYER
WHERE city = 'Sydney' ) SYDNEYEMP

Find the numbers and titles of the positions offered by


employers from Sydney
SELECT POSITION.pnumber, POSITION.title
FROM POSITION JOIN SYDNEYEMP
ON POSITION.ename = SYDNEYEMP.ename;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

72

0. SQL revisited

7/24/13 6:32 PM

Inline view
Find the numbers and titles of the positions offered by
employers from Sydney
SELECT POSITION.pnumber, POSITION.title
FROM POSITION JOIN ( SELECT ename
FROM EMPLOYER
WHERE city = 'Sydney' ) SYDNEYEMP
ON POSITION.ename = SYDNEYEMP.ename;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

73

0. SQL revisited

7/24/13 6:32 PM

Query decomposition
Find the full names of applicants and titles of the positions
they apply for in 2008
Find full information about applicants and positions they
apply for
( SELECT *
FROM APPLICANT JOIN APPLIES
ON APPLICANT.anumber = APPLIES.anumber
JOIN POSITION
ON APPLIES.pnumber = POSITION.pnumber )
APOS

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

74

0. SQL revisited

7/24/13 6:32 PM

Query decomposition
Find the full names of applicants and titles of the positions
they apply for in 2000
Find the full names of applicants and titles of the positions
they apply for in 2000
SELECT APOS.fname, APOS.lname
FROM APOS
WHERE TO_CHAR(APOS.appdate, 'YYYY') = '2000';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

75

0. SQL revisited

7/24/13 6:32 PM

Inline view
Find the full names of applicants and titles of the positions
they apply for in 2000
SELECT APOS.fname, APOS.lname
FROM ( SELECT *
FROM APPLICANT JOIN APPLIES
ON APPLICANT.anumber = APPLIES.anumber
JOIN POSITION
ON APPLIES.pnumber = POSITION.pnumber ) APOS
WHERE TO_CHAR(APOS.appdate, 'YYYY') = '2000';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

76

0. SQL revisited

7/24/13 6:32 PM

Inline view
Find the employers who offer more than one position, list the
names of employers and total number of positions on offer
SELECT POSCOUNT.ename, POSCOUNT.totpos
FROM ( SELECT ename, COUNT(*) totpos
FROM POSITION
GROUP BY ename ) POSCOUNT
WHERE POSCOUNT.totpos > 1;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

77

0. SQL revisited

7/24/13 6:32 PM

"It is all about join"


Find full information about employers from Sydney, New
South Wales
SELECT *
FROM EMPLOYER NATURAL JOIN
( SELECT 'Sydney' city, 'New South Wales' state
FROM DUAL );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

78

0. SQL revisited

7/24/13 6:32 PM

Union
Find the positions offered by University of Queensland and
University of New South Wales, list the titles of positions,
names of employers, and salaries in ascending order of
SELECT title, ename, salary
salaries
FROM POSITION
WHERE ename = 'University of Queensland'
UNION
SELECT title, ename, salary
FROM POSITION
WHERE ename = 'University of New South Wales'
ORDER BY salary ASC;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

79

0. SQL revisited

7/24/13 6:32 PM

Union
Find the title of positions offered by University of Queensland
and University of New South Wales, do not remove the
duplicates
SELECT title
FROM POSITION
WHERE ename = 'University of Queensland'
UNION ALL
SELECT title
FROM POSITION
WHERE ename = 'University of New South Wales';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

80

0. SQL revisited

7/24/13 6:32 PM

Union
Find the names of employer and total number of positions
offered by each employer, list the names of employer which
do not offer any positions with zero and order the results in
ascending way by total number of positions
SELECT ename, COUNT(*) total
FROM POSITION
GROUP BY ename
UNION
SELECT ename, 0
FROM EMPLOYER
WHERE ename NOT IN ( SELECT ename
FROM POSITION )
ORDER BY total ASC;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

81

0. SQL revisited

7/24/13 6:32 PM

Intersection
Find the titles of the same positions offered by University of
Queensland and University of New South Wales,
SELECT title
FROM POSITION
WHERE ename = 'University of Queensland'
INTERSECT
SELECT title
FROM POSITION
WHERE ename = 'University of New South Wales';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

82

0. SQL revisited

7/24/13 6:32 PM

Minus
Find the titles of the positions offered by University of New
South Wales and not University of Queensland and
SELECT title
FROM POSITION
WHERE ename = 'University of New South Wales'
MINUS
SELECT title
FROM POSITION
WHERE ename = 'University of Queensland';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

83

0. SQL revisited

7/24/13 6:32 PM

Minus
Find the names of employers which do not offer any
positions
SELECT ename
FROM EMPLOYER
MINUS
SELECT ename
FROM POSITION;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

84

0. SQL revisited

7/24/13 6:32 PM

IS [NOT] NULL
Find all employers who do not have email addresses, list a
name of employer, city and state
SELECT ename, city, state
FROM EMPLOYER
WHERE email IS NULL;

Find all positions that have bonus and extras, list the
numbers, titles, salaries, bonuses, and extras of the positions
selected
SELECT pnumber, title, salary, bonus, extras
FROM POSITION
WHERE bonus IS NOT NULL AND
extras IS NOT NULL;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

85

0. SQL revisited

7/24/13 6:32 PM

NULLs returned by subqueries


Employee

Find all employees who are not managers

e# | name | manager#
e# | name | manager#
--------------------10 | John | NULL
20 | Peter | 10
30 | Mary | 10
40 | Mike | 20
50 | Kate | 20
60 | Greg | 50
70 | Phil | 50
Janusz R. Getta

10
20
40

30
50

60

70

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

86

0. SQL revisited

7/24/13 6:32 PM

NULLs returned by subqueries


Employee

Find all employees who are not managers

e# | name | manager#
10

SELECT *
FROM Employee
WHERE e#

20

NOT IN
( SELECT manager#
FROM Employee );
Janusz R. Getta

40

30
50

60

70

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

87

0. SQL revisited

7/24/13 6:32 PM

NULLs returned by subqueries


Find all employees who are not managers
SELECT *
FROM Employee
WHERE e# NOT IN
( SELECT manager#
FROM Employee );
No rows selected !!!
NULL value means "any number" !!!

Janusz R. Getta

manager#
-------NULL !!!
10
10
20
20
50
50

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

88

0. SQL revisited

7/24/13 6:32 PM

NULLs returned by subqueries


Find all employees who are not managers
SELECT *
FROM Employee
WHERE e# NOT IN
( SELECT manager#
FROM Employee
WHERE manager# IS NOT NULL );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

89

0. SQL revisited

7/24/13 6:32 PM

Queries with three-valued logic


NOT

|TRUE
|FALSE

|FALSE
|TRUE

|UNKNOWN
|UNKNOWN

OR
TRUE
FALSE
UNKNOWN

|TRUE
|TRUE
|TRUE
|TRUE

|FALSE
|TRUE
|FALSE
|UNKNOWN

|UNKNOWN
|TRUE
|UNKNOWN
|UNKNOWN

AND
TRUE
FALSE
UNKNOWN

|TRUE
|TRUE
|FALSE
|UNKNOWN

|FALSE
|FALSE
|FALSE
|FALSE

|UNKNOWN
|UNKNOWN
|FALSE
|UNKNOWN

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

90

0. SQL revisited

7/24/13 6:32 PM

Queries with three-valued logic


Employee
e# | name | manager#
e# | name | manager#
--------------------10 | John | NULL
20 | Peter | 10
30 | Mary | 10
40 | Mike | 20
50 | Kate | 20
60 | Greg | 50
70 | Phil | 50
Janusz R. Getta

SELECT e#
FROM Employee
WHERE manager# = 10;

e#
-20
30

SELECT e#
e#
FROM Employee
-WHERE manager# <> 10; 40
50
Negation of UNKNOWN is 60
70
equal to UNKNOWN !!!

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

91

0. SQL revisited

7/24/13 6:32 PM

Query decomposition
Find the numbers and titles of the positions offered by
employers from Sydney
Find employers from Sydney
WITH SYDNEYEMP AS
( SELECT ename
FROM EMPLOYER
WHERE city = 'Sydney' )

Find the numbers and titles of the positions offered by


employers from Sydney
SELECT POSITION.pnumber, POSITION.title
FROM POSITION JOIN SYDNEYEMP
ON POSITION.ename = SYDNEYEMP.ename;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

92

0. SQL revisited

7/24/13 6:32 PM

Subquery factoring
Find the numbers and titles of the positions offered by
employers from Sydney
WITH SYDNEYEMP(employer_name) AS
( SELECT ename
FROM EMPLOYER
WHERE city = 'Sydney' )
SELECT POSITION.pnumber, POSITION.title
FROM POSITION JOIN SYDNEYEMP
ON POSITION.ename = SYDNEYEMP.employer_name;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

93

0. SQL revisited

7/24/13 6:32 PM

Query decomposition
Find the full names of applicants and titles of the positions
they apply for in 2008
Find full information about applicants and positions they
apply for
WITH APOS AS
( SELECT *
FROM APPLICANT JOIN APPLIES
ON APPLICANT.anumber = APPLIES.anumber
JOIN POSITION
ON APPLIES.pnumber = POSITION.pnumber )
SELECT APOS.fname, APOS.lname
FROM APOS
WHERE TO_CHAR(APOS.appdate, 'YYYY') = '2000';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

94

0. SQL revisited

7/24/13 6:32 PM

Subquery factoring
Find the full names of applicants and titles of the positions
they apply for in 2008
WITH APOS AS
( SELECT *
FROM APPLICANT JOIN APPLIES
ON APPLICANT.anumber = APPLIES.anumber
JOIN POSITION
ON APPLIES.pnumber = POSITION.pnumber )
SELECT APOS.fname, APOS.lname
FROM APOS
WHERE TO_CHAR(APOS.appdate, 'YYYY') = '2000';

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

95

0. SQL revisited

7/24/13 6:32 PM

Query decomposition
Find the employers who offer more than one position, list the
names of employers and total number of positions on offer
Find the total number of positions offered by each employer
WITH POSCOUNT(ename, totpos) AS
( SELECT ename, COUNT(*)
FROM POSITION
GROUP BY ename );
SELECT POSCOUNT.ename, POSCOUNT.totpos
FROM POSCOUNT
WHERE POSCOUNT.totpos > 1;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

96

0. SQL revisited

7/24/13 6:32 PM

Subquery factoring
Find the employers who offer more than one position, list the
names of employers and total number of positions on offer
WITH POSCOUNT(ename, totpos) AS
( SELECT ename, COUNT(*)
FROM POSITION
GROUP BY ename )
SELECT POSCOUNT.ename, POSCOUNT.totpos
FROM POSCOUNT
WHERE POSCOUNT.totpos > 1;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

97

0. SQL revisited

7/24/13 6:32 PM

UPDATE statement with a subquery in SET clause


Increase salaries of all positions by 10% of an average
amount of the current salaries
UPDATE POSITION
SET salary = salary + 0.1 * ( SELECT AVG(salary)
FROM POSITION );

Change the values of attributes title, salary, and


specification for position 1 to the same values as for
position 2
UPDATE POSITION
SET (title, salary, specification) =
( SELECT title, salary, specification
FROM POSITION
WHERE pnumber = 2 )
WHERE pnumber = 1;
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

98

0. SQL revisited

7/24/13 6:32 PM

UPDATE statement with a subquery in SET clause


Set a value of salary to 100000 and a value of bonus to 1000 in
a position number 1
UPDATE POSITION
SET salary = 100000, bonus = 1000
WHERE pnumber = 1;
UPDATE POSITION
SET (salary, bonus) = ( SELECT 100000, 1000
FROM DUAL )
WHERE pnumber = 1;

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

99

0. SQL revisited

7/24/13 6:32 PM

UPDATE statement with correlation variable


Change a value of each salary to an average value of all
salaries greater than the value to be changed, do it for all
salaries except the maximum salary
UPDATE POSITION P
SET salary = ( SELECT AVG(salary)
FROM POSITION
WHERE salary > P.salary )
WHERE salary != ( SELECT MAX(salary)
FROM POSITION );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

100

0. SQL revisited

7/24/13 6:32 PM

UPDATE statement with correlation variable


Increase by 10% salaries of all positions that need more than
2 skills
UPDATE POSITION P
SET salary = salary + 0.1* salary
WHERE 2 <= ( SELECT COUNT(*)
FROM SNEEDED
WHERE pnumber = P.pnumber );
UPDATE POSITION P
SET salary = salary + 0.1* salary
WHERE pnumber IN ( SELECT pnumber
FROM SNEEDED
GROUP BY pnumber
HAVING COUNT(*) >= 2 );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

101

0. SQL revisited

7/24/13 6:32 PM

DELETE statement with nested query


Delete employers from Sydney
Delete positions offered by employers from Sydney
Delete employment periods with employers from Sydney
DELETE POSITION
WHERE ename IN ( SELECT ename
FROM EMPLOYER
WHERE city = 'Sydney' );
DELETE EMPLBY
WHERE ename IN ( SELECT ename
FROM EMPLOYER
WHERE city = 'Sydney' );
DELETE EMPLOYER
WHERE city = 'Sydney';
Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

102

0. SQL revisited

7/24/13 6:32 PM

DELETE statement with correlation variable


Delete all position which require more than 2 skills
DELETE POSITION P
WHERE 2 <= ( SELECT COUNT(*)
FROM SNEEDED
WHERE pnumber = P.pnumber );

Delete all position which require the largest number of skills


DELETE POSITION P
WHERE ( SELECT MAX(COUNT(*))
FROM SNEEDED
GROUP BY pnumber ) =
( SELECT COUNT(*)
FROM SNEEDED
WHERE pnumber = P.pnumber );

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

103

0. SQL revisited

7/24/13 6:32 PM

References
Oracle Database SQL Language Reference
11g Release 2 (11.2)
http://docs.oracle.com/cd/E11882_01/server.112/e26088/toc.htm

Janusz R. Getta

CSCI317/MCS9317 Database Performance Tuning, SCSSE, Spring 2013

104

Potrebbero piacerti anche