Anda di halaman 1dari 50

Database Management & Administration

Join & Subquery

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Union, Intersection & Minus


The number of columns and the data types of

the columns being selected must be identical Union Returns all the tuples that are in any of the table eliminating the duplicates Intersect Returns all the tuples that are in both the tables Minus Returns all the tuples that are in Table 1 but not in Table 2

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Employee
Employee_Name Mr. A Mr. B Mr. C Mr. D Salary 12000 12300 14000 15070

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Employee2 Table
Employee_Name Mr. B Mr. F Mr. E Mr. D Mr. G Salary 12300 14600 17250 15070 18250

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Insert
Select Employee_Name, Salary from Employee

union select Employee_Name, Salary from Employee 2


Employee_Name Mr. C Mr. D Salary 14000 15070

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Union
Select Employee_Name, Salary from Employee

intersect select Employee_Name, Salary from Employee 2


Employee_Name Mr. A Mr. B Mr. C Mr. D Mr. F Mr. E Mr. G Salary 12000 12300 14000 15070 14600 17250 18250
Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Minus
Select Employee_Name, Salary from Employee

minus select Employee_Name, Salary from Employee 2


Employee_Name Mr. A Mr. B Salary 12000 12300

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Join
Joins are very important for relational

databases. Mainly used when we need to find information that distributes over multiple tables.

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Employee & Branch

Employee

Branch

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Employee Table
create table employee

( employee_id number primary key, name varchar2(50), branc_id number references branch(branch_id) );

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

10

Branch Table
create table branch

( branch_id number primary key, branch_name varcha2(50) );

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

11

Employee Table With Data


Employee_id 1 2 3 4 5 Name Mr. A Mr. B Mr. C Mr. D Mr. E Brach_id 2 1 1 1 2

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

12

Branch Table With Data


Branch_id 1 2 Branch_Name Polashi Bokshibazar

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

13

Joining
select * from employee, branch;

join

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

14

Joining of Employee and Branch


Employee_Name 1 Mr. A id 1 Mr. A 2 Mr. B 2 Mr. B 3 Mr. C 3 Mr. C 4 Mr. D 4 Mr. D 5 Mr. E 5 Mr. E Branch_i Branch_i Branch_Name 2 1 Polashi d d 2 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 2 1 Polashi 2 2 Bokshibazar

Employee

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Branch

15

To Selecting The Tuples


All the tuples from a join are not consistent. We have to eliminate the inconsistent tuples. To select the consistent tuples, where clause

should be used with join To uniquely identify the column name (if same name), the name should be preceded with table_nameand dot(.). If the column names do not conflict then they can be used without the table name. select * from employee, branch where employee.branch_id = branch.branch_id;
Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

16

Eliminitating Inconsistent Data


Employee_Name 1 Mr. A id 2 Mr. B 3 Mr. C 4 Mr. D 5 Mr. E Branch_i Branch_i Branch_Name 2 2 Bokshibazar d d 1 1 Polashi 1 1 Polashi 1 1 Polashi 2 2 Bokshibazar

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

17

Joining More
Any number of tables can be joined in joining select * from

table1, table2, ....... tableN where table1.col11 =table2.col21 and ......and tablen1 .coln1 =tablem1 .colm1 ;

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

18

Joining Table with itself


Here Manager_id is the employee_id of a

employees manager
Employee Manager

Employee_id 1 2 3 4 5

Name Mr. A Mr. B Mr. C Mr. D Mr. E

Manager_id 2 4 null 3 2
19

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Example
List the names of all employees together with

the name of their manager. Two same tables. S0 removing the conflict alias should be given to table

select e1.name, e2.name from employee

as e1, employee as e2 wheree1.manager_id=e2.employee_id;



Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

20

Inner Join
The joining done so far also called Inner Join. Oracle has another syntax. select * from

table1 inner join table2 on table1.col1 = table2.col2 on employee.branch_id = branch.branch_id;

select * from employee inner join branch

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

21

Natural Join
Automatically joins the two tables that have a

commonly named and defined field. The join returns the tuples which have the same value in the columns of same name. select * from employee natural join branch; Here, branch_id is the same column exists in both employee and branch table.

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

22

Outer Join
Three types
Left Outer Join Right Outer Join Full Outer Join

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

23

Left Outer Join


All tuples from the left table must be in the

tuples selected. If a tuple from the left table is not matched with any tuples in the right table, it will have null in all the fields for the right table column

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

24

Right Outer Join


All tuples from the right table must be in the

tuples selected. If a tuple from the right table is not matched with any tuples in the left table, it will have null in all the fields for the left table column

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

25

Full Outer Join


All tuples from both the table must be in the

selected tuple. If a tuple from the left table is not matched with any tuple from the right table, then it will have null values in the right table column If a tuple from the right table is not matched with any tuple from the left table, then it will have null values in the left table column

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

26

Employee and Branch Data Modified


Employee_id 1 2 3 4 5 6 Branch_id 1 2 3 Name Mr. A Mr. B Mr. C Mr. D Mr. E Mr. F Branch_Name Polashi Bokshibazar Motijheel
Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

Branch_id 2 1 1 1 2 5

27

Left Outer Join


select * from employee left outer join branch

on employee.branch_id = branch.branch_id; select * from employee, branch where employee.branch_id = branch.branch_id(+);

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

28

Left Outer Join Example


Employee_Name 1 Mr. A id 1 Mr. A 2 Mr. B 2 Mr. B 3 Mr. C 3 Mr. C 4 Mr. D 4 Mr. D 5 Mr. E 5 Mr. E 6 Mr. F Branch_i Branch_i Branch_Name 2 1 Polashi d d 2 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 2 1 Polashi 2 2 Bokshibazar 5 null null
Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

