Vous êtes sur la page 1sur 6

Joins:

Oracle:
1. Equijoin
2. Non Equijoin
3. Outer join
4. Self Join
5. Cartesian Product
SQL (Language)
1. Natural Join
2. Using Join
3. ON join
4. Left/Right/Full Outer Join
5. Cross Join

1. Equijoin:
(It joins two or more table based on equal columns and
equal values. If the values don’t match then it will skip
those values (Not show)
Syntax:
SELECT column_list
From table1, table2
Where table1.column=table2.column;
Examples:
SELECT employee_id,last_name,salary,employees.department_id, department_name
From employees , departments
where employees.department_id=departments.department_id
Order by 1
/

Table Prefix:
(To rename table for particular query only)
SELECT employee_id,last_name,job_title
From employees e,jobs j
Where e.job_id=j.JOB_ID;

SELECT employee_id,last_name,city,department_name
From employees e,departments d, locations l
where e.department_id=d.department_id
AND d.location_id=l.location_id

SELECT employee_id,last_name,city,department_name,SALARY
From employees e,departments d, locations l
where e.department_id=d.department_id
AND d.location_id=l.location_id
and salary> 3000
and city='South San Francisco'
/

SELECT employee_id,First_name||' '||last_name Full_name,department_name , City


From employees e, departments d, locations l
Where e.department_id=d.department_id
And l.location_id=d.location_id;

SELECT employee_id,last_name,job_title,salary
From employees emp,jobs j
where emp.job_id=j.job_id
and job_title LIKE '%Marketing%';
2. Non Equijoin
(To join unmatched columns that are not match physically in two or more
table’s data as well as values)

SELECT employee_id,last_name,Salary , Gra


From employees e,grade g
Where e.salary Between g.low_sal and Hig_sal;

select employee_id,last_name,hire_date,salary,boun_pct
From employees e, bouns b
where Round((sysdate-hire_date)/365) Between Exp_Start and Exp_END
Order by 1

3. Outer Join
(It shows the equal values records as well as unmatched records that
are not matched in equijoin)
Syntax:
SELECT column_list
From table1 t1, table t2
Where t1.column (+) =t2.column;

SELECT employee_id,last_name,salary,d.department_id, department_name


From employees e, departments d
where e.department_id=d.department_id(+)
Order by 1
/

SELECT employee_id,city
From employees e,departments d,locations l
where e.department_id(+)=d.department_id
And d.location_id (+)=l.location_id
and employee_id IS NULL
and city<>'Seattle'
4. Self Join
(To join table itself it called self join )

SELECT emp.employee_id,emp.last_name Employee_name,mgr.last_name


Manger_name
From employees emp,employees mgr
Where emp.manager_id=mgr.employee_id;

SELECT m.employee_id,m.last_name,e.last_name
From employees e,employees m
where e.employee_id(+)=m.manager_id
order by 1;

SELECT e.department_id,e.last_name Employee,c.last_name Colleague


From employees e,employees c
Where e.department_id=c.department_id
AND e.last_name<>c.last_name
and e.department_id IN (20,30);

5.Cartesian Product
1. Where clause omits
2. Wrong condition of join

select employee_id,last_name,salary,department_name
from employees e, departments d
where e.manager_id=d.manager_id;

select employee_id,last_name,salary , department_name


From employees , departments;
SQL :
1. Natural Join:
Syntax:
SELECT column_list
From table1 Natural Join table2;

SELECT employee_id,last_name,salary,department_name
From employees natural Join departments;

SELECT employee_id,last_name,salary,department_name,city
From employees natural Join departments natural join locations;

2. Using Clause join:


Syntax:
SELECT column_list
From table1 Join table2
Using (column_name);

(table prefix not allowed for before same column that is used in join
condition )

SELECT employee_id,last_name, salary,department_id,department_name


From employees join departments
Using (department_id);

SELECT employee_id,last_name,e.salary,d.department_name,city
From employees e Join departments d
Using (department_id)
Join locations l
Using(location_id);
3. On Clause Join:
SELECT column list
From table1 t1 Join table2 t2
On (t1.column_name=t2.column_name);

SELECT employee_id,last_name,e.salary,d.department_id,d.department_name,city
From employees e Join departments d
On(e.department_id=d.department_id)
Join locations l
ON(d.location_id=l.location_id);

SELECT e.employee_id,e.last_name,m.last_name
From employees e Join employees m
on (e.manager_id=m.employee_id);

4. Left/Right /Full Outer Join:


SELECT column list
From table1 t1 Left/Right/Full outer Join table2 t2
On (t1.column_name=t2.column_name);

SELECT e.employee_id,e.last_name,m.last_name
From employees e right outer Join employees m
on (e.manager_id=m.employee_id);

SELECT employee_id,last_name,department_name
From employees e Full Outer Join departments d
On (e.department_id=d.department_id);

5. Cross Join:

SELECT employee_id,last_name,department_name
From employees e cross Join departments d cross join locations

Vous aimerez peut-être aussi