Sei sulla pagina 1di 14

SQL: Joins

SQL JOINS are used in a SQL query to combine rows from multiple tables. A SQL JOIN is performed
whenever two or more tables are joined in a SQL statement.
There are 4 different types of SQL joins:
INNER JOIN (or sometimes called simple join)
LEFT OUTER JOIN (or sometimes called LEFT JOIN)
RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
FULL OUTER JOIN (or sometimes called FULL JOIN)
So let's discuss SQL JOINS by exploring the syntax, looking at visual illustrations and working through
some SQL JOIN examples.
SQL INNER JOIN (simple join)
Chances are, you've already written a SQL statement that uses an SQL INNER JOIN. It is the most
common type of SQL join. SQL INNER JOINS return all rows from multiple tables where the join condition
is met.
SQL INNER JOIN Syntax
The syntax for the SQL INNER JOIN is:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Visual Illustration of SQL INNER JOIN
In this visual diagram, the SQL INNER JOIN returns the shaded area:

The SQL INNER JOIN would return the records where table1 and table2 intersect.
SQL INNER JOIN Examples
Here is an example of a SQL INNER JOIN:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This SQL INNER JOIN example would return all rows from the suppliers and orders tables where there is
a matching supplier_id value in both the suppliers and orders tables.
Let's look at some data to explain how the INNER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_ name). It contains the following
data:
supplier_id supplier_name
10000 IBM
10001 Hewlett Packard
10002 Microsoft
10003 NVIDIA
We have another table called orders with three fields (order_id, supplier_id, and order_date). It contains
the following data:
order_id supplier_id order_date
500125 10000 2003/05/12
500126 10001 2003/05/13
500127 10004 2003/05/14
If we run the SQL statement (that contains an INNER JOIN) below:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
supplier_id name order_date
10000 IBM 2003/05/12
10001 Hewlett Packard 2003/05/13
The rows for Microsoft and NVIDIA from the supplier table would be omitted, since the supplier_id's
10002 and 10003 do not exist in both tables. The row for 500127 (order_id) from the orders table would
be omitted, since the supplier_id 10004 does not exist in the suppliers table.
Old SQL INNER JOIN Syntax
As a final note, it is worth mentioning that the SQL INNER JOIN example above could be rewritten using
the older implicit syntax as follows (but we still recommend using the INNER JOIN keyword syntax):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
SQL LEFT OUTER JOIN
Another type of join is called a LEFT OUTER JOIN. This type of join returns all rows from the LEFT-hand
table specified in the ON condition and only those rows from the other table where the joined fields are
equal (join condition is met).
SQL LEFT OUTER JOIN Syntax
The syntax for the SQL LEFT OUTER JOIN is:
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
In some databases, the LEFT OUTER JOIN keywords are replaced with LEFT JOIN.
Visual Illustration of SQL LEFT OUTER JOIN
In this visual diagram, the SQL LEFT OUTER JOIN returns the shaded area:

The SQL LEFT OUTER JOIN would return the all records from table1 and only those records
from table2 that intersect with table1.
SQL LEFT OUTER JOIN Examples
Here is an example of a SQL LEFT OUTER JOIN:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This LEFT OUTER JOIN example would return all rows from the suppliers table and only those rows from
the orders table where the joined fields are equal.
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the orders table
will display as <null> in the result set.
Let's look at some data to explain how LEFT OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and name). It contains the following data:
supplier_id supplier_name
10000 IBM
10001 Hewlett Packard
10002 Microsoft
10003 NVIDIA
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It contains
the following data:
order_id supplier_id order_date
500125 10000 2003/05/12
500126 10001 2003/05/13
If we run the SQL statement (that contains a LEFT OUTER JOIN) below:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
supplier_id supplier_name order_date
10000 IBM 2003/05/12
10001 Hewlett Packard 2003/05/13
10002 Microsoft <null>
10003 NVIDIA <null>
The rows for Microsoft and NVIDIA would be included because a LEFT OUTER JOIN was used.
However, you will notice that the order_date field for those records contains a <null> value.
Old SQL LEFT OUTER JOIN Syntax
As a final note, it is worth mentioning that the LEFT OUTER JOIN example above could be rewritten
using the older implicit syntax that utilizes the outer join operator (+) as follows (but we still recommend
using the LEFT OUTER JOIN keyword syntax):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id(+);
SQL RIGHT OUTER JOIN
Another type of join is called a SQL RIGHT OUTER JOIN. This type of join returns all rows from the
RIGHT-hand table specified in the ON condition and only those rows from the other table where the
joined fields are equal (join condition is met).
SQL RIGHT OUTER JOIN Syntax
The syntax for the SQL RIGHT OUTER JOIN is:
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
In some databases, the RIGHT OUTER JOIN keywords are replaced with RIGHT JOIN.
Visual Illustration of SQL RIGHT OUTER JOIN
In this visual diagram, the SQL RIGHT OUTER JOIN returns the shaded area:

