The Power of SQL Joins: Learn How to Connect Your Data with Simple Examples

Automated English translation of the original Spanish article. Code examples keep their original identifiers.

If you have ever wondered how to get more out of your databases, SQL Joins are your entry ticket. These tools allow you to connect information across different tables simply and efficiently, giving you the power to build more interesting and useful queries. In this introductory guide, we will explore what Joins are, the most common types, and how to use them with examples that anyone can run. We will use standard SQL syntax, so they will work across virtually any database engine you have at hand. To keep things practical, we will work with a simple scenario: employees (e), departments (d), and their locations (du), represented by these tables:

  • Departments (d): Id (primary key), DepartmentName, Location.
  • Employees (e): Id (primary key), FirstName, LastName, Salary, DepartmentId (foreign key referencing Departments.Id)
  • DepartmentLocation (du): DepartmentId (foreign key referencing Departments.Id), Location

Tip: Before diving into the examples, set up these tables in your database and insert some sample data. To make things even easier, I prepared scripts for SQL Server, MariaDB, and PostgreSQL that create these tables with their respective engine-specific tweaks and populate them with sample data. You can find them in my Gists so you can test these queries against whatever database you have available. You will be able to get started with just copy and paste!

What Are Joins? Connect Your Data Simply and Powerfully

A Join in SQL acts like a bridge connecting rows from two or more tables based on a shared column. For example, do you want to see an employee’s name alongside their department name? A Join makes that possible. We will use our employees (e), departments (d), and locations (du) tables to demonstrate how this works.

Why do they matter? In a well-structured database, data is normalized across multiple tables to eliminate redundancy. Joins help you assemble those pieces back together whenever you need them.

When should you use them? Every time you need to combine data from different tables, such as identifying who works in which department or where each department is located.

Types of Joins: Options for Every Need

SQL provides several types of Joins, each designed for a specific purpose. Let’s explore them with examples using our sample tables. In most cases, we will use the ON clause to specify how the tables relate to each other (getting this right is essential for accurate results!).

INNER JOIN: Matching Rows Only

This Join returns only the rows that have a match in both tables, as defined by the ON clause.

SELECT e.Nombre AS NombreEmpleado, e.Apellido, d.NombreDepartamento
FROM Empleados e
INNER JOIN Departamentos d ON e.DepartamentoId = d.Id;

What does it do? It lists employees who have an assigned department, showing their first name, last name, and department name.

LEFT JOIN: Everything from the Left, Matched or Not

It returns all rows from the left table (the first one referenced). If there are matching rows in the right table (according to the ON condition), it includes them; otherwise, it populates those columns with NULL.

SELECT e.Nombre AS NombreEmpleado, e.Apellido, d.NombreDepartamento
FROM Empleados e
LEFT JOIN Departamentos d ON e.DepartamentoId = d.Id;

What does it do? It lists all employees, including those without an assigned department (in those cases, DepartmentName will be NULL).

RIGHT JOIN: Everything from the Right, Matched or Not

This is the inverse of a LEFT JOIN: it includes all rows from the right table based on the matching condition specified in the ON clause.

SELECT e.Nombre AS NombreEmpleado, e.Apellido, d.NombreDepartamento
FROM Empleados e
RIGHT JOIN Departamentos d ON e.DepartamentoId = d.Id;

What does it do? It lists all departments, displaying their employees if they have any. If a department has no assigned employees, the employee-related columns will be NULL.

FULL JOIN: Everything from Both Sides

It combines everything: all rows from both tables, returning NULL wherever a match does not exist on either side, guided by the ON condition.

SELECT e.Nombre AS NombreEmpleado, e.Apellido, d.NombreDepartamento
FROM Empleados e
FULL OUTER JOIN Departamentos d ON e.DepartamentoId = d.Id;

What does it do? It returns all employees and all departments, even if there is no relationship between them. Note: Some database engines do not support FULL JOIN natively, so check your database documentation.

CROSS JOIN: Cartesian Product of Everything

This Join pairs every single row from one table with every row from the other, without requiring an ON clause.

SELECT e.Nombre AS NombreEmpleado, d.NombreDepartamento
FROM Empleados e
CROSS JOIN Departamentos d;

What does it do? It produces every possible combination of employees and departments. Be cautious when running this on large datasets, as the result set can explode very quickly!

NATURAL JOIN: Join on Matching Column Names

It automatically matches tables based on columns that share the exact same name—such as Location in our case—without requiring an explicit ON clause.

SELECT d.NombreDepartamento, du.Ubicacion
FROM Departamentos d
NATURAL JOIN DepartamentoUbicacion du;

What does it do? It displays department names alongside their locations, matching automatically on their shared Location column.

Joins in Action: Real-World Use Cases

With these Joins, you can solve common real-world queries like:

  • Listing employees alongside their department details using INNER JOIN or LEFT JOIN.
  • Viewing departments and their locations using NATURAL JOIN.
  • Getting a comprehensive overview of both employees and departments (including unassigned records) using FULL JOIN.

Experiment with these queries against your data and observe the output!

Best Practices for Better Performance

To ensure your Joins remain fast and reliable:

  • Indexes: Ensure columns used in your ON clauses (such as DepartmentId) are indexed.
  • Filter Early: Whenever possible, use WHERE clauses to filter down data before or alongside joining.
  • Be Specific: Select only the columns you actually need (avoid SELECT *).
  • Choose the Right Join: Pick the specific join type that directly aligns with the data you need to retrieve.

Updating Data with Joins

Joins are also extremely useful when performing updates across related tables. Take a look at this example:

UPDATE Empleados
SET Salario = Salario * 1.10
FROM Empleados e
INNER JOIN Departamentos d 
ON e.DepartamentoId = d.Id
WHERE d.NombreDepartamento = 'Marketing';

What does it do? It applies a 10% salary increase to all employees who belong to the Marketing department. Direct, safe, and clean!

Wrapping Up

Joins are the fundamental key to unlocking the full power of relational databases. With this guide, you now have the foundational tools to start connecting your data like a pro. My recommendation: set up the sample tables, run the queries, and build your own variations. Consistent practice is the fastest way to master Joins!

Ready to unlock your data’s potential? Let’s keep coding.