Site icon IT Tutorial

Oracle SQL Tutorials – Chapter 5

Chapter 5 – Selecting Data From Multiple Tables

 

Combining Tables – JOIN

 

INNER JOIN

SELECT department_id, department_name, city

FROM hr.departments d, hr.locations l

WHERE d.location_id=l.location_id;

 

SELECT department_id, department_name, city

FROM hr.departments d

INNER JOIN hr.locations l ON d.location_id=l.location_id;

OR

SELECT department_id, department_name, city

FROM hr.departments d

JOIN hr.locations l USING (location_id);

 

SELECT employee_id, first_name,last_name,city, department_name

FROM hr.employees e

JOIN hr.departments d ON d.department_id = e.department_id

JOIN hr.locations l ON d.location_id = l.location_id;

 

SELECT e.first_name,e.last_name, e.salary, j.job_title

FROM hr.employees e

JOIN hr.jobs j ON e.salary

BETWEEN j.min_salary AND j.max_salary;

 

LEFT OUTER JOIN

SELECT first_name,e.last_name, e.department_id, d.department_name

FROM hr.employees e

LEFT OUTER JOIN hr.departments d ON (e.department_id = d.department_id) ;

 

SELECT first_name,e.last_name, e.department_id, d.department_name

FROM hr.employees e

RIGHT OUTER JOIN hr.departments d ON (e.department_id = d.department_id) ;

 

CARTESIAN PRODUCT

SELECT COUNT(*) FROM hr.regions ;                                  SELECT COUNT(*) FROM hr.locations ;

                                                                                

 

SELECT *

FROM hr.regions, hr.locations ;

 

SET OPERATORS

UNION OPERATOR

 

SELECT employee_id, job_id

FROM hr.employees

UNION

SELECT employee_id, job_id

FROM hr.job_history;

 

UNION ALL OPERATOR

 

SELECT employee_id, job_id, department_id

FROM hr.employees

UNION ALL

SELECT employee_id, job_id, department_id

FROM hr.job_history ;

 

INTERSECT OPERATOR

SELECT first_name,last_name,salary FROM hr.employees

WHERE salary BETWEEN 1000 AND 5000

INTERSECT

SELECT first_name,last_name,salary FROM hr.employees

WHERE salary BETWEEN 3000 AND 7500;

 

MINUS OPERATOR

 

SELECT employee_id, job_id

FROM hr.employees

MINUS

SELECT employee_id, job_id

FROM hr.job_history;

 

SUB QUERIES

 

SELECT first_name,last_name, salary

FROM hr.employees

WHERE salary >

(SELECT salary

FROM hr.employees

WHERE last_name = 'Abel');

 

SUBQUERY VARIATIONS

SINGLE-ROW SUBQUERIES

 

SELECT first_name,last_name, job_id, salary

FROM hr.employees

WHERE job_id =

(SELECT job_id

FROM hr.employees

WHERE last_name = 'Austin')

AND salary >

(SELECT salary

FROM hr.employees

WHERE last_name = 'Austin');

 

SELECT first_name,last_name, job_id, salary

FROM hr.employees

WHERE salary =

(SELECT MIN(salary)

FROM hr.employees);

 

SELECT d.department_name,e.department_id, MIN(salary)

FROM hr.employees e,hr.departments d where e.department_id=d.department_id

GROUP BY e.department_id,d.department_name

HAVING MIN(salary) >

(SELECT MIN(salary)

FROM hr.employees

WHERE department_id = 50);

 

MULTI-ROW SUBQUERIES

IN OPERATOR

SELECT employee_id, first_name,last_name,salary

FROM hr.employees

WHERE salary in

(SELECT MIN(salary)

FROM hr.employees

GROUP BY department_id);

 

ANY OPERATOR

SELECT employee_id, last_name, job_id, salary

FROM hr.employees

WHERE salary < ANY

(SELECT salary

FROM hr.employees

WHERE job_id = 'IT_PROG')

AND job_id <> 'IT_PROG';

 

SELECT employee_id, last_name, job_id, salary

FROM hr.employees

WHERE salary > ANY

(SELECT salary

FROM hr.employees

WHERE job_id = 'IT_PROG')

AND job_id <> 'IT_PROG';

 

ALL OPERATOR

SELECT employee_id, first_name, last_name, job_id, salary

FROM hr.employees

WHERE salary < ALL

(SELECT salary

FROM hr.employees

WHERE job_id = 'IT_PROG')

AND job_id <> 'IT_PROG';

 

SELECT employee_id, first_name, last_name, job_id, salary

FROM hr.employees

WHERE salary > ALL

(SELECT salary

FROM hr.employees

WHERE job_id = 'IT_PROG')

AND job_id <> 'IT_PROG';

 

Exit mobile version