The SQL RIGHT OUTER JOIN would return the all records from table2 and only those records
from table1 that intersect with table2.
SQL RIGHT OUTER JOIN Examples
Here is an example of a SQL RIGHT OUTER JOIN:
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers
RIGHT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This RIGHT OUTER JOIN example would return all rows from the orders table and only those rows from
the suppliers table where the joined fields are equal.
If a supplier_id value in the orders table does not exist in the suppliers table, all fields in the suppliers
table will display as <null> in the result set.
Let's look at some data to explain how RIGHT OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and name). It contains the following data:
supplier_id supplier_name
10000 Apple
10001 Google
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It contains
the following data:
order_id supplier_id order_date
500125 10000 2013/08/12
500126 10001 2013/08/13
500127 10002 2013/08/14
If we run the SQL statement (that contains a RIGHT OUTER JOIN) below:
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers
RIGHT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
order_id order_date supplier_name
500125 2013/08/12 Apple
500126 2013/08/13 Google
500127 2013/08/14 <null>
The row for 500127 (order_id) would be included because a RIGHT OUTER JOIN was used. However,
you will notice that the supplier_name field for that record contains a <null> value.
Old SQL RIGHT OUTER JOIN Syntax
As a final note, it is worth mentioning that the RIGHT OUTER JOIN example above could be rewritten
using the older implicit syntax that utilizes the outer join operator (+) as follows (but we still recommend
using the RIGHT OUTER JOIN keyword syntax):
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers, orders
WHERE suppliers.supplier_id(+) = orders.supplier_id;
SQL FULL OUTER JOIN
Another type of join is called a SQL FULL OUTER JOIN. This type of join returns all rows from the LEFT-
hand table and RIGHT-hand table with nulls in place where the join condition is not met.
SQL FULL OUTER JOIN Syntax
The syntax for the SQL FULL OUTER JOIN is:
SELECT columns
FROM table1
FULL [OUTER] JOIN table2
ON table1.column = table2.column;
In some databases, the FULL OUTER JOIN keywords are replaced with FULL JOIN.
Visual Illustration of SQL FULL OUTER JOIN
In this visual diagram, the SQL FULL OUTER JOIN returns the shaded area:

The SQL FULL OUTER JOIN would return the all records from both table1 and table2.
SQL FULL OUTER JOIN Examples
Here is an example of a SQL FULL OUTER JOIN:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
FULL OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This FULL OUTER JOIN example would return all rows from the suppliers table and all rows from the
orders table and whenever the join condition is not met, <nulls> would be extended to those fields in the
result set.
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the orders table
will display as <null> in the result set. If a supplier_id value in the orders table does not exist in the
suppliers table, all fields in the suppliers table will display as <null> in the result set.
Let's look at some data to explain how FULL OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and name). It contains the following data:
supplier_id supplier_name
10000 IBM
10001 Hewlett Packard
10002 Microsoft
10003 NVIDIA
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It contains
the following data:
order_id supplier_id order_date
500125 10000 2013/08/12
500126 10001 2013/08/13
500127 10004 2013/08/14
If we run the SQL statement (that contains a FULL OUTER JOIN) below:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
FULL OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
supplier_id supplier_name order_date
10000 IBM 2013/08/12
10001 Hewlett Packard 2013/08/13
10002 Microsoft <null>
10003 NVIDIA <null>
<null> <null> 2013/08/14
The rows for Microsoft and NVIDIA would be included because a FULL OUTER JOIN was used.
However, you will notice that the order_date field for those records contains a <null> value.
The row for supplier_id 10004 would be also included because a FULL OUTER JOIN was used.
However, you will notice that the supplier_id and supplier_name field for those records contain a <null>
value.
Old SQL FULL OUTER JOIN Syntax
As a final note, it is worth mentioning that the FULL OUTER JOIN example above could not have been
written in the old syntax without using a UNION query.

