SQL JOIN connection details and simple use instances, sqljoin
SQL JOIN connection
The SQL JOIN clause is used to combine rows from two or more tables based on the common fields between these tables.
The most common JOIN type: SQL INNER JOIN (simple JOIN ). SQL INNER JOIN returns all rows that meet the JOIN conditions from multiple tables.
Let's take a look at the data selected from the "Orders" table:
OrderID |
CustomerID |
OrderDate |
10308 |
2 |
1996-09-18 |
10309 |
37 |
1996-09-19 |
10310 |
77 |
1996-09-20 |
Then, check the data selected from the "MERs" 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 |
Note that the "mermerid" column in the "Orders" table points to the Customers in the "Customers" table. The above two tables are linked through the "mermerid" column.
Then, if we run the following SQL statement (including INNER JOIN ):
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDateFROM OrdersINNER JOIN CustomersON Orders.CustomerID=Customers.CustomerID;
The running result is as follows:
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 JOIN
Before continuing to explain the examples, we will first list the different SQL JOIN types you can use:
- INNER JOIN: If the table has at least one match, the row is returned.
- Left join:Returns all rows from the left table even if no match exists in the right table.
- Right join:Returns all rows from the right table even if no match exists in the left table.
- Full join:If one of the tables matches, the row is returned.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!