all about joins in database management systems
and database programing using the SQL hope you get good information about joins
so this is also used by web developers as syntax to fetch information in the database
2. definitions
A JOIN is a means for combining fields from two tables by
using values common to each
• SQL joins are used to combine rows from two or more
tables.
OR
• An SQL JOIN clause is used to combine rows from two or
more tables, based on a common field between them.
3. Type of SQL JOINs
• INNER JOIN: Returns all rows when there is at least one match in
BOTH tables
• LEFT JOIN: Return all rows from the left table, and the matched
rows from the right table
• RIGHT JOIN: Return all rows from the right table, and the matched
rows from the left table
• FULL JOIN: Return all rows when there is a match in ONE of the
tables
• SELF JOIN: Returns table to itself as if the table were two tables;
temporarily renaming at least one table in the SQL statement.
• CARTESIAN JOIN: Returns the Cartesian product of the sets of
records from two or more joined tables
4. SQL INNER JOIN
• The INNER JOIN keyword selects all rows from
both tables as long as there is a match between
the columns in both tables.
• SQL INNER JOIN Syntax
• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
• Lets look at the tables on the preceding slides
6. SQL INNER JOIN
The relationship between the two tables above is the "CustomerID"
column.
• Then, if we run the following SQL statement (that contains an INNER
JOIN):
Example
SELECT
Customers.CustomerID,
Customers.CustomerName,
Customers.ContactName,
Customers.district,
Orders.OrderID,
Orders.OrderDate
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
7. SQL INNER JOIN
• Output would be
CustomerID CustomerName ContactName district OrderID OrderDate
1 Olumonde Godie wakiso 101 2019-03-18
2 Gamukama Raymond luwero 102 2019-03-19
3 Nabacwa Milica kampala 103 2019-03-20
Note: (It selects all rows from both tables as long
as there is a match between the columns in both
tables)
.
8. SQL LEFT JOIN Keyword
• The LEFT JOIN keyword returns all rows from the left
table (table1), with the matching rows in the right table
(table2). The result is NULL in the right side when there
is no match.
SQL LEFT JOIN Syntax
• SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
• In some databases LEFT OUTER JOIN is called LEFT
JOIN.
9. Using our tables above,
The following SQL statement will return all customers, and
any orders they might have:
SELECT
Customers.CustomerID,
Customers.CustomerName,
Customers.ContactName,
Customers.district,
Orders.OrderID,
Orders.OrderDate
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
10. SQL LEFT JOIN Example
Note: The LEFT JOIN keyword returns all the rows from the left table
(Customers), even if there are no matches in the right table (Orders).
CustomerID CustomerName ContactName district OrderID OrderDate
1 Olumonde Godie wakiso 101 3/18/2019
2 Gamukama Raymond luwero 102 3/19/2019
3 Nabacwa Milica kampala 103 3/20/2019
4 Kahinda Adon null null null
11. SQL RIGHT JOIN Keyword
• The RIGHT JOIN keyword returns all rows from the
right table (table2), with the matching rows in the left
table (table1). The result is NULL in the left side when
there is no match.
• SQL RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
NB:In some databases RIGHT OUTER JOIN is called
RIGHT JOIN.
12. Using our tables above,
The following SQL statement will return all employees, and
any orders they have placed:
SELECT
Customers.CustomerID,
Customers.CustomerName,
Customers.ContactName,
Customers.district,
Orders.OrderID,
Orders.OrderDate
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
13. SQL RIGHT JOIN Example
• Note: The RIGHT JOIN keyword returns all the rows from the right table
(Employees), even if there are no matches in the left table (Orders).
CustomerID CustomerName ContactName district OrderID OrderDate
1 Olumonde Godie wakiso 101 3/18/2019
2 Gamukama Raymond luwero 102 3/19/2019
3 Nabacwa Milica kampala 103 3/20/2019
null null null null 104 3/21/2019
14. SQL FULL OUTER JOIN Keyword
• The FULL OUTER JOIN keyword returns all rows
from the left table (table1) and from the right table
(table2).
• To achieve that, we use the join keyword
• The JOIN keyword combines the result of both LEFT
and RIGHT joins.
SQL FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
15. SQL FULL OUTER JOIN Example
• Using our tables above,
• The following SQL statement selects all customers, and all orders:
SELECT
Customers.CustomerID,
Customers.CustomerName,
Customers.ContactName,
Customers.district,
Orders.OrderID,
Orders.OrderDate
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
16. Note: The JOIN keyword returns all the rows from the left table (Customers), and all the rows from the
right table (Orders). If there are rows in "Customers" that do not have matches in "Orders", or if there are
rows in "Orders" that do not have matches in "Customers", those rows will be listed as well.
CustomerID CustomerName ContactName district OrderID OrderDate
1 Olumonde Godie wakiso 101 3/18/2019
2 Gamukama Raymond luwero 102 3/19/2019
3 Nabacwa Milica kampala 103 3/20/2019
4 Kahinda Adon null null null
null null null null 104 3/21/2019
17. SELF JOIN
• is used to join a table to itself as if the table were two
tables; temporarily renaming at least one table in the SQL
statement.
SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_field = b.common_field;
Here, the WHERE clause could be any given expression
based on your requirement, for example.(a.value>b.value)
18. CARTESIAN JOIN
• it returns the Cartesian product of the sets of records from
two or more joined tables. Thus, it equates to an inner join
where the join-condition always evaluates to either True
or where the join-condition is absent from the statement.
SELECT table1.column1, table2.column2...
FROM table1, table2 ;