Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4...

59
Lecture10: Data Manipulation in SQL , Advanced SQL queries Ref. Chapter5 1 College of Computer and Information Sciences - Information Systems Dept. Prepared by L. Nouf Almujally & Aisha AlArfaj IS220 / IS422 : Database Fundamentals

Transcript of Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4...

Page 1: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Lecture10: Data Manipulation in SQL , Advanced SQL

queries

Ref. Chapter5

1

Coll

ege

of

Co

mpu

ter

and I

nfo

rmat

ion S

cien

ces

- In

form

atio

n S

yst

ems

Dep

t.

Prepared by L. Nouf Almujally & Aisha AlArfaj

IS220 / IS422 : Dat abase Fundam ent a l s

Page 2: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

The Process of Database Design Real World

Domain

Conceptual model (ERD)

Relational Data Model

Create schema

(DDL)

Load Data

(DML) Lec

ture

10

2

Page 3: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Sample Data in Customer Table

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh

25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

Lec

ture

10

3

Page 4: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Sample Data in Product Table

prodNo prodName prodDes price

100 P0 Food 100

101 P1 healthy food 100

102 P2 200

103 P3 self_raising

flour,80%wheat

300

104 P4 network 80x 300

Lec

ture

10

4

Page 5: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Sample Data in Orders Table

ordNo ordDate custNo prodNo quantity

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

Lec

ture

10

5

Page 6: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Dept Number Dept Name Location Mail Number

D1 Computer Science Bundoora 39

D2 Information Science Bendigo 30

D3 Physics Bundoora 37

D4 Chemistry Bendigo 35

DEPARTMENT

EMPLOYEE

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

E4 Graham Burke D1 44000

E5 Annie Nguyen D1 60000

Lec

ture

10

6

Page 7: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

O_Id OrderDate OrderPrice Customer 1 2008/11/12 1000 Nora 2 2008/10/23 1600 Sara 3 2008/09/02 700 Nora 4 2008/09/03 300 Nora 5 2008/08/30 2000 Yara 6 2008/10/04 100 Sara

Table orders ( Example 3)

Lec

ture

10

7

Sample Data in Customer Table

Page 8: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Table names and Column names • Table name can be prefixed with the owner name.

eg, if table product is owned by user John, you can use

SELECT * FROM John.product;

• Column names can be prefixed with table name,

SELECT product.prodNo

FROM product;

Lec

ture

10

8

Page 9: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Alias • SQL aliases are used to temporarily rename a table or a

column heading.

• Syntax for Columns

• Syntax for Tables

Lec

ture

10

9

SELECT column_name AS alias_name

FROM table_name;

SELECT column_name(s)

FROM table_name [AS] alias_name;

Page 10: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Alias ( important note ) Columns Alias:

• For example, you might wish to know how much is the combined total salary of all employees whose salary is above $25,000 / year.

SELECT SUM(salary) AS "Total Salary" FROM employees WHERE salary > 25000;

• In this example, we've aliased the sum(salary) field as "Total Salary". As a result, "Total Salary" will display as the field name when the result set is returned.

Table Alias:

• SELECT o.OrderID, o.OrderDate FROM Orders AS o;

Lec

ture

10

10

Page 11: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Exercise create table count_null ( a number, b

number );

insert into count_null values ( 1, 5);

insert into count_null values ( null, 7);

insert into count_null values ( null,

null);

insert into count_null values ( 8, 2);

select count(a) as "count_a_not_null", count(b) as "count_b_not_null", count(*) as "count_all“

from count_null;

• Output :

Lec

ture

10

11

Page 12: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

JOIN

Lec

ture

10

12

Page 13: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

JOIN • Often two or more tables are needed at the same time to find

all required data

• These tables must be "joined" together

• The formal JOIN basically,

• computes a new table from those to be joined

• the new table contains data in the matching rows of the

individual tables.

Lec

ture

10

13

Page 14: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Types of JOIN

• types of JOIN:.

• INNER JOIN: Return rows when there is at least one match in both

tables

