Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4...
Transcript of Lecture 10 - · PDF fileLecture 10 : Data Manipulation in SQL , Advanced SQL queries ... E4...
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
The Process of Database Design Real World
Domain
Conceptual model (ERD)
Relational Data Model
Create schema
(DDL)
Load Data
(DML) Lec
ture
10
2
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
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
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
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
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
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
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;
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
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
JOIN
Lec
ture
10
12
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
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
Types of JOIN
Lec
ture
10
15
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
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
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
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
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
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
1) Inner Join SQL Example
SELECT Student_Name, Advisor_Name
FROM Students, Advisors
WHERE Students.Advisor_ID= Advisors.Advisor_ID;
Lec
ture
10
22
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
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
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
Lec
ture
10
26
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
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
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
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
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
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
Lec
ture
10
33
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
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
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
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
Lec
ture
10
38
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
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
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
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
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
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
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
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
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
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
Lec
ture
10
49
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
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
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
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
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
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
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
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
Lec
ture
10
58
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