SQL Queries

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

SQL

Murali Mani

SELECT-FROM-WHERE
SELECT * FROM Student WHERE sName=Greg AND address=320 FL Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sNumber 2 sName Greg address 320FL professor MM

(sName=Greg AND address=320 FL) (Student)


Murali Mani

Project
SELECT sNumber, sName FROM Student
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sNumber 1 2 3 sName Dave Greg Matt

(sNumber, sName) (Student)


Murali Mani

Extended Projection
SELECT sNumber || sName AS info FROM Student WHERE address=320 FL
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER info 1Dave 2Greg 3Matt

(sNumber||sNameinfo) ( (address=320 FL) (Student))


Murali Mani

SQL and Relational Algebra


In short, L ( C (R)) becomes SELECT L FROM R WHERE C

Murali Mani

Renaming
SELECT s1.sNumber AS num FROM Student S1 WHERE s1.sNumber >= 1;
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER num 1 2 3

(s1.sNumbernum) ( (s1.sNumber >= 1) ( S1 (Student)))


Murali Mani

String operators
Comparison Operators based on lexicographic ordering: =, <, >, <>, >=, <= Concatenation operator: || represented in strings with two consecutive Pattern match: s LIKE p
p = pattern % : any sequence of 0 or more characters - : matches 1 character Patterns can explicitly declare escape characters as: s LIKE x%%am% ESCAPE x
Murali Mani

Comparison with NULL values


Arithmetic operations on NULL return NULL. Comparison operators on NULL return UNKNOWN. We can explicitly check whether a value is null or not, by IS NULL, IS NOT NULL.

Murali Mani

Truth table with UNKNOWN


UNKNOWN AND TRUE = UNKNOWN UNKNOWN OR TRUE = TRUE UNKNOWN AND FALSE = FALSE UNKNOWN OR FALSE = UNKNOWN UNKNOWN AND UNKNOWN = UNKNOWN UNKNOWN OR UNKNOWN = UNKNOWN NOT UNKNOWN = UNKNOWN A WHERE clause is satisfied only when it evaluates to TRUE.

Murali Mani

UNION, INTERSECT, EXCEPT


(SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=320 FL) UNION, INTERSECT, EXCEPT have set semantics. For bag semantics, use UNION ALL, INTERSECT ALL, EXCEPT ALL
Murali Mani

EXCEPT - Example
(SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=320 FL) Student
sNumber 1 2 3 4 sName Dave Greg Matt Matt address 320FL 320FL 320FL 300FL professor MM MM ER ER sNumber sName address professor

Murali Mani

Joins
SELECT sName FROM Student, Professor WHERE pName=MM AND pNumber=professor;

(sName)(Student (pName=MM and pNumber=professor) Professor)

Murali Mani

Joins - example
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor 1 1 2 1 2

Professor
pNumber pName MM ER address 141FL 201FL

SELECT sName FROM Student, Professor WHERE pName=MM AND pNumber=professor;


Murali Mani

sName Dave Greg

Cross Product (Cartesian Product)


SELECT * FROM Student CROSS JOIN Professor; Student X Professor can also be written as: SELECT * FROM Student, Professor
Murali Mani

Cross Product - Example


Student
sNumber 1 2 3 sNumber 1 1 2 2 3 3 sName Dave Greg Matt sName Dave Dave Greg Greg Matt Matt address 320FL 320FL 320FL address 320FL 320FL 320FL 320FL 320FL 320FL professor 1 1 2 professor 1 1 1 1 2 2
Murali Mani

Professor
pNumber 1 2 pName MM ER address 141FL 201FL

pNumber 1 2 1 2 1 2

pName MM ER MM ER MM ER

address 141FL 201FL 141FL 201FL 141FL 201FL

Theta Join
SELECT * FROM Student JOIN Professor ON professor=pNumber;
Student (professor=pNumber) Professor

SELECT * FROM Student, Professor WHERE professor=pNumber;


Murali Mani

Theta Join Example


sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor 1 1 2 pNumber 1 1 2 pName MM MM ER address 141FL 141FL 201FL

Murali Mani

Natural Join
SELECT * FROM Student NATURAL JOIN Professor (Note: This requires the columns on which the join should be done should have the same names for Student and Professor).
Student Professor

Murali Mani

Natural Join - Example


Student
sNumber 1 2 3 sNumber 1 2 3 sName Dave Greg Matt sName Dave Greg Matt address 320FL 320FL 320FL address 320FL 320FL 320FL pNumber 1 1 2 pNumber 1 1 2 pName MM MM ER address 141FL 141FL 201FL 1 2

Professor
pNumber pName MM ER address 141FL 201FL

Murali Mani

Outer Joins
SELECT * FROM Student NATURAL FULL OUTER JOIN Professor
Student o Professor

SELECT * FROM Student NATURAL LEFT OUTER JOIN Professor


Student oL Professor

Murali Mani

10

Outer Joins
SELECT * FROM Student NATURAL RIGHT OUTER JOIN Professor

Student oR Professor

Murali Mani

Outer Joins - Example


Student
sNumber 1 2 3 4 sNumber 1 2 3 4 Null sName Dave Greg Matt Ben sName Dave Greg Matt Ben Null address 320FL 320FL 320FL 320FL address 320FL 320FL 320FL 320FL Null pNumber 1 1 2 4 pNumber 1 1 2 4 3
Murali Mani

Professor
pNumber 1 2 3 pName MM ER MW address 141FL 201FL 168FL

pName MM MM ER Null MW

address 141FL 141FL 201FL Null 168FL