SQL: UNION Query
The SQL UNION query is a SQL query to combine the result sets of 2 or more SQL SELECT
statements. It removes duplicate rows between the various SELECT statements.
Each SQL SELECT statement within the UNION query must have the same number of fields in the result
sets with similar data types.
SQL UNION Query Syntax
The syntax for the SQL UNION query is:
SELECT field1, field2, . field_n
FROM tables
UNION
SELECT field1, field2, . field_n
FROM tables;
SQL UNION Query Examples - Returns single field example
The following is an example of the SQL UNION query that returns one field from multiple SELECT
statements (and both fields have the same data type):
SELECT supplier_id
FROM suppliers
UNION
SELECT supplier_id
FROM orders;
In this SQL UNION query example, if a supplier_id appeared in both the suppliers and orders table, it
would appear once in your result set. The SQL UNION query removes duplicates. If you do not wish to
remove duplicates, try using the SQL UNION ALL query.
SQL UNION Query Examples - Using SQL ORDER BY Clause example
The SQL UNION query can use the SQL ORDER BY clause to order the results of the query.
For example:
SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id > 2000
UNION
SELECT company_id, company_name
FROM companies
WHERE company_id > 1000
ORDER BY 2;
In this SQL UNION query, since the column names are different between the two SQL SELECT
statements, it is more advantageous to reference the columns in the SQL ORDER BY clause by their
position in the result set. In this example, we've sorted the results by supplier_name / company_name in
ascending order, as denoted by the "ORDER BY 2".
The supplier_name / company_name fields are in position #2 in the result set.
Frequently Asked Questions (SQL UNION Query)

Question: I need to compare two dates and return the count of a field based on the date values. For
example, I have a date field in a table called last updated date. I have to check if trunc(last_updated_date
>= trunc(sysdate-13).
Answer: Since you are using the SQL COUNT function which is an aggregate function, we'd
recommend using a SQL UNION query. For example, you could try the following:
SELECT a.code AS Code, a.name AS Name, COUNT(b.Ncode)
FROM cdmaster a, nmmaster b
WHERE a.code = b.code
AND a.status = 1
AND b.status = 1
AND b.Ncode <> 'a10'
AND TRUNC(last_updated_date) <= TRUNC(sysdate-13)
GROUP BY a.code, a.name
UNION
SELECT a.code AS Code, a.name AS Name, COUNT(b.Ncode)
FROM cdmaster a, nmmaster b
WHERE a.code = b.code
AND a.status = 1
AND b.status = 1
AND b.Ncode <> 'a10'
AND TRUNC(last_updated_date) > TRUNC(sysdate-13)
GROUP BY a.code, a.name;
The SQL UNION query allows you to perform a SQL COUNT based on one set of criteria.
TRUNC(last_updated_date) <= TRUNC(sysdate-13)
As well as perform a SQL COUNT based on another set of criteria.
TRUNC(last_updated_date) > TRUNC(sysdate-13)


SQL: UNION ALL Query
The SQL UNION ALL query is a SQL query to combine the result sets of 2 or more SELECT
statements. It returns all rows from the query (even if the row exists in more than one of the SELECT
statements).
Each SQL SELECT statement within the SQL UNION ALL query must have the same number of fields in
the result sets with similar data types.
The syntax for the SQL UNION ALL query is:
SELECT field1, field2, ... field_n
FROM tables
UNION ALL
SELECT field1, field2, ... field_n
FROM tables;
SQL UNION ALL Query - Returns single field example
The following is an example of the SQL UNION ALL query that returns one field from multiple SELECT
statements (and both fields have the same data type):
SELECT supplier_id
FROM suppliers
UNION ALL
SELECT supplier_id
FROM orders;
This SQL UNION ALL query would return a supplier_id multiple times in your result set if the supplier_id
appeared in both the suppliers and orders table. The SQL UNION ALL query does not remove
duplicates. If you wish to remove duplicates, try using the SQL UNION query.
SQL UNION ALL Query - Using SQL ORDER BY Clause example
The SQL UNION ALL query can use the SQL ORDER BY clause to order the results of the query.
For example:

SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id > 2000
UNION ALL
SELECT company_id, company_name
FROM companies
WHERE company_id > 1000
ORDER BY 2;
In this SQL UNION ALL query, since the column names are different between the two SQL SELECT
statements, it is more advantageous to reference the columns in the SQL ORDER BY clause by their
position in the result set. In this example, we've sorted the results by supplier_name / company_name in
ascending order, as denoted by the "ORDER BY 2".
The supplier_name / company_name fields are in position #2 in the result set.

Potrebbero piacerti anche