Inner Join:-
There are 4 different types of SQL Server joins:
- SQL Server INNER JOIN (or sometimes called simple join)
- SQL Server LEFT OUTER JOIN (or sometimes called LEFT JOIN)
- SQL Server RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
- SQL Server FULL OUTER JOIN (or sometimes called FULL JOIN)
So let's discuss SQL Server JOIN syntax, look at visual illustrations of SQL Server JOINS, and explore SQL Server JOIN examples.
INNER JOIN (simple join)
Chances are, you've already written a statement that uses an SQL Server INNER JOIN. It is the most common type of join. SQL Server INNER JOINS return all rows from multiple tables where the join condition is met.
Syntax
The syntax for the INNER JOIN in SQL Server (Transact-SQL) is:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Visual Illustration
In this visual diagram, the SQL Server INNER JOIN returns the shaded area:
The SQL Server INNER JOIN would return the records where_table1_and_table2_intersect.
Example
Here is an example of an INNER JOIN in SQL Server (Transact-SQL):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This SQL Server INNER JOIN example would return all rows from the suppliers and orders tables where there is a matching supplier_id value in both the suppliers and orders tables.
Let's look at some data to explain how the INNER JOINS work:
We have a table called_suppliers_with two fields (supplier_id and supplier_name). It contains the following data:
supplier_id | supplier_name |
---|---|
10000 | IBM |
10001 | Hewlett Packard |
10002 | Microsoft |
10003 | NVIDIA |
We have another table called_orders_with three fields (order_id, supplier_id, and order_date). It contains the following data:
order_id | supplier_id | order_date |
---|---|---|
500125 | 10000 | 2003/05/12 |
500126 | 10001 | 2003/05/13 |
500127 | 10004 | 2003/05/14 |
Our result set would look like this:
supplier_id | name | order_date |
---|---|---|
10000 | IBM | 2003/05/12 |
10001 | Hewlett Packard | 2003/05/13 |
The rows for_Microsoft_and_NVIDIA_from the supplier table would be omitted, since the supplier_id's 10002 and 10003 do not exist in both tables. The row for 500127 (order_id) from the orders table would be omitted, since the supplier_id 10004 does not exist in the suppliers table.