Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

SQL Joins

Uploaded by

Lena Devaraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Joins

Uploaded by

Lena Devaraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL Joins

❮ PreviousNext ❯

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:

CustomerID CustomerName ContactName

1 Alfreds Futterkiste Maria Anders

2 Ana Trujillo Emparedados y helados Ana Trujillo


3 Antonio Moreno Taquería Antonio Moreno

Notice that the "CustomerID" column in the "Orders" table refers to the
"CustomerID" in the "Customers" table. The relationship between the two
tables above is the "CustomerID" column.

Then, we can create the following SQL statement (that contains an INNER
JOIN), that selects records that have matching values in both tables:

Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Try it Yourself »

and it will produce something like this:

OrderID CustomerName

10308 Ana Trujillo Emparedados y helados

10365 Antonio Moreno Taquería

10383 Around the Horn

10355 Around the Horn

10278 Berglunds snabbköp


Learn to Filter Data in S
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

SELECT *
FROM Orders
LEFT JOIN Customers
ON Orders.CustomerIDCustomers.CustomerID
= ;

SQL INNER JOIN Keyword


❮ PreviousNext ❯
SQL INNER JOIN Keyword
The INNER JOIN keyword selects records that have matching values in both tables.

INNER JOIN Syntax


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

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 "Customers" table:

CustomerID CustomerName ContactName Address City

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin

2 Ana Trujillo Emparedados y Ana Trujillo Avda. de la Constitución México D.F


helados 2222

3 Antonio Moreno Antonio Mataderos 2312 México


Taquería Moreno D.F.

SQL INNER JOIN Example


The following SQL statement selects all orders with customer information:

Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Try it Yourself »

Note: The INNER JOIN keyword selects all rows from both tables as long as
there is a match between the columns. If there are records in the "Orders"
table that do not have matches in "Customers", these orders will not be
shown!

JOIN Three Tables


The following SQL statement selects all orders with customer and shipper
information:
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 »

Exercise:
Choose the correct JOIN clause to select all records from the two tables where
there is a match in both tables.

SELECT *
FROM Orders
inner join customers

ON Orders.CustomerID=Customers.CustomerID;

SQL LEFT JOIN Keyword


❮ PreviousNext ❯

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:

CustomerID CustomerName ContactName Address City

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin

2 Ana Trujillo Emparedados y Ana Trujillo Avda. de la Constitución México


helados 2222 D.F.

3 Antonio Moreno Taquería Antonio Mataderos 2312 México


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:

Example
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


❮ PreviousNext ❯

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:

Example
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;

Try it Yourself »

Note: The RIGHT JOIN keyword returns all records from the right table
(Employees), even if there are no matches in the left table (Orders).

Exercise:
Choose the correct JOIN clause to select all the records from
the Customers table plus all the matches in the Orders table.

SELECT *
FROM Orders
right join customers
ON Orders.CustomerID=Customers.CustomerID;

You might also like