SQL Joins   «Prev  Next»

Lesson 6LEFT, RIGHT, and FULL joins
ObjectiveDescribe what LEFT, RIGHT, and FULL joins are

LEFT, RIGHT and FULL Join Operations

LEFT and RIGHT are two types of OUTER joins. Remember, an OUTER join is one that returns all of the rows for one or both of the tables, regardless of whether the data matches. A LEFT OUTER join is one in which all of the rows will be returned for the table listed on the left side of the join equation. A RIGHT OUTER join returns all of the rows for the table on the right side of the equation. LEFT and RIGHT joins enter the picture in cases where you want to list all entries from a given table and any entries in the second table if they exist.

Concepts of LEFT, RIGHT, and FULL joins in SQL

Elucidating the concepts of LEFT, RIGHT, and FULL joins in SQL to your students requires a comprehensive yet clear approach. These joins are fundamental in relational database management systems and are pivotal for effective data manipulation and analysis. Here's an authoritative explanation you can provide:
  1. LEFT JOIN (or LEFT OUTER JOIN):
    • The LEFT JOIN operation retrieves all records from the left table (table1) and the matched records from the right table (table2). If there is no match, the result set will include NULL values for the right table's columns.
    • It's particularly useful when you need to understand which rows in the left table have no corresponding rows in the right table.
    • Example Syntax:
      SELECT table1.column1, table2.column2
      FROM table1
      LEFT JOIN table2 ON table1.common_column = table2.common_column;
      
    • This syntax fetches `column1` from `table1` and `column2` from `table2`, showing all rows from `table1` regardless of whether there's a matching row in `table2`.
  2. RIGHT JOIN (or RIGHT OUTER JOIN):
    • The RIGHT JOIN does the opposite of the LEFT JOIN. It retrieves all records from the right table and the matched records from the left table. Unmatched rows in the left table will result in NULL values in the output.
    • This join is useful for identifying rows in the right table that do not have matching rows in the left table.
    • Example Syntax:
      SELECT table1.column1, table2.column2
      FROM table1
      RIGHT JOIN table2 ON table1.common_column = table2.common_column;
      
    • In this instance, every row from `table2` will be displayed along with the corresponding row from `table1`, if available.
  3. FULL JOIN (or FULL OUTER JOIN):
    • The FULL JOIN combines the effects of both LEFT and RIGHT joins. It returns all records when there is a match in either the left or right table. When there's no match, the result set will have NULL values for every column of the table that lacks a matching row.
    • This join is essential when the objective is to find all possible matches in both tables and also to identify rows in both tables that do not have corresponding matches.
    • Example Syntax:
      SELECT table1.column1, table2.column2
      FROM table1
      FULL OUTER JOIN table2 ON table1.common_column = table2.common_column;
      
    • Here, all rows from both `table1` and `table2` are displayed. Rows without a match in the counterpart table are shown with NULL values in the result set.

In teaching these concepts, it is beneficial to use real-world examples or datasets that your students can relate to. Visual aids like Venn diagrams can also be effective in demonstrating how these joins work, especially in showing what part of the data is included in each type of join. Practical exercises or lab sessions where students can write and execute SQL queries using these joins will further cement their understanding.

FULL OUTER Join

A FULL OUTER join is a join in which all of the rows are returned from both tables, regardless of whether or not a match was found. FULL (a.k.a. FULL OUTER) joins should be used if you want to return all rows in both tables, listing matches where they occur or blank column values where information doesn't match This will result in one row for each matched data item in both tables, one row for each unmatched item from the first table, and one row for each unmatched item from the second table. In the next lesson, we will look at an example.

Ad SQL Queries