SQL Join
SQL Join
SQL Join
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
Antonio
3 Antonio Moreno Taquería Mexico
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:
(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
__________ = __________ ;
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
The following SQL statement selects all orders with customer information:
EXAMPLE:
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!
EXAMPLE:
SELECT *
FROM Orders
__________
ON Orders.CustomerID=Customers.CustomerID;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
1 Alfreds Maria
Obere Str. 57 Berlin 12209 Germany
Futterkiste Anders
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
EXAMPLE:
Note: The LEFT JOIN keyword returns all records from the left table (Customers), even if
there are no matches in the right table (Orders).
Demo Database
In this tutorial we will use the well-known Northwind sample database.
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
And a selection from the "Employees" table:
Employee
LastName FirstName BirthDate Photo
ID
EXAMPLE:
Note: The RIGHT JOIN keyword returns all records from the right table (Employees), even
if there are no matches in the left table (Orders).
SELECT *
FROM Orders
__________
ON Orders.CustomerID=Customers.CustomerID;
Note: FULL OUTER JOIN can potentially return very large result-sets!
Tip: FULL OUTER JOIN and FULL JOIN are the same.
1 Alfreds Maria
Obere Str. 57 Berlin 12209 Germany
Futterkiste Anders
Ana
Trujillo Avda. de la
Ana México
2 Emparedad Constitución 05021 Mexico
Trujillo D.F.
os y 2222
helados
Antonio
Antonio Mataderos México
3 Moreno 05023 Mexico
Moreno 2312 D.F.
Taquería
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
The following SQL statement selects all customers, and all orders:
EXAMPLE:
CustomerName OrderID
Ana Trujillo
10308
Emparedados y helados
Note: The FULL OUTER JOIN keyword returns all matching records from both tables
whether the other table matches or not. So, if there are rows in "Customers" that do not
have matches in "Orders", or if there are rows in "Orders" that do not have matches in
"Customers", those rows will be listed as well.
SQL Self JOIN
A self JOIN is a regular join, but the table is joined with itself.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL Syntax
The UNION operator selects only distinct values by default. To allow duplicate values, use
UNION ALL:
Note: The column names in the result-set are usually equal to the column names in the
first SELECT statement in the UNION.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
49
Charlotte
1 Exotic Liquid Gilbert London EC1 4SD UK
Cooper
St.
P.O.
New Orleans New
2 Shelley Burke Box 70117 USA
Cajun Delights Orleans
78934
Grandma 707
Regina Ann
3 Kelly's Oxford 48104 USA
Murphy Arbor
Homestead Rd.
The following SQL statement returns the cities (only distinct values) from both the
"Customers" and the "Suppliers" table:
EXAMPLE:
The following SQL statement returns the cities (duplicate values also) from both the
"Customers" and the "Suppliers" table:
EXAMPLE:
The following SQL statement returns the German cities (only distinct values) from both
the "Customers" and the "Suppliers" table:
EXAMPLE:
Notice the "AS Type" above - it is an alias. SQL Aliases are used to give a table or a
column a temporary name. An alias only exists for the duration of the query. So, here we
have created a temporary column named "Type", that list whether the contact person is a
"Customer" or a "Supplier".
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM,
AVG) to group the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Demo Database
Below is a selection from the "Customers" table in the Northwind sample database:
Customer Contact
CustomerName Address City PostalCode Country
ID Name
1 Alfreds Maria
Obere Str. 57 Berlin 12209 Germany
Futterkiste Anders
EXAMPLE:
The following SQL statement lists the number of customers in each country, sorted high
to low:
EXAMPLE:
Demo Database
Below is a selection from the "Orders" table in the Northwind sample database:
10248 90 5 1996-07-04 3
10249 81 6 1996-07-05 1
10250 34 4 1996-07-08 2
And a selection from the "Shippers" table:
ShipperID ShipperName
1 Speedy Express
2 United Package
Federal
3
Shipping
EXAMPLE: