Deadlock condition

Let's talk about SQL Joins

SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. The most common type of join is: SQL INNER JOIN (simple join). SQL INNER JOIN return all rows from multiple tables where the join condition is met.

Let's look at a selection from the "Orders" table:
OrderIDCustomerIDOrderDate
1030821996-09-18
10309371996-09-19
10310771996-09-20

Then, have a look at a selection from the "Customers" table:

CustomerIDCustomerNameContactNameCountry
1RobertRobertoItaly
2JimmyJimmy BoyMexico
3AngelinaAngela GucciMexico

Notice that the "CustomerID" column in the "Orders" table refers to the customer in the "Customers" table. The relationship between the two tables above is the "CustomerID" column. Then, if we run the following SQL statement (that contains an INNER JOIN):

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

it will produce something like this:

OrderIDCustomerNameOrderDate
10308Robert9/18/1996
10365Angelina11/27/1996
10383Around the Horn12/16/1996
10355Around the Horn11/15/1996
10278Akiko8/12/1996

SQL INNER JOIN keyword 

The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.
INNER JOIN Syntax:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;

NOTE! INNER JOIN is the same as JOIN.


SQL LEFT JOIN keyword

The LEFT JOIN keyword returns all rows from the left table, with the matching rows in the right table. The result is NULL in the right side when there is no match.
SQL LEFT JOIN Syntax:

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;

NOTE! In some databases LEFT JOIN is called LEFT OUTER JOIN.




SQL RIGHT JOIN keyword

The RIGHT JOIN keyword returns all rows from the right table, with the matching rows in the left table. The result is NULL in the left side when there is no match.
SQL RIGHT JOIN Syntax:

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;

NOTE! In some databases RIGHT JOIN is called RIGHT OUTER JOIN.



Comments