11

Sorting: ORDER BY clause


SELECT * FROM Student WHERE sNumber >= 1 ORDER BY sNumber, sName (sNumber, sName) ( (sNumber >= 1) (Student))

Murali Mani

Subqueries
SELECT * FROM Student WHERE professor = (SELECT pName FROM Professor WHERE pNumber=1) Note: the inner subquery returns a relation, but SQL runtime ensures that the subquery returns a relation with one column and with one row, otherwise it is a run-time error.
Murali Mani

12

Subqueries - Example
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER

SELECT * FROM Student WHERE professor = (SELECT pName FROM Professor WHERE pNumber=1)

Professor
pNumber 1 2 pName MM ER address 141FL 201FL sNumber 1 2 sName Dave Greg address 320FL 320FL professor MM MM

Murali Mani

Subqueries
We can use IN, EXISTS (also NOT IN, NOT EXISTS) ALL, ANY can be used with comparisons SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor)
Murali Mani

13

Subqueries - Example
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER

Professor
pNumber 1 2 pName MM ER address 141FL 201FL

SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor)

sNumber 1

sName Dave

address 320FL

professor MM

Murali Mani

Subqueries: EXISTS
SELECT * FROM Student WHERE EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName)

Professor
pNumber 1 2 pName MM ER address 141FL 201FL

Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER

Murali Mani

14

Subqueries with negation


SELECT * FROM Student WHERE (sNumber, professor) NOT IN (SELECT pNumber, pName FROM Professor)

Professor
pNumber 1 2 pName MM ER address 141FL 201FL

Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sNumber 2 3 sName Greg Matt address professor 320FL 320FL MM ER

Murali Mani

Subqueries with negation


Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER 1 2

Professor
pNumber pName MM ER address 141FL 201FL

SELECT * FROM Student WHERE NOT EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName)
sNumber sName address professor

Murali Mani

15

Subqueries: ALL, ANY


SELECT * FROM Student WHERE sNumber > ALL (SELECT pNumber FROM Professor) SELECT * FROM Student WHERE sNumber = ANY (SELECT pNumber FROM Professor)
Murali Mani

Subqueries: ALL - Example


Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER 1 2

Professor
pNumber pName MM ER address 141FL 201FL

SELECT * FROM Student WHERE sNumber > ALL (SELECT pNumber FROM Professor)

sNumber 3

sName Matt

address professor 320FL ER

Murali Mani

16

Subqueries: ANY - Example


Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER 1 2

Professor
pNumber pName MM ER address 141FL 201FL

SELECT * FROM Student WHERE sNumber = ANY (SELECT pNumber FROM Professor)

sNumber 1 2

sName Dave Greg

address 320FL 320FL

professor MM MM

Murali Mani

Subqueries: NOT ALL Example


Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER 1 2

Professor
pNumber pName MM ER address 141FL 201FL

SELECT * FROM Student WHERE NOT sNumber > ALL (SELECT pNumber FROM Professor)

sNumber 1 2

sName Dave Greg

address 320FL 320FL

professor MM MM

Murali Mani

17

Subqueries: NOT ANY Example


Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER 1 2

Professor
pNumber pName MM ER address 141FL 201FL

SELECT * FROM Student WHERE NOT sNumber = ANY (SELECT pNumber FROM Professor)

sNumber 3

sName Matt

address professor 320FL ER

Murali Mani

Subqueries: Tip

Murali Mani

18

Subqueries in FROM clause


SELECT sName, pName FROM Student, (SELECT * FROM Professor WHERE pNumber=1) WHERE professor=pName;

Professor
pNumber 1 2 pName MM ER address 141FL 201FL

Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sName Dave Greg pName MM MM

Murali Mani

Duplicate Elimination
SELECT DISTINCT * FROM Student;

(Student)
Student

SELECT DISTINCT address FROM Student WHERE sNumber >= 1;

( (address) ( (sNumber >= 1) (Student)))

sNumber 1 2 3

sName Dave Greg Matt

address 320FL 320FL 320FL

professor MM MM ER

address 320FL

Murali Mani

19

Aggregation
SELECT COUNT (*) FROM Student; SELECT COUNT (sNumber) FROM Student; SELECT MIN (sNumber) FROM Student; SELECT MAX (sNumber) FROM Student; SELECT SUM (sNumber) FROM Student; SELECT AVG (sNumber) FROM Student; We can have distinct such as: SELECT COUNT (DISTINCT sNumber) FROM Student
Murali Mani

Grouping
SELECT COUNT (sName) FROM Student GROUP BY address;
(COUNT (sName))

( (address, COUNT (sName)) (Student))


professor MM MM ER COUNT (sName) 3

Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL

Murali Mani

20

Grouping
SELECT address, COUNT (sNumber) FROM Student WHERE sNumber > 1 GROUP BY address HAVING COUNT (sNumber) > 1; Student
sNumber 1 2 3 4 sName Dave Greg Matt Ben address 320FL 320FL 320FL 300FL professor MM MM ER ER
Murali Mani

address 320FL

COUNT (sNumber) 2

Aggregation and NULLs


NULLs are ignored in any aggregation; except COUNT (*) However if the set of attributes to be grouped on has null values, then grouping is done on the null values as well.

Murali Mani

21

SQL Queries - Summary


SELECT [DISTINCT] a1, a2, , an FROM R1, R2, , Rm [WHERE C1] [GROUP BY g1, g2, , gl [HAVING C2]] [ORDER BY o1, o2, , oj]

Murali Mani

22

You might also like