Day 6: SQL Joins: Master Data Relationships with INNER JOIN, LEFT JOIN, and More

Ashimabha Bose
4 min readOct 19, 2023

Follow for content on Investing : LinkedIn / Instagram

Follow for content on Technical: LinkedIn

Introduction

SQL joins are used to combine data from two or more tables based on a common field between them. This allows you to create more complex queries and get more insights from your data.

There are four main types of SQL joins:

  • INNER JOIN: An INNER JOIN returns all rows from both tables where the common field matches.
  • LEFT JOIN: A LEFT JOIN returns all rows from the left table, even if there is no matching row in the right table.
  • RIGHT JOIN: A RIGHT JOIN returns all rows from the right table, even if there is no matching row in the left table.
  • FULL JOIN: A FULL JOIN returns all rows from both tables, even if there is no matching row in the other table.

Visualizing SQL Joins

The following Venn diagrams illustrate how the different types of SQL joins work:

INNER JOIN

The INNER JOIN Venn diagram shows that the INNER JOIN will return all rows from both tables where the common field matches.

LEFT JOIN

The LEFT JOIN Venn diagram shows that the LEFT JOIN will return all rows from the left table, even if there is no matching row in the right table.

RIGHT JOIN

The RIGHT JOIN Venn diagram shows that the RIGHT JOIN will return all rows from the right table, even if there is no matching row in the left table.

FULL JOIN

The FULL JOIN Venn diagram shows that the FULL JOIN will return all rows from both tables, even if there is no matching row in the other table.

Practical Examples of SQL Joins

Here are some practical examples of how to use SQL joins:

INNER JOIN

The following query uses an INNER JOIN to combine data from the customers and orders tables:

SELECT customers.name, orders.order_date, orders.total_amount
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;

This query will return all rows from the customers and orders tables where the customer_id column matches.

LEFT JOIN

The following query uses a LEFT JOIN to combine data from the customers and orders tables:

SELECT customers.name, orders.order_date, orders.total_amount
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;

This query will return all rows from the customers table, even if there is no matching row in the orders table.

RIGHT JOIN

The following query uses a RIGHT JOIN to combine data from the customers and orders tables:

SELECT customers.name, orders.order_date, orders.total_amount
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;

This query will return all rows from the orders table, even if there is no matching row in the customers table.

FULL JOIN

The following query uses a FULL JOIN to combine data from the customers and orders tables:

SELECT customers.name, orders.order_date, orders.total_amount
FROM customers
FULL JOIN orders ON customers.customer_id = orders.customer_id;

This query will return all rows from the customers and orders tables, even if there is no matching row in the other table.

Conclusion

SQL joins are a powerful tool for combining data from multiple tables in a database. By understanding the different types of SQL joins and how to use them, you can create more complex queries and get more insights from your data.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ashimabha Bose
Ashimabha Bose

Written by Ashimabha Bose

Senior Business Analyst | Power BI | Digital Marketer | Data Analyst | AI Enthusiast

No responses yet

Write a response