0% found this document useful (0 votes)
4 views7 pages

SQL Join

The document explains SQL JOIN clauses used to combine rows from multiple tables based on related columns, detailing various types of JOINs: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. It provides examples using the Northwind sample database, illustrating how to select data from multiple tables using these JOINs. Additionally, it emphasizes that JOIN and INNER JOIN yield the same results, and outlines the syntax for each type of JOIN.

Uploaded by

mpari0181
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

SQL Join

The document explains SQL JOIN clauses used to combine rows from multiple tables based on related columns, detailing various types of JOINs: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. It provides examples using the Northwind sample database, illustrating how to select data from multiple tables using these JOINs. Additionally, it emphasizes that JOIN and INNER JOIN yield the same results, and outlines the syntax for each type of JOIN.

Uploaded by

mpari0181
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL JOIN

A JOIN clause is used to combine rows from two or more tables, based on
a related column between them.

Let's look at a selection from the "Orders" table:

OrderID CustomerID OrderDate

10308 2 1996-09-18

10309 37 1996-09-19

10310 77 1996-09-20

Then, look at a selection from the "Customers" table:

CustomerI CustomerName ContactName


D

1 Alfreds Futterkiste Maria Anders

2 Ana Trujillo Emparedados y helados Ana Trujillo

3 Antonio Moreno Taquería Antonio Moren

Different Types of SQL JOINs


Here are the different types of the JOINs in SQL:

 (INNER) JOIN: Returns records that have matching values in both


tables
 LEFT (OUTER) JOIN: Returns all records from the left table, and
the matched records from the right table
 RIGHT (OUTER) JOIN: Returns all records from the right table, and
the matched records from the left table
 FULL (OUTER) JOIN: Returns all records when there is a match in
either left or right table
JOIN or INNER JOIN
JOIN and INNER JOIN will return the same result.

INNER is the default join type for JOIN, so when you write JOIN the parser
actually writes INNER JOIN.

Example

JOIN is the same as INNER JOIN:

SELECT Products.ProductID, Products.ProductName,


Categories.CategoryName
FROM Products
JOIN Categories ON Products.CategoryID = Categories.CategoryID;

JOIN Three Tables


The following SQL statement selects all orders with customer and shipper
information:

Here is the Shippers table:

ShipperID ShipperName Phone

1 Speedy Express (503) 555-983


2 United Package (503) 555-319

3 Federal Shipping (503) 555-993

Example

SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName


FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);

Try it Yourself »

SQL LEFT JOIN Keyword


The LEFT JOIN keyword returns all records from the left table (table1),
and the matching records from the right table (table2). The result is 0
records from the right side, if there is no match.

LEFT JOIN Syntax


SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.

Demo Database
In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:


CustomerI CustomerName ContactNa Address City
D me

1 Alfreds Futterkiste Maria Obere Str. 57 Berlin


Anders

2 Ana Trujillo Ana Trujillo Avda. de la México


Emparedados y Constitución 2222 D.F.
helados

3 Antonio Moreno Antonio Mataderos 2312 México


Taquería Moreno D.F.

And a selection from the "Orders" table:

OrderID CustomerID EmployeeID OrderDate

10308 2 7 1996-09-18

10309 37 3 1996-09-19

10310 77 8 1996-09-20

SQL LEFT JOIN Example


The following SQL statement will select all customers, and any orders they
might have:

ExampleGet your own SQL Server

SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

Try it Yourself »

Note: The LEFT JOIN keyword returns all records from the left table
(Customers), even if there are no matches in the right table (Orders).
SQL RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all records from the right table (table2),
and the matching records from the left table (table1). The result is 0
records from the left side, if there is no match.

RIGHT JOIN Syntax


SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

Demo Database
In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Orders" table:

OrderID CustomerID EmployeeID OrderDate

10308 2 7 1996-09-18

10309 37 3 1996-09-19

10310 77 8 1996-09-20

And a selection from the "Employees" table:

EmployeeID LastName FirstName BirthDate

1 Davolio Nancy 12/8/1968

2 Fuller Andrew 2/19/1952


3 Leverling Janet 8/30/1963

SQL RIGHT JOIN Example


The following SQL statement will return all employees, and any orders
they might have placed:

ExampleGet your own SQL Server

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName


FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;

SQL FULL OUTER JOIN


Keyword
The FULL OUTER JOIN keyword returns all records when there is a match
in left (table1) or right (table2) table records.

Tip: FULL OUTER JOIN and FULL JOIN are the same.

FULL OUTER JOIN Syntax


SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Note: FULL OUTER JOIN can potentially return very large result-sets!

Demo Database
In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

CustomerI CustomerName ContactNa Address City


D me

1 Alfreds Futterkiste Maria Obere Str. 57 Berlin


Anders

2 Ana Trujillo Ana Trujillo Avda. de la México


Emparedados y Constitución 2222 D.F.
helados

3 Antonio Moreno Antonio Mataderos 2312 México


Taquería Moreno D.F.

And a selection from the "Orders" table:

OrderID CustomerID EmployeeID OrderDate

10308 2 7 1996-09-18

10309 37 3 1996-09-19

10310 77 8 1996-09-20

You might also like