SQL Joins

## SQL Basic

SQL HOME SQL Intro SQL Syntax SQL SELECT SQL SELECT DISTINCT SQL WHERE SQL AND & OR SQL ORDER BY SQL INSERT INTO SQL UPDATE SQL DELETE SQL Injection

## SQL Advanced

SQL SELECT TOP SQL LIKE SQL Wildcards SQL IN SQL BETWEEN SQL Aliases SQL Joins SQL INNER JOIN SQL LEFT JOIN SQL RIGHT JOIN SQL FULL JOIN SQL UNION SQL SELECT INTO SQL INSERT INTO SELECT SQL CREATE DB SQL CREATE TABLE SQL Constraints SQL NOT NULL SQL UNIQUE SQL PRIMARY KEY SQL FOREIGN KEY SQL CHECK SQL DEFAULT SQL CREATE INDEX SQL DROP SQL ALTER SQL Auto Increment SQL Views SQL Dates SQL NULL Values SQL NULL Functions SQL General Data Types SQL DB Data Types

## SQL Functions

SQL Functions SQL AVG() SQL COUNT() SQL FIRST() SQL LAST() SQL MAX() SQL MIN() SQL SUM() SQL GROUP BY SQL HAVING SQL UCASE() SQL LCASE() SQL MID() SQL LEN() SQL ROUND() SQL NOW() SQL FORMAT()
SQL Quick Ref SQL Hosting SQL Summary

## SQL Quiz

SQL Quiz

# SQL Joins

« Previous
Next Chapter »

SQL joins are used to combine rows from two or more tables.

## SQL JOIN

An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.

The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is met.

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, have a look at a selection from the "Customers" table:

CustomerID | CustomerName | ContactName | Country
--- | --- | --- | ---
1 | Alfreds Futterkiste | Maria Anders | Germany
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Mexico
3 | Antonio Moreno Taquería | Antonio Moreno | Mexico

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

Then, if we run the following SQL statement (that contains an INNER JOIN):

## Example

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

it will produce something like this:

OrderID | CustomerName | OrderDate
--- | --- | ---
10308 | Ana Trujillo Emparedados y helados | 9/18/1996
10365 | Antonio Moreno Taquería | 11/27/1996
10383 | Around the Horn | 12/16/1996
10355 | Around the Horn | 11/15/1996
10278 | Berglunds snabbköp | 8/12/1996

## Different SQL JOINs

Before we continue with examples, we will list the types the different SQL JOINs you can use:

- INNER JOIN: Returns all rows when there is at least one match in BOTH tables
- LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
- RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
- FULL JOIN: Return all rows when there is a match in ONE of the tables

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE