0% found this document useful (0 votes)
13 views

SQL Lecture

The document discusses how SQL implements operations from the relational algebra such as projection, selection, joins, set operations. SQL SELECT statements roughly correspond to projection and selection in RA. Joins in SQL correspond to the various join operations in RA such as natural join. Set operations like union are also implemented. The document also discusses how SQL uses bag semantics by default rather than set semantics.

Uploaded by

Sadiholic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

SQL Lecture

The document discusses how SQL implements operations from the relational algebra such as projection, selection, joins, set operations. SQL SELECT statements roughly correspond to projection and selection in RA. Joins in SQL correspond to the various join operations in RA such as natural join. Set operations like union are also implemented. The document also discusses how SQL uses bag semantics by default rather than set semantics.

Uploaded by

Sadiholic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

SQL: An Implementation of the Relational Algebra

P.J. McBrien

Imperial College London

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 1 / 35
SQL and RA RA to SQL DML

SQL DML: An Implementation of the RA

SQL SELECT statements: Rough Equivalence to RA


SELECT A1 , ..., An
FROM R1 , ..., Rm
WHERE P1 πA1 ,...,An σP1 ∧...∧Pk R1 × . . . × Rm
AND ...
AND Pk

SQL SELECT implements RA π, σ and ×

πbname,no σbranch.sortcode=account.sortcode∧account.type=‘current’ (branch × account)

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

Naming columns in SQL


Column naming rules in SQL
You must never have an ambiguous column name in an SQL statement
You can use SELECT * to indicate all columns (i.e. have no projection)
You can use tablename.* to imply all columns from a table

✓ ✗ ✓
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

Quiz 1: Translating RA into SQL

Which SQL query implements πbname,no σtype=‘deposit’ (account ⋊


⋉ branch)?

A B

SELECT ∗ SELECT bname , no


FROM a c c o unt , b r a n c h FROM a c c o unt , b r a n c h
WHERE t y p e= ’ d e p o s i t ’ WHERE t y p e= ’ d e p o s i t ’

C D

SELECT bname , no SELECT bname , no


FROM br a nc h , a c c o u n t FROM a c c o unt , b r a n c h
WHERE b r a n c h . s o r t c o d e = WHERE b r a n c h . s o r t c o d e =
account . s or tcode a c c o u n t . no
AND t y p e= ’ d e p o s i t ’ AND t y p e= ’ d e p o s i t ’

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 4 / 35
SQL and RA RA to SQL DML

Connectives Between SQL SELECT statements

Binary operators between SELECT statements


SQL UNION implements RA ∪
SQL EXCEPT implements RA −
SQL INTERSECT implements RA ∩
Note that two tables must be union compatible: have the same number and type
of columns

πno account − πno movement

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

Modern SQL Join Syntax

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

Special Syntax for Natural Join

SELECT ∗
FROM b r a n c h NATURAL JOIN a c c o u n t

Another Special Syntax for Natural Join

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

Overview of RA and SQL correspondances

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

Try some examples yourself . . .

medusa-s2(pjm)-4$ psql -h db -U lab -d bank branch -W


Password:
bank branch=> SELECT *
bank branch-> FROM branch NATURAL JOIN account;

sortcode | bname | cash | no | type | cname | rate


----------+-----------+----------+-----+----------+-------------------+------
67 | Strand | 34005.00 | 100 | current | McBrien, P. |
67 | Strand | 34005.00 | 101 | deposit | McBrien, P. | 5.25
34 | Goodge St | 8900.67 | 103 | current | Boyd, M. |
56 | Wimbledon | 94340.45 | 107 | current | Poulovassilis, A. |
56 | Wimbledon | 94340.45 | 119 | deposit | Poulovassilis, A. | 5.50
56 | Wimbledon | 94340.45 | 125 | current | Bailey, J. |

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

medusa-s2(pjm)-4$ sqsh -S sqlserver -X -U lab -D bank branch


Password:
[21] sqlserver.bank branch.1> SELECT *
[21] sqlserver.bank branch.2> FROM branch NATURAL JOIN account
[21] sqlserver.bank branch.3> \go

Msg 102, Level 15, State 1


Server ’DOWITCHER’, Line 2
Line 2: Incorrect syntax near ’account’.

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 9 / 35
Bags and Sets Bags or Sets

SQL: Bags and Sets

SELECT sortcode SELECT DISTINCT sortcode


FROM account FROM account

≈ πsortcode account πsortcode account


sortcode sortcode
67 34
67 56
56 67
56
56
34

SQL SELECT: Bag semantics


By default, an SQL SELECT (equivalent to an RA π) does not eliminate
duplicates, and returns a bag (or multiset) rather than a set.
Any SELECT that does not cover a key of the input relation, and requires a set
based answer, should use DISTINCT.

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 10 / 35
Bags and Sets Bags or Sets

SQL: Bags and Sets

SELECT ALL sortcode SELECT DISTINCT sortcode


FROM account FROM account

≈ πsortcode account πsortcode account


sortcode sortcode
67 34
67 56
56 67
56
56
34

SQL SELECT: Bag semantics


By default, an SQL SELECT (equivalent to an RA π) does not eliminate
duplicates, and returns a bag (or multiset) rather than a set.
Any SELECT that does not cover a key of the input relation, and requires a set
based answer, should use DISTINCT.

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 10 / 35
Bags and Sets Bags or Sets

Quiz 2: Correct use of SELECT DISTINCT (1)


branch(sortcode,bname,cash)
key branch(sortcode)
key branch(bname)

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

SELECT bname , c a s h SELECT c a s h


FROM branch FROM branch
WHERE cash >10000

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 11 / 35
Bags and Sets Bags or Sets

Quiz 3: Correct use of SELECT DISTINCT (2)


branch(sortcode,bname,cash) key branch(sortcode)
account(no,type,cname,rate,sortcode) key branch(bname)
key account(no)

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

Quiz 4: Operators that might produce bags

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

Bag and Set operations in SQL

RA Operator Set Based SQL Bag Based SQL


πA1 ,...,An SELECT DISTINCT A1 , . . . , An SELECT ALL A1 , . . . , An
R1 × . . . × Rm FROM R1 , . . . , Rm FROM R1 , . . . , Rm
σP1 ,...,Pk WHERE P1 AND . . . AND Pk WHERE P1 AND . . . AND Pk
R1 ∪ R2 R1 UNION DISTINCT R2 R1 UNION ALL R2
R1 − R2 R1 EXCEPT DISTINCT R2 R1 EXCEPT ALL R2
R1 ∩ R2 R1 INTERSECT DISTINCT R2 R1 INTERSECT ALL R2

Chosing between set and bag semantics


If you omit DISTINCT or ALL, then the defaults are:
SELECT ALL
UNION DISTINCT
EXCEPT DISTINCT
INTERSECT DISTINCT

No FROM DISTINCT or WHERE DISTINCT?


There is no need for DISTINCT or ALL around FROM (×) and WHERE (σ) cannot
introduce any duplicates, and any existing duplicates can be removed in the SELECT

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

SQL SELECT statements: Exact Equivalence to RA


SELECT DISTINCT A1 , ..., An
FROM R1 , ..., Rm
WHERE P1 ≡ πA1 ,...,An σP1 ∧...∧Pk R1 × . . . × Rm
AND ...
AND Pk

SQL SELECT implements RA π, σ and ×


Omit DISTINCT when either
you known A1 , ..., An cover a key
you want a bag (rather than set) answer

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 15 / 35
Bags and Sets Bags or Sets

Quiz 5: SQL EXCEPT

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

What is the result of the above SQL query?

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

Quiz 6: SQL EXCEPT ALL

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

What is the result of the above SQL query?

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

Table and Column Aliases


The SQL operator AS allows a column or table name to be renamed.
Essential when needing to join a table with itself

List people with a current and a deposit account

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 ’

cname current no deposit no


‘McBrien, P.’ 100 101
‘Poulovassilis, A.’ 107 119

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 18 / 35
Bags and Sets Bags or Sets

Worksheet: Translating Between Relational Algebra and SQL

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

IN operator tests for membership of a set

SELECT ∗
FROM account
WHERE t y p e= ’ c u r r e n t ’
AND no IN ( 1 0 0 , 1 0 1 )

Can use nested SELECT to generate set


SELECT no
FROM account no
WHERE t y p e= ’ c u r r e n t ’ 100
AND no IN ( SELECT no
FROM movement
WHERE amount >500)

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 20 / 35
Bags and Sets Set Operations

Set Operations: IN

IN operator tests for membership of a set

SELECT ∗
FROM account
WHERE t y p e= ’ c u r r e n t ’
AND no IN ( 1 0 0 , 1 0 1 )

Can use nested SELECT to generate set


SELECT no
SELECT DISTINCT a c c o u n t . no
FROM account
FROM a c c o u n t JOIN movement
WHERE t y p e= ’ c u r r e n t ’
≡ ON a c c o u n t . no=movement . no
AND no IN ( SELECT no
WHERE t y p e= ’ c u r r e n t ’
FROM movement
AND amount >500
WHERE amount >500)

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 20 / 35
Bags and Sets Set Operations

