You are on page 1of 5

How To Join Two Tables in a Single Query?

Two tables can be joined together in a query in 4 ways:


Inner Join: Returns only rows from both tables that satisfy the join condition.
Left Outer Join: Returns rows from both tables that satisfy the join condition,
and the rest of rows from the first (left) table.
Right Outer Join: Returns rows from both tables that satisfy the join condition,
and the rest of rows from the second (right) table.
Full Outer Join: Returns rows from both tables that satisfy the join condition,
the rest of rows from the first (left) table, and the rest of rows from the seco
nd (right) table.
How To Write a Query with an Inner Join?
If you want to query from two tables with an inner join, you can use the INNER J
OIN ... ON clause in the FROM clause. The following query returns output with an
inner join from two tables: employees and departments. The join condition is th
at the department ID in the employees table equals to the department ID in the d
epartments table:
SQL> SELECT employees.first_name, employees.last_name,
2 departments.department_name
3 FROM employees INNER JOIN departments
4 ON employees.department_id=departments.department_id;
FIRST_NAME LAST_NAME DEPARTMENT_NAME
-------------------- -------------------- ---------------
Steven King Executive
Neena Kochhar Executive
Lex De Haan Executive
Alexander Hunold IT
Bruce Ernst IT
David Austin IT
Valli Pataballa IT
......
Note that when multiple tables are used in a query, column names need to be pref
ixed with table names in case the same column name is used in both tables.
How To Define and Use Table Alias Names?
When column names need to be prefixed with table names, you can define table ali
as name and use them to prefix column names as shown in the following select sta
tement:
SQL> SELECT e.first_name, e.last_name, d.department_name
FROM employees e INNER JOIN departments d
ON e.department_id=d.department_id;
FIRST_NAME LAST_NAME DEPARTMENT_NAME
-------------------- -------------------- ---------------
Steven King Executive
Neena Kochhar Executive
Lex De Haan Executive
Alexander Hunold IT
Bruce Ernst IT
David Austin IT
Valli Pataballa IT
......
How To Write a Query with a Left Outer Join?
If you want to query from two tables with a left outer join, you can use the LEF
T OUTER JOIN ... ON clause in the FROM clause. The following query returns outpu
t with a left outer join from two tables: departments and employees. The join co
ndition is that the manager ID in the departments table equals to the employee I
D in the employees table:
SQL> set NULL 'NULL'
SQL> SELECT d.department_name, e.first_name, e.last_name
2 FROM departments d LEFT OUTER JOIN employees e
3 ON d.manager_id = e.employee_id;
DEPARTMENT_NAME FIRST_NAME LAST_NAME
-------------------- -------------------- --------------
Administration Jennifer Whalen
Marketing Michael Hartstein
Purchasing Den Raphaely
Human Resources Susan Mavris
Shipping Adam Fripp
IT Alexander Hunold
......
Treasury NULL NULL
Corporate Tax NULL NULL
Control And Credit NULL NULL
Shareholder Services NULL NULL
Benefits NULL NULL
Manufacturing NULL NULL
Construction NULL NULL
......
Note that a left outer join may return extra rows from the first (left) table th
at do not satisfy the join condition. In those extra rows, columns from the seco
nd (right) table will be given null values.
The extra rows returned from the left outer join in this example represents depa
rtments that have no manager IDs.
How To Write a Query with a Right Outer Join?
If you want to query from two tables with a right outer join, you can use the RI
GHT OUTER JOIN ... ON clause in the FROM clause. The following query returns out
put with a right outer join from two tables: departments and employees. The join
condition is that the manager ID in the departments table equals to the employe
e ID in the employees table:
SQL> set NULL 'NULL'
SQL> SELECT d.department_name, e.first_name, e.last_name
2 FROM departments d RIGHT OUTER JOIN employees e
3 ON d.manager_id = e.employee_id;
DEPARTMENT_NAME FIRST_NAME LAST_NAME
-------------------- -------------------- ---------------
Administration Jennifer Whalen
Marketing Michael Hartstein
Purchasing Den Raphaely
Human Resources Susan Mavris
Shipping Adam Fripp
IT Alexander Hunold
......
NULL Clara Vishney
NULL Jason Mallin
NULL Hazel Philtanker
NULL Nanette Cambrault
NULL Alana Walsh
NULL Karen Partners
NULL Bruce Ernst
......
Note that a right outer join may return extra rows from the second (right) table
that do not satisfy the join condition. In those extra rows, columns from the f
irst (left) table will be given null values.
The extra rows returned from the right outer join in this example represents emp
loyees that are not assigned as managers in the departments table.
How To Write a Query with a Full Outer Join?
If you want to query from two tables with a full outer join, you can use the FUL
L OUTER JOIN ... ON clause in the FROM clause. The following query returns outpu
t with a full outer join from two tables: departments and employees. The join co
ndition is that the manager ID in the departments table equals to the employee I
D in the employees table:
SQL> set NULL 'NULL'
SQL> SELECT d.department_name, e.first_name, e.last_name
2 FROM departments d FULL OUTER JOIN employees e
3 ON d.manager_id = e.employee_id;
DEPARTMENT_NAME FIRST_NAME LAST_NAME
-------------------- -------------------- --------------
Administration Jennifer Whalen
Marketing Michael Hartstein
Purchasing Den Raphaely
Human Resources Susan Mavris
Shipping Adam Fripp
IT Alexander Hunold
......
Treasury NULL NULL
Corporate Tax NULL NULL
Control And Credit NULL NULL
Shareholder Services NULL NULL
Benefits NULL NULL
Manufacturing NULL NULL
Construction NULL NULL
......
NULL Clara Vishney
NULL Jason Mallin
NULL Hazel Philtanker
NULL Nanette Cambrault
NULL Alana Walsh
NULL Karen Partners
NULL Bruce Ernst
......
Note that a right outer join may return two sets of extra rows: one set from the
first (left) table that do not satisfy the join condition, and the other set fr
om the second (right) table that do not satisfy the join condition.
How To Write an Inner Join with the WHERE Clause?
If you don't want to use the INNER JOIN ... ON clause to write an inner join, yo
u can put the join condition in the WHERE clause as shown in the following query
example:
SQL> SELECT d.department_name, e.first_name, e.last_name
2 FROM departments d, employees e
3 WHERE d.manager_id = e.employee_id;
DEPARTMENT_NAME FIRST_NAME LAST_NAME
-------------------- -------------------- --------------
Administration Jennifer Whalen
Marketing Michael Hartstein
Purchasing Den Raphaely
Human Resources Susan Mavris
Shipping Adam Fripp
IT Alexander Hunold
......
How To Write a Left Outer Join with the WHERE Clause?
If you don't want to use the LEFT OUTER JOIN ... ON clause to write a left outer
join, you can use a special criteria in the WHERE clause as "left_table.column
= right_table.column(+)". The select statement below is an example of a left out
er join written with the WHERE clause:
SQL> set NULL 'NULL'
SQL> SELECT d.department_name, e.first_name, e.last_name
2 FROM departments d, employees e
3 WHERE d.manager_id = e.employee_id(+);
DEPARTMENT_NAME FIRST_NAME LAST_NAME
-------------------- -------------------- --------------
Administration Jennifer Whalen
Marketing Michael Hartstein
Purchasing Den Raphaely
Human Resources Susan Mavris
Shipping Adam Fripp
IT Alexander Hunold
......
Treasury NULL NULL
Corporate Tax NULL NULL
Control And Credit NULL NULL
Shareholder Services NULL NULL
Benefits NULL NULL
Manufacturing NULL NULL
......
Note that a left outer join may return extra rows from the first (left) table th
at do not satisfy the join condition. In those extra rows, columns from the seco
nd (right) table will be given null values.
The extra rows returned from the left outer join in this example represents depa
rtments that have no manager IDs.
How To Name Query Output Columns?
Each column in the query output has a default name. If you don't like the defaul
t name, you can specify a new name for any column in the query output by using t
he AS clause. The following statement shows you a good example:
SQL> SELECT department_id AS ID, MIN(salary) AS Low,
2 MAX(salary) AS High, AVG(salary) AS Average
3 FROM employees GROUP BY department_id
4 HAVING AVG(salary) < 5000;
ID LOW HIGH AVERAGE
---------- ---------- ---------- ----------
30 2500 11000 4150
50 2100 8200 3475.55556
10 4400 4400 4400
What Is a Subquery?
A subquery is a SELECT statement used as part of the selection criteria of the m
ain SELECT statement. The subquery specified in the WHERE clause will be evaluat
ed repeated on each row of the selection base table. The output of the subquery
will be used in the final evaluation of the criteria. Usually, subqueries are us
ed in the following boolean operations:
"expression IN (subquery)"
"expression NOT IN (subquery)"
"EXISTS (subquery)"
"NOT EXISTS (subquery)"
How To Use Subqueries with the IN Operator?
A subquery can be used with the IN operator as "expression IN (subquery)". The s
ubquery should return a single column with one or more rows to form a list of va
lues to be used by the IN operation. The following tutorial exercise shows you h
ow to use a subquery with the IN operator:
SQL> SELECT first_name, last_name FROM employees
2 WHERE department_id IN (
3 SELECT department_id FROM departments
4 WHERE location_id = 1700
5 );
FIRST_NAME LAST_NAME
-------------------- -------------------------
Steven King
Neena Kochhar
Lex De Haan
Nancy Greenberg
Daniel Faviet
John Chen
Ismael Sciarra
......
How To Use Subqueries with the EXISTS Operator?
A subquery can be used with the EXISTS operator as "EXISTS (subquery)", which re
turns true if the subquery returns one or more rows. The following statement is
a good example of "EXISTS (subquery)". It returns rows from employees table that
there are rows existing in the departments table linked to the employees table
with location_id = 1700.
SQL> SELECT first_name, last_name FROM employees e
2 WHERE EXISTS (
3 SELECT * FROM departments d
4 WHERE e.department_id = d.department_id
5 AND d.location_id = 1700
6 );
FIRST_NAME LAST_NAME
-------------------- -------------------------
Steven King
Neena Kochhar
Lex De Haan
Nancy Greenberg
Daniel Faviet
John Chen
Ismael Sciarra
......

You might also like