DbSchema Database Designer

SQL Joins Explained - INNER, LEFT, RIGHT, FULL



When working with databases, your data is often stored in more than one table. But how can you bring the data together?

This is what SQL JOINs are for. A JOIN helps you combine rows from two tables based on a related column, usually a column that appears in both.

Types of SQL JOINs

Here are the most common types of JOINs and what they do:

JOIN TypeWhat it returns
INNER JOIN Only rows that exist in both tables
LEFT JOIN All rows from the left table and matching rows from the right table
RIGHT JOIN All rows from the right table and matching rows from the left table
FULL JOINAll rows from both tables

What Is a SQL JOIN?

Let’s say you have:

  • One table with a list of employees
  • Another table with a list of departments

Each employee works in a department. If we want to know who works where, we need to connect the tables using a JOIN.

The JOIN compares values in both tables and brings the matching rows together.


Example Tables

Table: Employees

employee_idnamedepartment_id
1Sarah James10
2Mark White20
3Olivia ReedNULL

Table: Departments

department_iddepartment_name
10Engineering
20Sales
30HR

INNER JOIN

Returns only employees that are assigned to a department.

SELECT name, department_name
FROM Employees
INNER JOIN Departments
ON Employees.department_id = Departments.department_id;

Result:

namedepartment_name
Sarah JamesEngineering
Mark WhiteSales
INNER JOIN diagram

Employees without a department, and departments with no employees, are not shown.

LEFT JOIN

Returns all employees, even if they don’t belong to any department.

SELECT name, department_name
FROM Employees
LEFT JOIN Departments
ON Employees.department_id = Departments.department_id;

Result:

namedepartment_name
Sarah JamesEngineering
Mark WhiteSales
Olivia ReedNULL
LEFT JOIN diagram

You get all employees. If they have no department, the result will show NULL.


RIGHT JOIN

Returns all departments, even if no employee is assigned to them.

SELECT name, department_name
FROM Employees
RIGHT JOIN Departments
ON Employees.department_id = Departments.department_id;

Result:

namedepartment_name
Sarah JamesEngineering
Mark WhiteSales
NULLHR
RIGHT JOIN diagram

You get all departments. If no one works there, the result shows NULL for the name.


FULL JOIN

Returns all employees and all departments. If they match, they are shown together. If not, one side will be NULL.

SELECT name, department_name
FROM Employees
FULL JOIN Departments
ON Employees.department_id = Departments.department_id;

Result:

namedepartment_name
Sarah JamesEngineering
Mark WhiteSales
Olivia ReedNULL
NULLHR
FULL JOIN diagram

This gives you the full picture.


Summary

  • Use INNER JOIN when you want only matching data
  • Use LEFT JOIN when you want all rows from the first table
  • Use RIGHT JOIN when you want all rows from the second table
  • Use FULL JOIN when you want everything, matched or not

How to Do This Visually in DbSchema

You can use DbSchema to build JOINs without writing any SQL.

  1. Drag the two tables into the canvas

  2. Connect them by dragging from one column to the other, to create a foreign key

Foreign Key in DbSchema

  1. Open the Query Builder and cascade related tables

Query Builder in DbSchema

  1. Right-click the line to select the JOIN type

  2. Choose the columns you want to display

Query Builder in DbSchema

  1. Run the query and see the results

Query Builder in DbSchema

This is a great way to learn how JOINs work, because DbSchema auto-generates SQL code for you!

Query Builder in DbSchema

Note: The JOIN types shown in the menu depend on the database you are connected to. Some databases (like SQLite) do not support RIGHT JOIN or FULL JOIN. In these cases, DbSchema will only show the join types supported by your database.

Learn More

If you're learning SQL and want more examples, visit our full SQL Tutorial here: https://dbschema.com/blog/tutorials/

DbSchema Database Designer
Visual Design & Schema Diagram

➤ Create and manage your database schema visually through a user-friendly graphical interface.

➤ Easily arrange tables, columns, and foreign keys to simplify complex database structures, ensuring clarity and accessibility.

GIT & Collaboration
Version Control & Collaboration

➤ Manage schema changes through version control with built-in Git integration, ensuring every update is tracked and backed up.

➤ Collaborate efficiently with your team to maintain data integrity and streamline your workflow for accurate, consistent results.

Data Explorer & Query Builder
Relational Data & Query Builder

➤ Seamlessly navigate and visually explore your database, inspecting tables and their relationships.

➤ Build complex SQL queries using an intuitive drag-and-drop interface, providing instant results for quick, actionable insights.

Interactive Documentation & Reporting
HTML5 Documentation & Reporting

➤ Generate HTML5 documentation that provides an interactive view of your database schema.

➤ Include comments for columns, use tags for better organization, and create visually reports.