Quiz 7: SQL Set Membership Testing

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

What is the result of the above SQL query?

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

Quiz 7: SQL Set Membership Testing

SELECT no SELECT DISTINCT a c c o u n t . no


FROM account FROM account
WHERE t y p e= ’ c u r r e n t ’ JOIN movement
AND no NOT IN 6≡
ON a c c o u n t . no=movement . no
( SELECT no WHERE t y p e= ’ c u r r e n t ’
FROM movement AND NOT amount >500
WHERE amount >500)

What is the result of the above SQL query?

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

Set Operations: EXISTS

Testing for Existence


IN can be used to test if some value is in a relation, either listed, or produced by
some SELECT statement
EXISTS can be used to test if a SELECT statement returns any rows

List people without a deposit account

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

Set Operations: EXISTS

NOT EXISTS and EXCEPT


Most queries involving EXCEPT can be also written using NOT EXISTS
EXCEPT relatively recent addition to SQL

πno account − πno movement

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

Set Operations: SOME and ALL


Can test a value against members of a set
V op SOME S is TRUE is there is at least one Vs ∈ S such that V op Vs
V op ALL S is TRUE is there are no values Vs ∈ S such that NOT V op Vs

names of branches that only have current accounts

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 )

names of branches that have deposit accounts

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

Worksheet: 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

Worksheet: Set Operations (3)

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

Worksheet: Set Operations (4)

Write an SQL query that lists the cname of customers that have every type of
account that appears in account

SELECT DISTINCT cname


FROM a c c o u n t AS c u s t a c c o u n t
WHERE NOT EXISTS ( SELECT t y p e
FROM account
EXCEPT
SELECT t y p e
FROM account
WHERE a c c o u n t . cname=c u s t a c c o u n t . cname
)

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 27 / 35
Bags and Sets Set Operations

Worksheet: Set Operations (4)

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

Worksheet: Set Operations (4)

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

Set Operations: NOT SOME NOT and ALL

Equivalence between exists and for all


In first order classical logic ¬∃¬ ≡ ∀

accounts with all movements less than or equal to 500

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

Set Operations: NOT SOME NOT and ALL

Equivalence between exists and for all


In first order classical logic ¬∃¬ ≡ ∀

accounts with all movements less than or equal to 500

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

Several definitions of null have been proposed, including:

1 null represents a something that is not present in the UoD

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 35
Null

Null

Several definitions of null have been proposed, including:

1 null represents a something that is not present in the UoD


2 null represents something that might be present in the UoD, but we do not know
its value at present

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 35
Null

Null

Several definitions of null have been proposed, including:

1 null represents a something that is not present in the UoD


2 null represents something that might be present in the UoD, but we do not know
its value at present
3 null represents something that is present in the UoD, but we do not know its
value at present

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 35
Null

Null

Several definitions of null have been proposed, including:

1 null represents a something that is not present in the UoD


2 null represents something that might be present in the UoD, but we do not know
its value at present
3 null represents something that is present in the UoD, but we do not know its
value at present

SQL handling of NULL


SQL uses a three valued logic to process WHERE predicate
Truth values are TRUE, FALSE, and UNKNOWN
SQL standard vague, but handling of NULL is nearest to option 2

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 35
Null

Quiz 8: SQL handling of NULL (1)

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

What is the result of the SQL query above?

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

Quiz 9: SQL handling of NULL (2)

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

What is the result of the SQL query above?

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

SQL implements three valued logic

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

SQL implements three valued logic

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

SQL implements three valued logic

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

SQL implements three valued logic

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

Truth values of SQL Formulae


Formula Results
x=null UNKNOWN
null=null UNKNOWN
x IS NULL TRUE if x has a null value, FALSE otherwise
x IS NOT NULL TRUE if x does not have a null value, FALSE otherwise

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 32 / 35
Null 3VL

‘Correct’ SQL Queries Using null

Correct testing for NULL


SELECT no SELECT no
FROM account FROM account
WHERE rate=NULL WHERE rate IS NULL

SELECT no SELECT no
FROM account FROM account
WHERE rate=NULL WHERE rate IS NULL
OR rate<>NULL OR rate IS NOT NULL

Testing for logical truth value


SELECT no
FROM account
WHERE (rate=5.50) IS NOT TRUE

P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 33 / 35
Null 3VL

Quiz 10: SQL ’Might Be’

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

What is the result of the above SQL query?

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

Worksheet: Null values in SQL

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

You might also like