SQL INNER JOIN Keyword

« Previous Chapter Next Chapter »

SQL INNER JOIN Keyword

The INNER JOIN keyword return rows when there is at least one match in both tables.

SQL INNER JOIN Syntax

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PS: INNER JOIN is the same as JOIN.

SQL INNER JOIN Example

The "Persons" table:

P_Id LastName FirstName Address City
1 Karmen Kasa Los Angels - USA New Orleans
2 Jacob Mary Los Angels - USA New Orleans
3 Maxwell Glen Australia Sydney

The "Purchases" table:

PU_ID OrderNo P_Id
1 I-1567 3
2 I-6008 3
3 I-1110 1
4 I-5616 1
5 I-9009 15

Now we want to list all the persons with any Purchases.

We use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Purchases.OrderNo
FROM Persons
INNER JOIN Purchases
ON Persons.P_Id=Purchases.P_Id
ORDER BY Persons.LastName

The result-set will look like this:

LastName FirstName OrderNo
Karmen Kasa I-1110
Karmen Kasa I-5616
Maxwell Glen I-1567
Maxwell Glen I-6008

The INNER JOIN keyword return rows when there is at least one match in both tables. If there are rows in "Persons" that do not have matches in "Purchases", those rows will NOT be listed.


« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!