Skip to content
MasterData
  • Home
  • All Courses
  • Blog
    • Apache Spark
Start Learning
Start Learning
MasterData
  • Home
  • All Courses
  • Blog
    • Apache Spark

Apache Airflow

2
  • Apache Airflow: What, Why, and How?
  • How to Deploy Apache Airflow on Kubernetes

Apache Iceberg

3
  • [01] – Introduction to Apache Iceberg
  • [02] – Getting Started with Apache Iceberg
  • [03] – Apache Iceberg Architecture

Apache Spark

4
  • [00] – Apache Spark By Example Course
  • [01] – What is Apache Spark?
  • [02] – Installing Apache Spark Locally
  • [03] – Deploy Apache Spark with Kubernetes (K8s)

Data Build Tool (DBT)

7
  • [00] – dbt by Example Course
  • [01] – dbt : What it is, Why and How?
  • [02] – Install dbt in local
  • [03] – Explore dbt Models
  • [04] – Sources in dbt
  • [05] – Seeds in dbt
  • [06] – Jinja Templates and Macros in dbt

SQL - Advanced

2
  • [02] – View vs Materialized View
  • [03] – Window function in SQL

SQL - Basics

1
  • 02 – Understanding SQL Operations (DML, DDL, DCL, and TCL)

SQL - Intermediate

1
  • SQL Joins: Understanding INNER, LEFT, RIGHT, and FULL Joins
  • Home
  • Docs
  • SQL
  • SQL - Intermediate
  • SQL Joins: Understanding INNER, LEFT, RIGHT, and FULL Joins
View Categories

SQL Joins: Understanding INNER, LEFT, RIGHT, and FULL Joins

kerrache.massipssa

In this tutorial, you’ll learn about the different types of SQL joins, including inner, left, right and full with SQL statements provided for each join type to demonstrate how to perform them.

What are SQL Joins? #

SQL joins allow multiple tables to be combined in a single query. This leverages the power of relational databases to efficiently retrieve results that merge data from multiple tables.

Types of Joins #

INNER JOIN #

Returns only the matching records from both tables.

Inner join

In the SQL statement below, the INNER JOIN ensures that only rows with matching id values in both tableA and tableB are included in the result.If a row in tableA does not have a corresponding row in tableB, it will be excluded (and vice versa).

SELECT a.column1, b.column2
FROM tableA a
INNER JOIN tableB b ON a.id = b.id;

LEFT JOIN (LEFT OUTER JOIN) #

Returns all records from the left table and matching records from the right table. If no match is found, NULL values are returned.

Left join

The LEFT JOIN ensures that all rows from tableA (the left table) are included in the result, even if there is no matching row in tableB. If a match is found in tableB, the corresponding values are included; otherwise, NULL values are returned for columns from tableB.

SELECT a.column1, b.column2
FROM tableA a
LEFT JOIN tableB b ON a.id = b.id;

RIGHT JOIN (RIGHT OUTER JOIN) #

Returns all records from the right table and matching records from the left table. If no match is found, NULL values are returned.

Right join

The RIGHT JOIN ensures that all rows from tableB (the right table) are included in the result, even if there is no matching row in tableA. If a match is found in tableA, the corresponding values are included; otherwise, NULL values are returned for columns from tableA.

SELECT a.column1, b.column2
FROM tableA a
RIGHT JOIN tableB b ON a.id = b.id;

FULL JOIN (FULL OUTER JOIN) #

Returns all records when there is a match in either left or right table. If no match exists, NULL values are returned from the unmatched table.

Full join

The FULL JOIN (or FULL OUTER JOIN) returns all records from both tables.

  • If there is a match between tableA and tableB on id, the corresponding values from both tables are included.
  • If no match is found, NULL values are returned for the columns of the table that lacks a matching row.
SELECT a.column1, b.column2
FROM tableA a
FULL JOIN tableB b ON a.id = b.id;

When to Use Joins #

  • Use INNER JOIN when only matching records are needed.
  • Use LEFT JOIN when all records from the left table are required.
  • Use RIGHT JOIN when all records from the right table are required.
  • Use FULL JOIN to get all records from both tables.

Conclusion #

Joins are a fundamental part of SQL that allow for powerful and flexible data retrieval. Understanding when to use each type helps optimize query performance and achieve desired results.

Updated on March 8, 2025

Leave a Reply Cancel reply

You must be logged in to post a comment.

Table of Contents
  • What are SQL Joins?
  • Types of Joins
    • INNER JOIN
    • LEFT JOIN (LEFT OUTER JOIN)
    • RIGHT JOIN (RIGHT OUTER JOIN)
    • FULL JOIN (FULL OUTER JOIN)
  • When to Use Joins
  • Conclusion

Copyright © 2025 MasterData

Powered by MasterData

Manage Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}