• LEFT JOIN: Return all rows from the left table, even if there are no

matches in the right table

• RIGHT JOIN: Return all rows from the right table, even if there are no

matches in the left table

• Full Outer Joins : retains rows that are unmatched in both the tables.

NOTE: In all the above outer joins, the displayed unmatched

columns are filled with NULLS.

Lec

ture

10

14

Page 15: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Types of JOIN

Lec

ture

10

15

Page 16: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

SQL Examples of Joins ( 1) • Simple Join

SELECT E.firstName, E.lastName, D.deptName

FROM EMPLOYEE E, DEPARTMENT D

WHERE E.deptNumber = D.deptNumber;

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

E4 Graham Burke D1 44000

E5 Annie Nguyen D1 60000

Dept Number Dept Name Location Mail Number

D1 Computer Science Bundoora 39

D2 Information Science Bendigo 30

D3 Physics Bundoora 37

D4 Chemistry Bendigo 35

Lec

ture

10

16

Page 17: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Employee No.First Name Last Name Salary Dept Number Dept Number Dept Name LocationMail Number

E1 Mandy Smith 50000 D1 D1 Computer Science Bundoora 39

E2 Daniel Hodges 45000 D2 D2 Information Science Bendigo 30

E3 Shaskia Ramanthan 58000 D2 D2 Information Science Bundoora 37

E4 Graham Burke 44000 D1 D1 Computer Science Bundoora 39

E5 Annie Nguyen 60000 D1 D1 Computer Science Bundoora 39

This is the result from the matching

This is the final result: Lec

ture

10

17

FIRSTNAME LASTNAME DEPTNAME

----------------- ------------------- -------------------

Mandy Smith computer science

Annie Nguyen computer science

Graham Burke Computer Science

Shaskia Ramanthan Information Science

Daniel Hodges Information Science

Page 18: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

SQL Examples of Joins ( 2 ) • Joining more than two tables

SELECT E.firstName, E.lastName, P.projTitle

FROM EMPLOYEE E, WORKS_ON W, PROJECT P

WHERE E.employeeNo = W.employeeNo AND W.projNo = P.projNo;

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E4 Graham Burke D1 44000

E5 Annie Nguyen D1 60000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

Employee No. ProjNo

E1 1

E4 1

E5 2

E2 3

E3 1

ProjNo Project Title

1 Project A

2 Project B

3 Project C

EMPLOYEE

WORKS_ON

PROJECT

Lec

ture

10

18

FIRSTNAME LASTNAME PROJTITLE

------------------ ------------------ -------------------

Mandy Smith Project A

Graham Burke Project A

Annie Nguyen Project B

Daniel Hodges Project C

Shaskia Ramanthan Project A

Page 19: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

SQL Examples of Joins ( 3 ) • List customers (by customer number, name and address) who have

ordered the product 100.

SELECT c.custNo, custName, custSt, custCity

FROM customer c, orders o

WHERE c.custNo=o.custNo AND prodNo=100;

custN

o

custN

ame

custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

ordNo ordDate custNo prodNo quantity

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

Lec

ture

10

19

CUSTNO CUSTNAME CUSTST CUSTCITY

---------------- ----------------- ------------- ----------------

1 C1 Olaya St Jeddah

2 C2 Mains St Riyadh

3 C3 Mains Rd Riyadh

Page 20: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

SQL Examples of Joins ( 4 )

• Find the total price of the products ordered by customer 1.

SELECT sum(price*quantity) FROM orders, product

WHERE orders.prodNo = product.prodNo AND custNo = 1;

prod

No

prodN

ame

prodDes price

100 P0 Food 100

101 P1 healthy food 100

102 P2 200

103 P3 self_raising

flour,80%wheat

300

104 P4 network 80x 300

ordNo ordDate custNo prodN

o

quant

ity

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

Lec

ture

10

20

SUM(PRICE*QUANTITY)

----------------------------------

400

Page 21: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Outer Joins in Oracle SQL

• Put an (+) on the potentially deficient side, ie the side where nulls may be added

