LECTURE10: DATA MANIPULATION IN SQL , ADVANCED SQL … · Outer Joins in Oracle SQL Lecture10 21...
Transcript of LECTURE10: DATA MANIPULATION IN SQL , ADVANCED SQL … · Outer Joins in Oracle SQL Lecture10 21...
LECTURE10:
DATA MANIPULATION IN SQL , ADVANCED
SQL QUERIES
1
Ref. Chapter5
From
“Database Systems: A Practical Approach to Design, Implementation and Management.”Thomas Connolly, Carolyn Begg.
I S 2 2 0 : D a t a b a s e F u n d a m e n t a l s
The Process of Database Design2
Conceptual Design (ERD)
Logical Design
(Relational Model)
Physical Design
Create schema
(DDL)
Load Data
(DML)
Sample Data in Customer Table
Lecture10
3
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
Sample Data in Product Table
Lecture10
4
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
Sample Data in Orders Table
Lecture10
5
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
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
Lecture106
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
Lecture107
Sample Data in Customer Table
Table names and Column names
Lecture10
8
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;
Alias
Lecture10
9
SQL aliases are used to temporarily rename a table or a column heading.
Syntax for Columns
Syntax for Tables
SELECT column_name AS alias_name
FROM table_name;
SELECT column_name AS alias_name
FROM table_name;
SELECT column_name(s)
FROM table_name [AS] alias_name;
SELECT column_name(s)
FROM table_name [AS] alias_name;
Alias ( important note )
Lecture10
10
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.OrderDateFROM Orders AS o;
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 :
11
Lecture10
JOIN
12
Lecture10
JOIN
Lecture10
13
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.
Types of JOIN
Lecture10
14
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.
Types of JOIN
Lecture10
15
SQL Examples of Joins ( 1)
Lecture10
16
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
Lecture10
17
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:
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 )
Lect
ure1
0
18
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
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 )
Lecture10
19
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
CUSTNO CUSTNAME CUSTST CUSTCITY
---------------- ----------------- ------------- ----------------
1 C1 Olaya St Jeddah
2 C2 Mains St Riyadh
3 C3 Mains Rd Riyadh
SQL Examples of Joins ( 4 )20
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
SUM(PRICE*QUANTITY)
----------------------------------
400
Outer Joins in Oracle SQL
Lecture10
21
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.
Lecture10
22
1) Inner Join SQL Example
Lecture10
23
SELECT Student_Name, Advisor_Name
FROM Students, Advisors
WHERE Students.Advisor_ID= Advisors.Advisor_ID;
2) Left Outer Join SQL Example
Lecture10
24
SELECT Student_Name, Advisor_Name
FROM Students, Advisors
WHERE Students.Advisor_ID= Advisors.Advisor_ID (+);
3) Right Outer Join SQL Example
Lecture10
25
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
4) Full Outer Join SQL Example
Lecture10
26
SELECT Student_Name, Advisor_Name
FROM Students , Advisors
WHERE Students.Advisor_ID (+) = Advisors.Advisor_ID (+) ;
Lecture1027
Nested Queries (1)
Lecture10
28
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
PRODNO SUM
-------------- --------
100 14
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
Nested Queries (2)
Lecture10
29
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
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
Lecture10
30
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
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
Lecture10
31
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
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
Lecture10
32
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 location = ‘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
FIRSTNAME LASTNAME
---------------- - -----------------
Annie nguyen
graham burke
mandy smith
Subquery
Lecture10
33
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
PRODNO
----------
102
100
100
Lecture1034
Queries using EXISTS or NOT EXISTS
Lecture10
35
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 );
SELECT column_name
FROM table_name
WHERE EXISTS|NOT EXISTS ( subquery );
Queries using EXISTS or NOT EXISTS
Lecture10
36
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
FIRSTNAME LASTNAME
---------------- ----------------
Shaskia Ramanthan
Daniel Hodges
Example . EXISTS
Lecture10
37
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 custNo prodNo quanti
ty
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
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
Lecture10
38
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 ag
e
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 quanti
ty
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
CUSTNO CUTNAME CUSTST CUSTCITY AGE
-------------- -------------- ------------- -------------- ---------
5 C5 Mains Rd Riyadh
4 C4 Mains Rd Dammam
3 C3 Mains Rd Riyadh 25
Lecture1039
UNION
Lecture10
40
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;
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2;
UNION
Lecture10
41
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
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
UNION Example 1
Lecture10
42
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”
UNION Example 2
Lecture10
43
SELECT custNo FROM customer
WHERE custCity=‘Riyadh’
UNION
SELECT custNo FROM orders
WHERE prodNo=102; // union of the two queries
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 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
CUSTNO
-------------
2
3
5
MINUS
Lecture10
44
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 ;
SELECT column_name(s) FROM table_name1
MINUS
SELECT column_name(s) FROM table_name2 ;
MINUS Example 1
Lecture10
45
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
PRODNO
---------------
103
104
INTERSECT
Lecture10
46
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 ;
SELECT column_name(s) FROM table_name1
INTERSECT
SELECT column_name(s) FROM table_name2 ;
INTERSECT
Lecture10
47
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
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
Lecture10
48
Examples
Lecture10
49
SELECT employeeNo, firstName, lastName
FROM EMPLOYEE
UNION
SELECT employeeNo, firstName, lastName
FROM DEPENDENT;
SELECT employeeNo
FROM EMPLOYEE
INTERSECT
SELECT employeeNo
FROM DEPENDENT
Lecture1050
EMPLOYEE Table Example1
Lecture10
51
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
DEPARTMENT_ID COUNT(*) MAX(SALARY) MIN(SALARY)
------------------------- ------------ -------------------- -------------------
20 1 800 800
40 1 2200 2200
30 4 2850 1250
EMPLOYEE Table Example2
Lecture10
52
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
Employee_ID FIRST_NAME DEPARTMENT_ID
---------------- ------------------- -------------------------
7505 JEAN 30
EMPLOYEE Table Example3
Lecture10
53
SELECT Employee_ID
FROM employee
WHERE department_id IN (SELECT department_id
FROM department WHERE name=’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
EMPLOYEE_ID
---------------------
7521
7506
7505
7499
EMPLOYEE Table Example4
Lecture10
54
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
NAME
----------------
ACCOUNTING
Lecture10
55
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
Lecture10
56
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 department WHERE name 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
LAST_NAME DEPARTMENT_ID NAME
------------------ ------------------------ ----------
BAKER 40 OPERATIONS
10 ACCOUNTING
EMPLOYEE Table Example5
Lecture10
57
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 department WHERE name 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 ACCOUNTINGLAST_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
Lecture10
58
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
EMPL OYEE_ID FIRST_NAME LAST_NAME SALARY
-------------------- ------------------ ------------------- --------------
7505 JEAN DOYLE 2850
7506 LYNN DENNIS 2750
Lecture1059