Академический Документы
Профессиональный Документы
Культура Документы
1
SQL Query
– Example:
2
SELECT Clause
3
Wildcard(*) in SELECT-Clause
4
AS in SELECT-Clause
5
DISTINCT in SELECT-Clause
6
Arithmetic Expression in SELECT-Clause
7
String functions in SELECT-Clause
8
String functions in SELECT-Clause
9
String functions in SELECT-Clause
10
Boolean Expression in SELECT-Clause
11
FROM Clause
12
Joined Relations in FROM-Clause
Query 1: Retrieve the name and address of all employees who work
for the 'Research' department
is equivalent to:
13
Joined Relations in FROM-Clause
14
JOIN
R SELECT *
FROM R JOIN S on R.Id = S. Id S
Id Name
Id Value
111 John
foreach row r in table R 111 B
222 Mary
foreach row s in table S 111 A
333 Bill
if r.Id = s.Id then 222 A
444 Joe
output the merged
row of r and s
Id Name Id Value
111 John 111 B
111 John 111 A
222 Mary 222 A
15
JOIN – Example
Student Transcript
Id Name Addr Status StudId CrsCode Sem Grade
111 John ….. ….. 111 CSE305 S00 B
222 Mary ….. ….. 111 CSE306 S99 A
333 Bill ….. ….. 222 CSE304 F99 A
444 Joe ….. …..
R SELECT *
FROM R NATURAL JOIN S S
Id Name
foreach row r in table R Id Value
111 John
foreach row s in table S 111 B
222 Mary Examine their common attributes 111 A
333 Bill If their values are the same, then 222 A
444 Joe merge the rows while removing
their duplicate columns
Id Name Value
111 John B
111 John A
222 Mary A
17
Natural Join
Join attribute must have the same name (Id)
Student Transcript2
Id Name Addr Status Id CrsCode Sem Grade
111 John ….. ….. 111 CSE305 S00 B
222 Mary ….. ….. 111 CSE306 S99 A
333 Bill ….. ….. 222 CSE304 F99 A
444 Joe ….. …..
18
Cross Join
R SELECT *
FROM R CROSS JOIN S S
Id Name
Id Value
111 John
foreach row r in table R 111 B
222 Mary
foreach row s in table S 111 A
333 Bill
output the merged row r and s 222 A
444 Joe
Id Name Id Value
111 John 111 B
111 John 111 A
111 John 222 A
Output has 12 rows
… … … …
444 Joe 222 A
19
Left Outer Join
SELECT *
R FROM R LEFT OUTER JOIN S S
Id Name ON R.Id = S.Id
Id Value
111 John foreach row r in table R 111 B
222 Mary foreach row s in table S. 111 A
333 Bill if r.Id = S.Id then
222 A
444 Joe output the merged row r and s
if row r is not merged with any rows in S
output row r with NULL values for s
Id Name Id Value
111 John 111 B
111 John 111 A Output has 5 rows
222 Mary 222 A
333 Bill NULL NULL
444 Joe NULL NULL
20
Left Outer Join - Example
21
Right Outer Join
SELECT *
R FROM R RIGHT OUTER JOIN S S
Id Name ON R.Id = S.Id
Id Value
111 John foreach row s in table S 111 B
222 Mary foreach row r in table R 111 A
333 Bill if r.Id = S.Id then
555 A
444 Joe output the merged row r and s
if row s is not merged with any rows in R
output row s with NULL values for r
Id Name Id Value
111 John 111 B
111 John 111 A Output has 3 rows
NULL NULL 555 A
22
WHERE-Clause
WHERE <condition>
– Complex conditions:
<condition> AND <condition>
<condition> OR <condition>
NOT <condition>
23
Boolean Expression in WHERE-Clause
SELECT *
FROM EMPLOYEE
WHERE SSN > SUPERSSN AND SALARY > 49999.99
AND MINIT='B' AND LNAME='SMITH'
AND BDATE >= '1980-01-01';
24
Substring Comparison in WHERE-Clause
26
Substring Comparison
27
Arithmetic Expression in WHERE-Clause
28
Arithmetic Expression in WHERE-Clause
29
UNSPECIFIED WHERE-clause
SELECT SSN
FROM EMPLOYEE;
30
NULLS IN SQL QUERIES
32
Example in Oracle
Not available
in MySQL
33
Example
34
Example
(SELECT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME='Smith')
UNION
(SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN
AND NAME='Smith');
35
Example
36
Example
37
IN Operator
v IN W
Examples:
– 3 in {1, 2, 3} TRUE
– 0 in {1, 2, 3} FALSE
38
IN Operator
Using IN Operator:
39
Example
40
Exercise
41