Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SQL revisited
7/24/13 6:31 PM
SQL revisited
Janusz R. Getta
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
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]
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
0. SQL revisited
7/24/13 6:31 PM
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
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);
Janusz R. Getta
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
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
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';
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;
Janusz R. Getta
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
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
13
0. SQL revisited
7/24/13 6:31 PM
SELECT statement
SELECT statement consists of the following clauses:
Janusz R. Getta
PARTITION BY/
DIMENSION BY
RULES
ITERATE
14
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
15
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
16
0. SQL revisited
7/24/13 6:31 PM
Selecting columns
Select a column from a relational table
SELECT anumber
FROM APPLICANT;
Janusz R. Getta
17
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
18
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
19
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
20
0. SQL revisited
7/24/13 6:31 PM
21
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
22
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
23
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
24
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
25
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
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
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
28
0. SQL revisited
7/24/13 6:31 PM
29
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
30
0. SQL revisited
7/24/13 6:31 PM
31
0. SQL revisited
7/24/13 6:31 PM
32
0. SQL revisited
7/24/13 6:31 PM
33
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
34
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
35
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
36
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
37
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
38
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
39
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
40
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
41
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
AND
42
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
43
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
44
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
45
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
46
0. SQL revisited
7/24/13 6:31 PM
47
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
48
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
49
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
50
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
51
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
52
0. SQL revisited
7/24/13 6:31 PM
Janusz R. Getta
53
0. SQL revisited
7/24/13 6:31 PM
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
54
0. SQL revisited
7/24/13 6:32 PM
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
55
0. SQL revisited
7/24/13 6:32 PM
56
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
57
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
58
0. SQL revisited
7/24/13 6:32 PM
59
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
60
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
61
0. SQL revisited
7/24/13 6:32 PM
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
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
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
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 );
Janusz R. Getta
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
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
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
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
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
70
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
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
Janusz R. Getta
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
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
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
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
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
77
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
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
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
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
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
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
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
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
85
0. SQL revisited
7/24/13 6:32 PM
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
86
0. SQL revisited
7/24/13 6:32 PM
e# | name | manager#
10
SELECT *
FROM Employee
WHERE e#
20
NOT IN
( SELECT manager#
FROM Employee );
Janusz R. Getta
40
30
50
60
70
87
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
manager#
-------NULL !!!
10
10
20
20
50
50
88
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
89
0. SQL revisited
7/24/13 6:32 PM
|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
90
0. SQL revisited
7/24/13 6:32 PM
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 !!!
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' )
Janusz R. Getta
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
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
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
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
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
97
0. SQL revisited
7/24/13 6:32 PM
98
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
99
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
100
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
101
0. SQL revisited
7/24/13 6:32 PM
102
0. SQL revisited
7/24/13 6:32 PM
Janusz R. Getta
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
104