• The (+) operator is placed in the join condition next to the table that is allowed to have NULL values.

• Example (Left Outer Join) : List all customers, and the products ordered if they have ordered some products.

SELECT c.custNo, o.prodNo, quantity

FROM customer c, orders o

WHERE c.custNo = o.custNo (+);

• Note:

• a table may be outer joined with only one other table.

• Which table column to use is important, eg, in above example, do not use o.custNo in place of c.custNo in the SELECT list.

Lec

ture

10

21

Page 22: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

1) Inner Join SQL Example

SELECT Student_Name, Advisor_Name

FROM Students, Advisors

WHERE Students.Advisor_ID= Advisors.Advisor_ID;

Lec

ture

10

22

Page 23: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

2) Left Outer Join SQL Example

SELECT Student_Name, Advisor_Name

FROM Students, Advisors

WHERE Students.Advisor_ID= Advisors.Advisor_ID (+);

Lec

ture

10

23

Page 24: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

3) Right Outer Join SQL Example

SELECT Student_Name, Advisor_Name

FROM Students, Advisors

WHERE Students.Advisor_ID(+)= Advisors.Advisor_ID ;

Student_Name Advisor_Name

Student_1 advisor 1

Student_5 advisor 3

Student_7 advisor 3

Student_9 advisor 1

Student_10 advisor 3

null Advisor 5

Lec

ture

10

24

Page 25: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

4) Full Outer Join SQL Example

SELECT Student_Name, Advisor_Name

FROM Students , Advisors

WHERE Students.Advisor_ID (+) = Advisors.Advisor_ID (+) ;

Lec

ture

10

25

Page 26: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Lec

ture

10

26

Page 27: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Nested Queries (1) • Query results are tables, which can also be queried.

SELECT *

FROM (SELECT prodNo, sum(quantity) AS sum

FROM orders

GROUP BY prodNo)

WHERE sum>10;

Equivalent to

SELECT prodNo, sum(quantity) as sum

FROM orders

GROUP BY prodNo

HAVING sum(quantity)>10;

• The inner query is referred to as a subquery

PRODNO SUM

100 14

101 2

102 1

Lec

ture

10

27

PRODNO SUM

-------------- --------

100 14

Page 28: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Nested Queries (2) • If the query result is a single value, it can be treated as a value,

and be compared with other values.

• Subquery with equality ( < , >) :

Example: Find products with price more than average

SELECT prodNo, price

FROM product

WHERE price > (SELECT AVG(price)

FROM product);

AVG(PRICE)

200

Lec

ture

10

28

PRODNO PRICE

----------------- ----------------

103 300

104 300

prodNo prodName prodDes price

100 P0 Food 100

101 P1 healthy food 100

102 P2 200

103 P3 self_raising flour,80%wheat 300

104 P4 network 80x 300

Page 29: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Subquery

• Subquery with equality (=) : Give a list with first and last names of employees who work in the department

with mail number = 39

SELECT firstName, lastName

FROM EMPLOYEE

WHERE deptNumber =(SELECT deptNumber

FROM DEPARTMENT

WHERE mailNumber = 39);

DEPTNUMBER

D1

Lec

ture

10

29

FIRSTNAME LASTNAME

----------------- ---------

mandy smith

graham burke

Annie nguyen

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

E4 Graham Burke D1 44000

E5 Annie Nguyen D1 60000

Dept Number Dept Name Location Mail Number

D1 Computer Science Bundoora 39

D2 Information Science Bendigo 30

D3 Physics Bundoora 37

D4 Chemistry Bendigo 35

Page 30: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Subquery

• Subquery with aggregate function: Give a list with first, last names and salary of employees whose salary is

greater than average salary for all employees.

SELECT firstName, lastName, salary

FROM EMPLOYEE

WHERE salary > (SELECT avg(salary)

FROM EMPLOYEE);

AVG(SALARY)

51400

Lec

ture

10

30 FIRSTNAME LASTNAME SALARY

----------------- -------------------- ----------

