SQL Lecture
SQL Lecture
P.J. McBrien
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 1 / 35
SQL and RA RA to SQL DML
SELECT b r a n c h . bname ,
a c c o u n t . no
FROM account , branch
WHERE a c c o u n t . s o r t c o d e=b r a n c h . s o r t c o d e
AND a c c o u n t . t y p e= ’ c u r r e n t ’
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 2 / 35
SQL and RA RA to SQL DML
✓ ✗ ✓
SELECT b r a n c h . bname , SELECT bname , SELECT bname ,
account . s o r t c o d e sortcode account . s o r t c o d e
FROM account , branch FROM account , branch FROM account , branch
WHERE a c c o u n t . s o r t c o d e= WHERE a c c o u n t . s o r t c o d e= WHERE a c c o u n t . s o r t c o d e=
branch . s o r t c o d e branch . s o r t c o d e branch . s o r t c o d e
AND a c c o u n t . t y p e= ’ c u r r e n t ’ AND t y p e= ’ c u r r e n t ’ AND t y p e= ’ c u r r e n t ’
✓
SELECT b r a n c h . ∗ ,
sortcode bname cash no
no
67 ’Strand’ 34005.00 100
FROM account , branch
34 ’Goodge St’ 8900.67 103
WHERE a c c o u n t . s o r t c o d e=
56 ’Wimbledon’ 94340.45 107
branch . s o r t c o d e
56 ’Wimbledon’ 94340.45 125
AND t y p e= ’ c u r r e n t ’
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 3 / 35
SQL and RA RA to SQL DML
A B
C D
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 4 / 35
SQL and RA RA to SQL DML
SELECT no
FROM account
EXCEPT
SELECT no
FROM movement
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 5 / 35
SQL and RA Joins
SQL Joins
‘Classic’ SQL Join Syntax
SELECT b r a n c h . ∗ , no , ty pe , cname , r a t e
FROM br a nc h , a c c o u n t
WHERE b r a n c h . s o r t c o d e =a c c o u n t . s o r t c o d e
SELECT b r a n c h . ∗ , no , ty pe , cname , r a t e
FROM b r a n c h JOIN a c c o u n t ON b r a n c h . s o r t c o d e =a c c o u n t . s o r t c o d e
SELECT ∗
FROM b r a n c h NATURAL JOIN a c c o u n t
SELECT b r a n c h . ∗ , no , ty pe , cname , r a t e
FROM b r a n c h JOIN a c c o u n t USING ( s o r t c o d e )
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 6 / 35
SQL and RA Joins
RA and SQL
RA Operator SQL Operator
π SELECT
σ WHERE
R1 × R2 FROM R1 , R2 or FROM R1 CROSS JOIN R2
R1 ⋉
⋊ R2 FROM R1 NATURAL JOIN R2
θ
R1 ⋉
⋊ R2 FROM R1 JOIN R2 ON θ
R1 − R2 R1 EXCEPT R2
R1 ∪ R2 R1 UNION R2
R1 ∩ R2 R1 INTERSECT R2
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 7 / 35
SQL and RA Joins
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 8 / 35
SQL and RA Joins
. . . and find out that not all DBMSs are the same
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 9 / 35
Bags and Sets Bags or Sets
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 10 / 35
Bags and Sets Bags or Sets
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 10 / 35
Bags and Sets Bags or Sets
Which SQL query requires the use of DISTINCT in order to avoid the
possibility of a bag being produced?
A B
SELECT ∗ SELECT s o r t c o d e
FROM branch FROM branch
WHERE cash >10000 WHERE cash >10000
C D
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 11 / 35
Bags and Sets Bags or Sets
Which SQL query requires the use of DISTINCT in order to avoid the
possibility of a bag being produced?
A B
SELECT ∗ SELECT b r a n c h . s o r t c o d e , ty pe , r a t e
FROM b r a n c h NATURAL JOIN FROM b r a n c h NATURAL JOIN
account account
C D
SELECT b r a n c h . s o r t c o d e , no SELECT b r a n c h . s o r t c o d e , no , c a s h
FROM b r a n c h NATURAL JOIN FROM b r a n c h NATURAL JOIN
account account
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 12 / 35
Bags and Sets Bags or Sets
If R and S are sets, which RA operator could produce a bag result if the
implementation did not check for duplicates?
A B C D
σR R∪S R−S R×S
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 13 / 35
Bags and Sets Bags or Sets
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 14 / 35
Bags and Sets Bags or Sets
Project-Select-Product Queries
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 15 / 35
Bags and Sets Bags or Sets
movement
mid no amount tdate
account
SELECT no 1000 100 2300.00 5/1/1999
no type cname rate sortcode
1001 101 4000.00 5/1/1999
FROM movement 1002 100 -223.45 8/1/1999
100 ’current’ ’McBrien, P.’ NULL 67
101 ’deposit’ ’McBrien, P.’ 5.25 67
EXCEPT 1004 107 -100.00 11/1/1999
103 ’current’ ’Boyd, M.’ NULL 34
1005 103 145.50 12/1/1999
SELECT no 1006 100 10.23 15/1/1999
107 ’current’ ’Poulovassilis, A.’ NULL 56
119 ’deposit’ ’Poulovassilis, A.’ 5.50 56
FROM account 1007 107 345.56 15/1/1999
125 ’current’ ’Bailey, J.’ NULL 56
1008 101 1230.00 15/1/1999
1009 119 5600.00 18/1/1999
A B C D
no no no no
100 100 100
101 101 100
103 103 101
107 107 107
119 119
125
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 16 / 35
Bags and Sets Bags or Sets
movement
mid no amount tdate
account
SELECT no 1000 100 2300.00 5/1/1999
no type cname rate sortcode
1001 101 4000.00 5/1/1999
FROM movement 1002 100 -223.45 8/1/1999
100 ’current’ ’McBrien, P.’ NULL 67
101 ’deposit’ ’McBrien, P.’ 5.25 67
EXCEPT ALL 1004 107 -100.00 11/1/1999
103 ’current’ ’Boyd, M.’ NULL 34
1005 103 145.50 12/1/1999
SELECT no 1006 100 10.23 15/1/1999
107 ’current’ ’Poulovassilis, A.’ NULL 56
119 ’deposit’ ’Poulovassilis, A.’ 5.50 56
FROM account 1007 107 345.56 15/1/1999
125 ’current’ ’Bailey, J.’ NULL 56
1008 101 1230.00 15/1/1999
1009 119 5600.00 18/1/1999
A B C D
no no no no
100 100 100
101 101 100
103 103 101
107 107 107
119 119
125
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 17 / 35
Bags and Sets Bags or Sets
Table Aliases
SELECT c u r r e n t a c c o u n t . cname ,
c u r r e n t a c c o u n t . no AS c u r r e n t n o ,
d e p o s i t a c c o u n t . no AS d e p o s i t n o
FROM a c c o u n t AS c u r r e n t a c c o u n t
JOIN a c c o u n t AS d e p o s i t a c c o u n t
ON c u r r e n t a c c o u n t . cname=d e p o s i t a c c o u n t . cname
AND c u r r e n t a c c o u n t . t y p e= ’ c u r r e n t ’
AND d e p o s i t a c c o u n t . t y p e= ’ d e p o s i t ’
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 18 / 35
Bags and Sets Bags or Sets
movement
mid no amount tdate
1000 100 2300.00 5/1/1999
1001 101 4000.00 5/1/1999
account 1002 100 -223.45 8/1/1999
no type cname rate sortcode 1004 107 -100.00 11/1/1999
100 ’current’ ’McBrien, P.’ NULL 67 1005 103 145.50 12/1/1999
101 ’deposit’ ’McBrien, P.’ 5.25 67 1006 100 10.23 15/1/1999
103 ’current’ ’Boyd, M.’ NULL 34 1007 107 345.56 15/1/1999
107 ’current’ ’Poulovassilis, A.’ NULL 56 1008 101 1230.00 15/1/1999
119 ’deposit’ ’Poulovassilis, A.’ 5.50 56 1009 119 5600.00 18/1/1999
125 ’current’ ’Bailey, J.’ NULL 56 fk
movement(no) ⇒ account.no
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 19 / 35
Bags and Sets Set Operations
Set Operations: IN
SELECT ∗
FROM account
WHERE t y p e= ’ c u r r e n t ’
AND no IN ( 1 0 0 , 1 0 1 )
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 20 / 35
Bags and Sets Set Operations
Set Operations: IN
SELECT ∗
FROM account
WHERE t y p e= ’ c u r r e n t ’
AND no IN ( 1 0 0 , 1 0 1 )
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 20 / 35
Bags and Sets Set Operations
movement
SELECT no mid no amount tdate
account
FROM account no type cname rate sortcode
1000 100 2300.00 5/1/1999
1001 101 4000.00 5/1/1999
WHERE t y p e= ’ c u r r e n t ’ 100 ’current’ ’McBrien, P.’ NULL 67
1002 100 -223.45 8/1/1999
101 ’deposit’ ’McBrien, P.’ 5.25 67
AND no NOT IN 103 ’current’ ’Boyd, M.’ NULL 34
1004 107 -100.00 11/1/1999
1005 103 145.50 12/1/1999
( SELECT no 107 ’current’ ’Poulovassilis, A.’ NULL 56
1006 100 10.23 15/1/1999
119 ’deposit’ ’Poulovassilis, A.’ 5.50 56
FROM movement 125 ’current’ ’Bailey, J.’ NULL 56
1007 107 345.56 15/1/1999
1008 101 1230.00 15/1/1999
WHERE amount >500) 1009 119 5600.00 18/1/1999
A B C D
no no no no
100 100 103 103
103 103 107 107
107 107 125
125
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 21 / 35
Bags and Sets Set Operations
A B C D
no no no no
100 100 103 103
103 103 107 107
107 107 125
125
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 21 / 35
Bags and Sets Set Operations
SELECT cname
SELECT cname
FROM account
FROM account
WHERE NOT EXISTS
WHERE cname NOT IN ≡ ( SELECT ∗
( SELECT cname
FROM a c c o u n t AS d e p o s i t a c c o u n t
FROM account
WHERE t y p e= ’ d e p o s i t ’
WHERE t y p e= ’ d e p o s i t ’ )
AND a c c o u n t . cname=cname )
cname
‘Boyd, M.’
‘Bailey, J.’
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 22 / 35
Bags and Sets Set Operations
SELECT no SELECT no
FROM account FROM account
EXCEPT ≡ WHERE NOT EXISTS ( SELECT no
SELECT no FROM movement
FROM movement WHERE no=a c c o u n t . no )
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 23 / 35
Bags and Sets Set Operations
SELECT bname
FROM branch
WHERE ’ c u r r e n t ’=ALL (SELECT t y p e
FROM account
WHERE b r a n c h . s o r t c o d e=a c c o u n t . s o r t c o d e )
SELECT bname
FROM branch
WHERE ’ d e p o s i t ’=SOME ( SELECT t y p e
FROM account
WHERE b r a n c h . s o r t c o d e=a c c o u n t . s o r t c o d e )
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 24 / 35
Bags and Sets Set Operations
branch account
sortcode bname cash no type cname rate sortcode
56 ’Wimbledon’ 94340.45 100 ’current’ ’McBrien, P.’ NULL 67
34 ’Goodge St’ 8900.67 101 ’deposit’ ’McBrien, P.’ 5.25 67
67 ’Strand’ 34005.00 103 ’current’ ’Boyd, M.’ NULL 34
107 ’current’ ’Poulovassilis, A.’ NULL 56
movement 119 ’deposit’ ’Poulovassilis, A.’ 5.50 56
mid no amount tdate 125 ’current’ ’Bailey, J.’ NULL 56
1000 100 2300.00 5/1/1999
1001 101 4000.00 5/1/1999
1002 100 -223.45 8/1/1999 key branch(sortcode)
1004 107 -100.00 11/1/1999 key branch(bname)
1005 103 145.50 12/1/1999 key movement(mid)
1006 100 10.23 15/1/1999 key account(no)
1007 107 345.56 15/1/1999 fk
1008 101 1230.00 15/1/1999 movement(no) ⇒ account(no)
1009 119 5600.00 18/1/1999 fk
account(sortcode) ⇒ branch(sortcode)
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 25 / 35
Bags and Sets Set Operations
Write an SQL query without using any negation (i.e. without the use of NOT or
EXCEPT) that list accounts with no movements on or before the 11-Jan-1999.
SELECT no
FROM account
WHERE ’ 11− j a n −1999 ’<ALL ( SELECT t d a t e
FROM movement
WHERE movement . no=a c c o u n t . no )
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 26 / 35
Bags and Sets Set Operations
Write an SQL query that lists the cname of customers that have every type of
account that appears in account
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 27 / 35
Bags and Sets Set Operations
Write an SQL query that lists the cname of customers that have every type of
account that appears in account
SELECT cname
FROM account
GROUP BY cname
HAVING COUNT( DISTINCT t y p e )=( SELECT COUNT( DISTINCT t y p e )
FROM account
);
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 27 / 35
Bags and Sets Set Operations
Write an SQL query that lists the cname of customers that have every type of
account that appears in account
SELECT cname
FROM account ,
( SELECT COUNT( DISTINCT t y p e ) AS n o t y p e s
FROM account
) AS a l l d a t a
GROUP BY cname , n o t y p e s
HAVING COUNT( DISTINCT t y p e )= a l l d a t a . n o t y p e s ;
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 27 / 35
Bags and Sets Set Operations
SELECT no
FROM account
WHERE 500>=ALL ( SELECT amount
FROM movement
WHERE a c c o u n t . no=movement . no )
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 28 / 35
Bags and Sets Set Operations
SELECT no
FROM account
WHERE 500>=ALL ( SELECT amount
FROM movement
WHERE a c c o u n t . no=movement . no )
≡
SELECT no
FROM account
WHERE NOT 500<SOME ( SELECT amount
FROM movement
WHERE a c c o u n t . no=movement . no )
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 28 / 35
Null
Null
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 35
Null
Null
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 35
Null
Null
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 35
Null
Null
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 35
Null
account
no type cname rate sortcode
100 ’current’ ’McBrien, P.’ NULL 67 SELECT no
101 ’deposit’ ’McBrien, P.’ 5.25 67
103 ’current’ ’Boyd, M.’ NULL 34
FROM account
107 ’current’ ’Poulovassilis, A.’ NULL 56 WHERE rate=NULL
119 ’deposit’ ’Poulovassilis, A.’ 5.50 56
125 ’current’ ’Bailey, J.’ NULL 56
A B C D
no no no no
100 100 101
101 103 119
103 107
107 125
119
125
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 30 / 35
Null
account
no type cname rate sortcode SELECT no
100 ’current’ ’McBrien, P.’ NULL 67
101 ’deposit’ ’McBrien, P.’ 5.25 67 FROM account
103 ’current’ ’Boyd, M.’ NULL 34 WHERE rate=null
107 ’current’ ’Poulovassilis, A.’ NULL 56
119 ’deposit’ ’Poulovassilis, A.’ 5.50 56 OR rate<>null
125 ’current’ ’Bailey, J.’ NULL 56
A B C D
no no no no
100 100 101
101 103 119
103 107
107 125
119
125
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 31 / 35
Null 3VL
AND
P1 AND P2 P2
TRUE UNKNOWN FALSE
TRUE TRUE UNKNOWN FALSE
P1 UNKNOWN UNKNOWN UNKNOWN FALSE
FALSE FALSE FALSE FALSE
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 32 / 35
Null 3VL
AND
P1 AND P2 P2
TRUE UNKNOWN FALSE
TRUE TRUE UNKNOWN FALSE
P1 UNKNOWN UNKNOWN UNKNOWN FALSE
FALSE FALSE FALSE FALSE
OR
P1 OR P2 P2
TRUE UNKNOWN FALSE
TRUE TRUE TRUE TRUE
P1 UNKNOWN TRUE UNKNOWN UNKNOWN
FALSE TRUE UNKNOWN FALSE
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 32 / 35
Null 3VL
AND
P1 AND P2 P2
TRUE UNKNOWN FALSE
TRUE TRUE UNKNOWN FALSE
P1 UNKNOWN UNKNOWN UNKNOWN FALSE NOT
FALSE FALSE FALSE FALSE
NOT P1
TRUE FALSE
OR P1 UNKNOWN UNKNOWN
FALSE TRUE
P1 OR P2 P2
TRUE UNKNOWN FALSE
TRUE TRUE TRUE TRUE
P1 UNKNOWN TRUE UNKNOWN UNKNOWN
FALSE TRUE UNKNOWN FALSE
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 32 / 35
Null 3VL
AND
P1 AND P2 P2
TRUE UNKNOWN FALSE
TRUE TRUE UNKNOWN FALSE
P1 UNKNOWN UNKNOWN UNKNOWN FALSE NOT
FALSE FALSE FALSE FALSE
NOT P1
TRUE FALSE
OR P1 UNKNOWN UNKNOWN
FALSE TRUE
P1 OR P2 P2
TRUE UNKNOWN FALSE
TRUE TRUE TRUE TRUE
P1 UNKNOWN TRUE UNKNOWN UNKNOWN
FALSE TRUE UNKNOWN FALSE
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 32 / 35
Null 3VL
SELECT no SELECT no
FROM account FROM account
WHERE rate=NULL WHERE rate IS NULL
OR rate<>NULL OR rate IS NOT NULL
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 33 / 35
Null 3VL
account
no type cname rate sortcode
SELECT no 100 ’current’ ’McBrien, P.’ NULL 67
101 ’deposit’ ’McBrien, P.’ 5.25 67
FROM account 103 ’current’ ’Boyd, M.’ NULL 34
WHERE (rate=5.25) IS NOT FALSE 107 ’current’ ’Poulovassilis, A.’ NULL 56
119 ’deposit’ ’Poulovassilis, A.’ 5.50 56
125 ’current’ ’Bailey, J.’ NULL 56
A B C D
no no no no
100 100 100
101 103 103
103 107 107
107 119 125
125 125
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 34 / 35
Null 3VL
movement
mid no amount tdate
0999 119 45.00 null
1000 100 2300.00 5/1/1999
1001 101 4000.00 5/1/1999
account
1002 100 -223.45 8/1/1999
no type cname rate sortcode
1004 107 -100.00 11/1/1999
100 ’current’ ’McBrien, P.’ null 67
1005 103 145.50 12/1/1999
101 ’deposit’ ’McBrien, P.’ 5.25 67
1006 100 10.23 15/1/1999
119 ’deposit’ ’Poulovassilis, A.’ 5.50 56
1008 101 1230.00 15/1/1999
125 ’current’ ’Bailey, J.’ null 56
1009 119 5600.00 18/1/1999
1010 100 null 20/1/1999
1011 null null 20/1/1999
1012 null 600.00 20/1/1999
1013 null -46.00 20/1/1999
P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 35 / 35