A SQL JOIN is used to combine two or more tables, based on a related column between tables.
Let's take a look selection from the "Orders" table:
OrderId | CustomerId | OrderDate |
---|---|---|
1001 | 1 | 2020-05-22 |
1002 | 2 | 2020-05-22 |
1003 | 3 | 2020-05-22 |
Then, look at a selection from the "Customers" table:
CustomerId | CustomerName | Country |
---|---|---|
1 | John | USA |
2 | Bill | USA |
3 | Jimmy | USA |
Now, the relationship between Orders table and Customers table is the "CustomerId" column.
Then, we can create the following SQL statement that contains an INNER JOIN, It'll select records that have matching values in both tables:
SELECT o.OrderId, c.CustomerName, o.OrderDate
FROM Orders as o
INNER JOIN Customers as c ON o.CustomerId=c.CustomerId
and above SQL statement will produce following result like this:
OrderId | CustomerName | OrderDate |
---|---|---|
1001 | John | 2020-05-22 |
1002 | Bill | 2020-05-22 |
1003 | Jimmy | 2020-05-22 |
Types of SQL JOINs
- INNER JOIN : Return records that have matching value 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
Comments
Post a Comment