0% found this document useful (0 votes)
22 views

Understanding JOINS

Uploaded by

arnela.sokolic1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Understanding JOINS

Uploaded by

arnela.sokolic1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

IT 101  Database Design and Implementation

INTRODUCTION TO JOINS

Assist. Prof. Dr. Bećir ISAKOVIĆ


AGENDA
● Introduction
● INNER JOINS
● LEFT JOIN
● RIGHT JOIN
● CROSS JOIN
● SELF JOIN
JOINS
● In a relational database, multiple interconnected tables are utilized,
establishing connections through shared columns, termed foreign key
columns. Consequently, each table contains only partial data from a
business standpoint.
● For instance, within a sample database, consider the orders and
orderdetails tables, which are linked via the orderNumber column. To
obtain comprehensive order information, data must be extracted from
both tables.
● This necessity brings joins to the forefront. A join serves as a
mechanism for connecting data across one (self-join) or multiple
tables, relying on common column values for linkage.
JOINS
● Following syntax is used in order to join tables using INNER JOIN
SELECT column_list
FROM table_1
INNER JOIN table_2 ON join_condition;
● The inner join clause evaluates each row from the first table against every row from
the second table. When values from both rows meet the join condition, a new row
is formed, incorporating all columns from both original rows. This resultant row is
then added to the output set. Essentially, the inner join clause exclusively includes
rows that have matches in both tables.
● When the join condition employs the equality operator (=), and the column names
for matching in both tables are identical, you have the option to utilize the USING
clause instead.
JOINS
SELECT column_list
FROM table_1
INNER JOIN table_2 USING (column_name);
JOINS
● In the query shown in previous slide you can see the Venn diagram
that shows that the matching rows from both tables are returned by
the inner join
● It is important to mention that the default join in the MySQL is INNER
JOIN
● This means that we can omit the INNER keyword when writing the
INNER JOIN as that is the default JOIN
LEFT JOIN
● Like an inner join, a left join necessitates a join predicate. When employing a
left join to combine two tables, the terms "left" and "right" tables become
relevant.
● In a left join, data is chosen beginning with the left table. Every row in the left
table is compared with each row in the right table.
● Should the values in both rows fulfill the join condition, the left join clause
generates a fresh row containing all columns from both table rows and adds
it to the result set.
● When the values in the two rows do not match, the left join clause still
generates a new row. This row includes the columns from the row in the left
table and NULL values for the columns of the row in the right table.
LEFT JOIN
● Put simply, the left join retrieves all data from the left table regardless
of whether matching rows exist in the right table.
● When no matching rows are found in the right table, the left join fills in
NULL values for columns from the right table in the result set.
● Below is the fundamental syntax for a left join clause joining two
tables. Left join also supports the USING clause for joining tables
SELECT column_list
FROM table_1
LEFT JOIN table_2 ON join_condition;
LEFT JOIN
● Generally, this query pattern can find rows in the left table that do not
have corresponding rows in the right table.
● This Venn diagram illustrates how to use the left join to select rows
that only exist in the left table:
RIGHT JOIN
● The right join clause mirrors the left join clause, with the roles of the
left and right tables reversed. Instead of beginning with the left table,
the right join initiates data selection from the right table.
● With the right join clause, all rows from the right table are chosen, and
matches are sought in the left table.
● When a row from the right table lacks corresponding matches in the
left table, the columns of the left table will contain NULL values in the
resultant set.
RIGHT JOIN
● Below is the fundamental syntax for a right join clause joining two
tables. Right join also supports the USING clause for joining tables
SELECT column_list
FROM table_1
RIGHT JOIN table_2 ON join_condition;

● This Venn diagram illustrates the right join:


CROSS JOIN
● In contrast to the inner join, left join, and right join, the cross join
clause lacks a specific join condition.
● Cross join produces a Cartesian product of rows from the joined
tables. It combines each row from the first table with every row from
the second table to form the result set.
● If the first table has n rows and the second table has m rows, a
cross-join operation will yield nxm rows.
CROSS JOIN
● Below is the syntax for the cross-join clause:
SELECT select_list
FROM table_1
CROSS JOIN table_2;

● The cross join is useful for generating planning data but in same time,
due to the number of rows that will be returned, you should be careful
when using it. Following diagrams shows the product of cross join
SELF JOIN
● A self join in MySQL is when a table is joined with itself. This means
that the table is treated as if it were two separate tables, allowing you
to compare rows within the same table. Self joins are typically used
when you need to compare rows within a single table, such as when
you want to find related records or hierarchical data.
● For example, suppose you have a table called "employees" with
columns like "employee_id" and "manager_id", where the
"manager_id" refers to the "employee_id" of another employee who is
the manager. You could use a self join to retrieve information about
both the employee and their manager in a single query.
SELF JOIN
● Following is the query that would extract the information about the
employee and his manager
SELECT e.employee_id,
e.employee_name,
m.employee_id AS manager_id,
m.employee_name AS manager_name
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id;
● In this query, the "employees" table is aliased as both "e" and "m",
allowing us to compare each employee's "manager_id" with another
employee's "employee_id" within the same table.
SELF JOIN
● Following diagram depicts the idea behind the self join where the table
has foreign key reference to itself and is being joined to itself
JOIN CONDITIONS
● In MySQL, join conditions are typically applied in the ON clause or the USING
clause of a join statement as we have show previously
● However, although it is not recommended, we can add the join conditions in
the WHERE clause as shown in the example below
SELECT *
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name
WHERE table1.some_column = table2.some_column;
● Placing join conditions in the ON clause clearly indicates that these
conditions are part of the join operation, making the query more
understandable. It also helps to separate join conditions from filtering
conditions, which are typically specified in the WHERE clause.
JOIN CONDITIONS CONT.
● Also it is important to note that we can add multiple condition on the
ON clause if needed. In most cases we will only add the
primary-foreign key constraint but in some occasions we also have to
specify additional conditions in our ON clause as shown in the
example below
SELECT *
FROM customers c
JOIN orders o ON
c.customerNumber = o.customerNumber AND
o.orderDate > 20030101 AND o.orderDate < 20030110;
Thank you

You might also like