29

Right Outer Join


select * from employee right outer join branch

on employee.branch_id = branch.branch_id; select * from employee, branch where employee.branch_id(+) = branch.branch_id;

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

30

Right Outer Join Example


Employee_Name 1 Mr. A id 1 Mr. A 2 Mr. B 2 Mr. B 3 Mr. C 3 Mr. C 4 Mr. D 4 Mr. D 5 Mr. E 5 Mr. E null null Branch_i Branch_i Branch_Name 2 1 Polashi d d 2 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 2 1 Polashi 2 2 Bokshibazar null 3 Motijheel
Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

31

Full Outer Join


select * from employee full outer join branch on

employee.branch_id = branch.branch_id;

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

32

Full Outer Join


Employee_Name 1 Mr. A id 1 Mr. A 2 Mr. B 2 Mr. B 3 Mr. C 3 Mr. C 4 Mr. D 4 Mr. D 5 Mr. E 5 Mr. E 6 Mr. F null null Branch_i Branch_i Branch_Name 2 1 Polashi d d 2 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 1 1 Polashi 1 2 Bokshibazar 2 1 Polashi 2 2 Bokshibazar 5 null null null 3 Motijheel
Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

33

Sub Query
a query result can also be used in a condition

of a where clause. in such a case the query is called a subquery and the complete select statement is called a nested query. We can refer the top query table from sub query. View the sub query as if it is executed for each tuple in top query

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

34

In Where Clause
A respective condition in the where clause then can

have one of the following forms:

1. Set-valued subqueries <expression> [not] in (<subquery>) <expression> <comparison operator> [any|all] (<subquery>)

An <expression> can either be a column or a

computed value.

2. Test for (non)existence [not] exists (<subquery>)

In a where clause conditions using subqueries can

be combined arbitrarily by using the logical connectives and and or.


Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

35

Example
Select the employees who works in Palashi

Branch
select * from employee where branch_id

in(select branch_id from branch where name=Palashi);


Select the employees who does not work in

Palashi Branch
select * from employee where branch_id not

in(select branch_id from branch where name=Palashi);

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

36

Caution
You can use (=) for checking condition, if you

are sure enough that your subquery returns a single value. Better, use in. Because it eliminates the necessity of single return value. And also works for the single return value.

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

37

Exists/Not Exists
Often a query result depends on whether

certain rows do (not) exist in (other) tables. Such type of queries is formulated using the exists operator. List all branch that have no employees
select * from branch

where not exists (select * from employee where branch_id = branch.branch_id);

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

38

Modified Employee Table


Employee_id 1 2 3 4 5 6 Name Mr. A Mr. B Mr. C Mr. D Mr. E Mr. F Branch_id 2 1 1 1 2 5 Salary 12000 14000 12000 15000 14000 13000

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

39

Any
For the clause any, the condition evaluates

to true if there exists at least on row selected by the sub query for which the comparison holds. If the sub query yields an empty result set, the condition is not satisfied. Retrieve all employees who are working in branch 1 and who earn at least as much as any employee working in department 2.
select * from employee where branch_id = 1

and salary>=any(select salary from employee where branch_id=2);

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

40

All
For the clause all, the condition evaluates

to true if for all rows selected by the sub query the comparison holds. In this case the condition evaluates to true if the sub query does not yield any row or value. select the employee with maximum salary
select * from employee where salary

>=all(select salary from employee);

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

41

Subquery in From Clause


As any select query returns a temporary table,

thus subquery can be joined with a table. select ............ from table1, table2...., (select ..... from table_s ......)subqueryName, ..... here, subqueryNamecan be used to reference a column from subquery. select the employee, salary and average salary. Employee_name Salary Average_salary
select employee_name, salary,

subquery.average_salary from employee, (select avg(salary) as average_salary from employee)subquery;


Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

42

Sequence
Oracle provides an object called a Sequence

that can generate numeric value. Provides


Generating the number in ascending or

descending order The maximum number that can be generated by sequence The increment value for generating the next number

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

43

Syntax
CREATE SEQUENCE <sequenceName>

[INCREMENT BY <integerValue> START WITH <integerValue> MAXVALUE <integerValue>/ NOMAXVALUE MINVALUE <integerValue> / NOMINVALUE CYCLE/NOCYCLE CACHE <integerValue>/NOCACHE ORDER/NOORDER]

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

44

Example
create sequence employee_id_seq Incremented

by 1 start with 1 minvalue 1 maxvalue 999 cycle;

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

45

Referencing a Sequence
Every time nextval references a sequence its

output automatically incremented. select <sequenceName>.NextVal from dual; select employee_id_seq .nextval from dual;

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

46

Altering a Sequence
Alter Sequence <sequenceName>

[ <propertyName> newValue]; property name is any property that is used in sequence parameter. start value cannot be altered alter sequence employee_id_seq Incremeneted by 2;

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

47

Dropping a Sequence
drop sequence <sequenceName> drop sequence employee_id_seq ;

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

48

Greatest
Greatest(expr1, expr2, ......, exprn)
select greatest(4, 5, 17) from dual;

Greatest can also be used to find the max

value row wise. [note: Max search column wise] select greatest(A, B) from table1;

Table1

A 5 25

B 10 15

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

49

Least
Least(expr1, expr2, ......, exprn)
select greatest(4, 5, 17) from dual;

Least can also be used to find the min value

row wise. [note: Min search column wise] select least(A, B) from table1;

Table1

A 5 25

B 10 15

Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET

50

Anda mungkin juga menyukai