Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

SQL Joins:

1. Inner Join:
Inner join returns only rows that match in both the tables.
Example:

1. One categories can have many products


2. One product has only one category.

The syntax of SQL INNER JOIN is as follows:


SELECT selection_list
FROM table_A
INNER JOIN table_B ON join_condition
WHERE row_conditions.
Example based on above 2 tables
SELECT productID,productName,categoryName
FROM products
INNER JOIN categories ON products.categoryID = categories.categoryID

productID productName categoryName


--------- --------------------------------- --------------
1 Chai Beverages
2 Chang Beverages
3 Aniseed Syrup Condiments
4 Chef Anton's Cajun Seasoning Condiments
5 Chef Anton's Gumbo Mix Condiments
6 Grandma's Boysenberry Spread Condiments
7 Uncle Bob's Organic Dried Pears Produce
8 Northwoods Cranberry Sauce Condiments
9 Mishi Kobe Niku Meat/Poultry
10 Ikura Seafood
There is another form of SQL INNER JOIN which called implicit inner join. Here is the implicit
SQL INNER JOIN syntax:
SELECT selection_list
FROM table_A, table_B
WHERE join_condition.
In this form you list all joined-tables after the FROM clause and put join condition in WHERE
clause of the SQL SELECT statement. As our above example, we can rewrite the example query
as follows:
SELECT productID,productName,categoryName
FROM products, categories
WHERE products.categoryID = categories.categoryID

Visualization of the SQL INNER JOIN

2 SQL Outer Join:


SQL OUTER JOIN returns all records from both joined tables even there is no matching record
found. There are three types of SQL OUTER JOIN: FULL OUTER JOIN, LEFT OUTER JOIN
and RIGHT OUTER.
Left Outer Join
Suppose we join two tables A and B. Left outer join returns all records from the table A (left table)
plus matching records in the table B (right table). It means the result of the SQL LEFT OUTER
JOIN always contains the records in the table A (table in the left side) even no matching record
found in the table B plus matching records in the table B.
Here is the Venn diagram to visualize how SQL LEFT OUTER JOIN works.

Left Outer Join Syntax:

SELECT * FROM table_A


LEFT OUTER JOIN table_B ON join_conditions
WHERE row_conditions
Right Outer Join
SQL RIGHT OUTER JOIN returns all records from the table B (table in the right side), even no
matching record found in the table A, plus matching records in the table A.
Venn diagram to visualize how SQL RIGHT OUTER JOIN works:

Right Outer Join Syntax:


SELECT column_list
FROM table_A
RIGHT OUTER JOIN table_B ON join_conditions
WHERE row_conditions

Full Outer Join


SQL FULL OUTER JOIN combines results of both left outer join and right outer join therefore it
returns all records from both tables.

Venn diagram to visualize how SQL Full Outer Join works:

Full Outer Join Syntax:


SELECT column_list FROM table_A
FULL OUTER JOIN table_B ON join_conditions
WHERE row_conditions
3. SQL Self-join:
SQL self-join simply is a normal join which is used to join a table to itself. The SQL self-join can
be done by using table aliases to cheat one table like a different table and then join them
together. SQL self-join can be any form of join such as inner join, outer join… so you can apply
any join to the SQL self-join.

Self-Join Syntax

SELECT column_list
FROM table_A A
INNER JOIN table_A B ON A.column_name1 = B.column_name2….
WHERE row conditions

Example:
SQL self-join is very useful when you want to retrieve related data storing in one table such as
organizational structure. In our database sample, we have employees table which stores not only
employee data but also organization structure. The column reportsTo specifies the manager of an
employee and is referenced to employeeId column. To list all information of employees and
managers we can use SQL self-join as follows.

SELECT concat(e.firstname, e.lastname) employee,


concat(m.firstname,m.lastname) manager
FROM employees e
INNER JOIN employees m ON m.employeeId = e.reportsTo

Here is the output:

employee manager
-------- -------
Nancy Andrew
Janet Andrew
Margaret Andrew
Steven Andrew
Michael Steven
Robert Steven
Laura Andrew
Anne Steven

You might also like