SQL

SQL INNER JOIN With Examples

SQL INNER JOIN

SQL INNER JOIN  :

SQL INNER JOIN is the most commonly join used in the real-time project.  By using INNER JOIN we can generate a new result by combining two tables or more than that based on our requirement. Here query compares each row of table_name_1 with each row of table_name_2 to find all pairs of rows.

Note: We can combine as many tables as per our requirement. Let’s see how it works. First, we will see the syntax of the inner join.

SQL INNER JOIN Avtartime

Syntax:

SELECT column_names
FROM table_name_1
INNER JOIN table_name_2 ON table_name_1.column_name = table_name_2.column_name;

For example, just see below and try to understand

Table -1 CUSTOMERS Table is as follows.     

+------------+--------------+-------------+
| CustomerId | CustomerName | CustomerAge |
+------------+--------------+-------------+
|          1 | John         |          25 |
|          2 | Smith        |          27 |
|          3 | Lulia        |          32 |
|          4 | Kristin      |          26 |
|          5 | Martin       |          22 |
+------------+--------------+-------------+

 Table -2  Orders Table is as follows.

+---------+-----------+------------+--------+
| OrderId | OrderName | CustomerId | Amount |
+---------+-----------+------------+--------+
|       1 | Pizza     |          2 |    150 |
|       2 | Burger    |          2 |     50 |
|       3 | Noddle    |          4 |     30 |
|       4 | Ice Cream |          5 |     20 |
+---------+-----------+------------+--------+

Let’s we want to find customer name along with the order name and amount.

Appling Inner join

SELECT customers.CustomerName,orders.OrderName,orders.Amount
FROM customers
INNER JOIN orders on orders.CustomerId=customers.CustomerId

Below is the result for above query

+--------------+-----------+--------+
| CustomerName | OrderName | Amount |
+--------------+-----------+--------+
| Smith        | Pizza     |    150 |
| Smith        | Burger    |     50 |
| Kristin      | Noddle    |     30 |
| Martin       | Ice Cream |     20 |
+--------------+-----------+--------+

For more join detail, you can check here

Sign Up for getting more new articles directly in your mail Inbox. 

Also Read  SQL FULL JOIN With Examples

About the author

Avtar

I am a blogger and I always believes in sharing a knowledge and happiness because happiness is the last journey of life.

Leave a Comment

Share1
Tweet
Pin
+1
Share
1 Shares