Shaskia Ramanthan 58000

Annie Nguyen 60000

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

E4 Graham Burke D1 44000

E5 Annie Nguyen D1 60000

Dept Number Dept Name Location Mail Number

D1 Computer Science Bundoora 39

D2 Information Science Bendigo 30

D3 Physics Bundoora 37

D4 Chemistry Bendigo 35

Page 31: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Subquery

• Nested Subquery (use of IN): Give a list with first, last names of employees whose departments are located in

Bundoora . SELECT firstName, lastName

FROM EMPLOYEE

WHERE deptNumber IN (SELECT deptNumber

FROM DEPARTMENT

WHERE lo atio = Bundoora ;

deptNumber

D1

D3

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

E4 Graham Burke D1 44000

E5 Annie Nguyen D1 60000

Dept Number Dept Name Location Mail Number

D1 Computer Science Bundoora 39

D2 Information Science Bendigo 30

D3 Physics Bundoora 37

D4 Chemistry Bendigo 35

Lec

ture

10

31

FIRSTNAME LASTNAME

---------------- - -----------------

Annie nguyen

graham burke

mandy smith

Page 32: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Subquery

• List the products ordered by customers living in Riyadh.

SELECT prodNo

FROM orders

WHERE custNo IN (SELECT custNo

FROM customer

WHERE custCity = Riyadh ;

- This query is equivalent to

SELECT prodNo

FROM orders o, customer c

WHERE o.custNo =c.custNo AND custCity = Riyadh';

custNo

2

3

5

Lec

ture

10

32

PRODNO

----------

102

100

100

Page 33: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Lec

ture

10

33

Page 34: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Queries using EXISTS or NOT EXISTS Queries using EXISTS

• Designed for use only with subqueries

• EXISTS return true if there exists at least one row in the result table

returned by the subquery, it is false if the subquery returns an

empty result table.

• Syntax

SELECT column_name

FROM table_name

WHERE EXISTS|NOT EXISTS ( subquery );

Lec

ture

10

34

Page 35: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Queries using EXISTS or NOT EXISTS • Example

SELECT firstName, lastName

FROM EMPLOYEE E

WHERE EXISTS (SELECT * FROM DEPARTMENT D

WHERE E.deptNumber = D.deptNumber

AND D.location = Bendigo );

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

E4 Graham Burke D1 44000

E5 Annie Nguyen D1 60000

Dept Number Dept Name Location Mail Number

D1 Computer Science Bundoora 39

D2 Information Science Bendigo 30

D3 Physics Bundoora 37

D4 Chemistry Bendigo 35

Lec

ture

10

35

FIRSTNAME LASTNAME

---------------- ----------------

Shaskia Ramanthan

Daniel Hodges

Page 36: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Example . EXISTS

• Find all customers who have ordered some products.

SELECT *

FROM customer c

WHERE exists (SELECT *

FROM orders o

WHERE o.custNo =c.custNo);

• If the subquery is not empty, then the exists condition is true.

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

ordNo ordDate custN

o

prodNo quant

ity

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

Lec

ture

10

36

CUSTNO CUNAME CUSTST CUSTCITY AGE

------------ ------------ ---------- --------------- ------------

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

Page 37: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Example . NOT EXISTS • Find all customers such that no order made by them has a

quantity less than 2.

SELECT *

FROM customer c

WHERE NOT EXISTS (SELECT *

FROM orders o

WHERE o.custNo = c.custNo

AND quantity <2);

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

ordNo ordDate custNo prodNo quant

ity

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

Lec

ture

10

37

CUSTNO CUTNAME CUSTST CUSTCITY AGE

-------------- -------------- ------------- -------------- ---------

5 C5 Mains Rd Riyadh

4 C4 Mains Rd Dammam

3 C3 Mains Rd Riyadh 25

Page 38: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Lec

ture

10

38

Page 39: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

UNION • The UNION operator is used to combine the result-set of two or

more SELECT statements.

• Notice that each SELECT statement within the UNION must

1. have the same number of columns.

2. The columns must also have similar data types.

3. the columns in each SELECT statement must be in the same

order.

• Combines the results of two SELECT statements into one result set,

and then eliminates any duplicate rows from that result set.

• SQL UNION Syntax

SELECT column_name(s) FROM table_name1

UNION

SELECT column_name(s) FROM table_name2

Lec

ture

10

39

Page 40: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

UNION • Note: The UNION operator selects only distinct values by

default. To allow duplicate values, use UNION ALL.

• UNION ALL Combines the results of two SELECT statements

into one result set.

• SQL UNION ALL Syntax

SELECT column_name(s) FROM table_name1

UNION ALL

SELECT column_name(s) FROM table_name2

Lec

ture

10

40

Page 41: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

UNION Example 1

• list all the different employees in Norway and USA

SELECT E_Name FROM Employees_Norway

UNION

SELECT E_Name FROM Employees_USA;

E_Name

Hansen, Ola

Svendson, Tove

Svendson, Stephen

Pettersen, Kari

Turner, Sally

Kent, Clark

Scott, Stephen

E_ID E_Name

01 Hansen, Ola

02 Svendson, Tove

03 Svendson, Stephen

04 Pettersen, Kari

E_ID E_Name

01 Turner, Sally

02 Kent, Clark

03 Svendson, Stephen

04 Scott, Stephen

"Employees_Norway" “Employees_USA”

Lec

ture

10

41

Page 42: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

UNION Example 2

SELECT custNo FROM customer

WHERE custCity= Riyadh

UNION

SELECT custNo FROM orders

WHERE prodNo=102; // union of the two queries

custNo custNam

e

custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh

25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

ordNo ordDate custNo prodNo quantity

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

Lec

ture

10

42

CUSTNO

-------------

2

3

5

Page 43: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

MINUS • the MINUS operator returns only unique rows returned by the

first query but not by the second.

• Takes the result set of one SELECT statement, and removes

those rows that are also returned by a second SELECT

statement.

• SQL MINUS Syntax

SELECT column_name(s) FROM table_name1

MINUS

SELECT column_name(s) FROM table_name2 ;

Lec

ture

10

43

Page 44: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

MINUS Example 1

Example: List the products that had never been ordered by customers

SELECT prodNo FROM product

MINUS

SELECT prodNo FROM orders; //difference from the two queries

ordNo ordDate custNo prodNo quantity

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

prodNo prodName prodDes price

100 P0 Food 100

101 P1 healthy food 100

102 P2 200

103 P3 self_raising

flour,80%wheat

300

104 P4 network 80x 300

Lec

ture

10

44

PRODNO

---------------

103

104

Page 45: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

INTERSECT • The INTERSECT operator returns only those rows returned by

both queries.

• Returns only those rows that are returned by each of two

SELECT statements.

• SQL INTERSECT Syntax

SELECT column_name(s) FROM table_name1

INTERSECT

SELECT column_name(s) FROM table_name2 ;

Lec

ture

10

45

Page 46: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

INTERSECT Example: List the customers from Riyadh city who ordered

product 102

SELECT custNo FROM customer

WHERE custCity= Riyadh' INTERSECT

SELECT custNo FROM orders

WHERE prodNo=102; // intersect of the two queries

custN

o

custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh

25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

ordNo ordDate custNo prodNo quantit

y

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

Lec

ture

10

46

CUSTNO

----------------

2

Page 47: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

DEPENDENT

EMPLOYEE

Employee No. First Name Last Name Date of Birth

E1 Joshua Smith 12-Jun-1998

E3 Jay Ramanthan 04-Jan-1996

E1 Jemima Smith 08-Sep-2000

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

Examples

Lec

ture

10

47

Page 48: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Examples SELECT employeeNo, firstName, lastName

FROM EMPLOYEE

UNION

SELECT employeeNo, firstName, lastName

FROM DEPENDENT;

SELECT employeeNo

FROM EMPLOYEE

INTERSECT

SELECT employeeNo

FROM DEPENDENT

Lec

ture

10

48

Page 49: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Lec

ture

10

49

Page 50: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

EMPLOYEE Table Example1

SELECT department_id, count(*), max(salary), min(salary)

FROM employee

GROUP BY department_id;

EMPLOYEE_ID LAST_NAME FIRST_NAME JOB_ID MANAGER_ID SALARY COMM DEPARTMENT_ID

7369 SMITH JOHN 667 7902 800 NULL 20 7499 ALLEN KEVIN 670 7698 1600 300 30 7505 DOYLE JEAN 671 7839 2850 NULL 30 7506 DENNIS LYNN 671 7839 2750 NULL 30 7507 BAKER LESLIE 671 7839 2200 NULL 40 7521 WARK CYNTHIA 670 7698 1250 500 30

Lec

ture

10

50

DEPARTMENT_ID COUNT(*) MAX(SALARY) MIN(SALARY)

------------------------- ------------ -------------------- -------------------

20 1 800 800

40 1 2200 2200

30 4 2850 1250

Page 51: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

EMPLOYEE Table Example2 SELECT Employee_ID, FIRST_NAME,DEPARTMENT_ID

FROM employee

WHERE salary=(SELECT max(salary) FROM employee);

EMPLOYEE_ID LAST_NAME FIRST_NAME JOB_ID MANAGER_ID SALARY COMM DEPARTMENT_ID

7369 SMITH JOHN 667 7902 800 NULL 20 7499 ALLEN KEVIN 670 7698 1600 300 30 7505 DOYLE JEAN 671 7839 2850 NULL 30 7506 DENNIS LYNN 671 7839 2750 NULL 30 7507 BAKER LESLIE 671 7839 2200 NULL 40 7521 WARK CYNTHIA 670 7698 1250 500 30

Lec

ture

10

51 Employee_ID FIRST_NAME DEPARTMENT_ID

---------------- ------------------- -------------------------

7505 JEAN 30

Page 52: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

EMPLOYEE Table Example3

SELECT Employee_ID

FROM employee

WHERE department_id IN (SELECT department_id

FROM department WHERE a e= SALES ;

DEPARTMENT

Department_ID Name Location_ID 10 ACCOUNTING 122 20 RESEARCH 124 30 SALES 123 40 OPERATIONS 167

EMPLOYEE_ID LAST_NAME FIRST_NAME JOB_ID MANAGER_ID SALARY COMM DEPARTMENT_ID

7369 SMITH JOHN 667 7902 800 NULL 20 7499 ALLEN KEVIN 670 7698 1600 300 30 7505 DOYLE JEAN 671 7839 2850 NULL 30 7506 DENNIS LYNN 671 7839 2750 NULL 30 7507 BAKER LESLIE 671 7839 2200 NULL 40 7521 WARK CYNTHIA 670 7698 1250 500 30

Lec

ture

10

52

EMPLOYEE_ID

---------------------

7521

7506

7505

7499

Page 53: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

EMPLOYEE Table Example4

SELECT name

FROM department d

WHERE NOT EXISTS (SELECT last_name

FROM employee e

WHERE d.department_id=e.department_id);

DEPARTMENT

Department_ID Name Location_ID 10 ACCOUNTING 122 20 RESEARCH 124 30 SALES 123 40 OPERATIONS 167

EMPLOYEE_ID LAST_NAME FIRST_NAME JOB_ID MANAGER_ID SALARY COMM DEPARTMENT_ID

7369 SMITH JOHN 667 7902 800 NULL 20 7499 ALLEN KEVIN 670 7698 1600 300 30 7505 DOYLE JEAN 671 7839 2850 NULL 30 7506 DENNIS LYNN 671 7839 2750 NULL 30 7507 BAKER LESLIE 671 7839 2200 NULL 40 7521 WARK CYNTHIA 670 7698 1250 500 30

Lec

ture

10

53 NAME

----------------

ACCOUNTING

Page 54: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Lec

ture

10

54

LAST_NAME DEPARTMENT_ID Department_ID Name

SMITH 20 20 RESEARCH ALLEN 30 30 SALES DOYLE 30 30 SALES

DENNIS 30 30 SALES BAKER 40 40 OPERATIONS WARK 30 30 SALES

EMPLOYEE Table Example4

ACCOUNTING Department Not exist

Page 55: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

EMPLOYEE Table Example5

SELECT last_name, d.department_id, d.name

FROM employee e, department d

WHERE e.department_id (+)= d.department_id AND d.department_id in (SELECT

department_id FROM depart e t WHERE a e IN ACCOUNTING , OPERATIONS ;

DEPARTMENT

Department_ID Name Location_ID 10 ACCOUNTING 122 20 RESEARCH 124 30 SALES 123 40 OPERATIONS 167

EMPLOYEE_ID LAST_NAME FIRST_NAME JOB_ID MANAGER_ID SALARY COMM DEPARTMENT_ID

7369 SMITH JOHN 667 7902 800 NULL 20 7499 ALLEN KEVIN 670 7698 1600 300 30 7505 DOYLE JEAN 671 7839 2850 NULL 30 7506 DENNIS LYNN 671 7839 2750 NULL 30 7507 BAKER LESLIE 671 7839 2200 NULL 40 7521 WARK CYNTHIA 670 7698 1250 500 30

Lec

ture

10

55

LAST_NAME DEPARTMENT_ID NAME

------------------ ------------------------ ----------

BAKER 40 OPERATIONS

10 ACCOUNTING

Page 56: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

EMPLOYEE Table Example5

SELECT last_name, d.department_id, d.name

FROM employee e, department d

WHERE e.department_id (+)= d.department_id AND d.department_id in (SELECT

department_id FROM depart e t WHERE a e IN ACCOUNTING , OPERATIONS ;

LAST_NAME DEPARTMENT_ID Department_ID Name

SMITH 20 20 RESEARCH

ALLEN 30 30 SALES

DOYLE 30 30 SALES

DENNIS 30 30 SALES

BAKER 40 40 OPERATIONS

WARK 30 30 SALES

10 ACCOUNTING Lec

ture

10

56

LAST_NAME DEPARTMENT_ID NAME

------------------ ------------------------ ----------

BAKER 40 OPERATIONS

10 ACCOUNTING

LAST_NAME DEPARTMENT_ID Department_ID Name

SMITH 20 20 RESEARCH

ALLEN 30 30 SALES

DOYLE 30 30 SALES

DENNIS 30 30 SALES

BAKER 40 40 OPERATIONS

WARK 30 30 SALES

10 ACCOUNTING

LAST_NAME Department_ID Name

BAKER 40 OPERATIONS

10 ACCOUNTING

Page 57: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

EMPLOYEE Table Example6

SELECT employee_id, First_name, Last_name, Salary

FROM employee

WHERE last_name like D% ;

EMPLOYEE_ID LAST_NAME FIRST_NAME JOB_ID MANAGER_ID SALARY COMM DEPARTMENT_ID

7369 SMITH JOHN 667 7902 800 NULL 20 7499 ALLEN KEVIN 670 7698 1600 300 30 7505 DOYLE JEAN 671 7839 2850 NULL 30 7506 DENNIS LYNN 671 7839 2750 NULL 30 7507 BAKER LESLIE 671 7839 2200 NULL 40 7521 WARK CYNTHIA 670 7698 1250 500 30

Lec

ture

10

57

EMPL OYEE_ID FIRST_NAME LAST_NAME SALARY

-------------------- ------------------ ------------------- --------------

7505 JEAN DOYLE 2850

7506 LYNN DENNIS 2750

Page 58: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

Lec

ture

10

58

Page 59: Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4 Graham Burke D1 44000 E5 Annie Nguyen D1 60000 Dept Number Dept Name Location Mail Number

References • Data ase Systems: A Practical Approach to Design,

Implementation and Management. Tho as Connolly,

Carolyn Begg. 5th Edition, Addison-Wesley, 2009.

Lec

ture

10

59