SQL Joins Explained

SQL JOINS are used to combine rows from two or more tables based on a related column between them. They allow you to retrieve data from multiple tables and display it in a single result set. There are different types of joins, each serving a specific purpose. Let’s break down the common types of SQL joins:

1. INNER JOIN

An INNER JOIN returns records that have matching values in both tables. If a record in one table has no corresponding record in the other table, it will not appear in the result.

Example:

Let’s say we have two tables:

  • employees (with employee_idnamedepartment_id)
  • departments (with department_iddepartment_name)
sql
SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id;
  • This query will return all employees who have a matching department (based on department_id).
  • If an employee has no department assigned (i.e., department_id doesn’t match), they will not appear in the result.

2. LEFT JOIN (or LEFT OUTER JOIN)

A LEFT JOIN returns all records from the left table (the table before the JOIN keyword) and the matched records from the right table. If there’s no match, NULL values are returned for columns from the right table.

Example:

sql
SELECT employees.name, departments.department_name FROM employees LEFT JOIN departments ON employees.department_id = departments.department_id;
  • This query returns all employees, including those without a department. For employees without a department, the department_name will be NULL.

3. RIGHT JOIN (or RIGHT OUTER JOIN)

A RIGHT JOIN is the opposite of a LEFT JOIN. It returns all records from the right table and the matching records from the left table. If no match is found, NULL values will be returned for columns from the left table.

Example:

sql
SELECT employees.name, departments.department_name FROM employees RIGHT JOIN departments ON employees.department_id = departments.department_id;
  • This query returns all departments, including those with no employees. For departments without any employees, the name column will be NULL.

4. FULL JOIN (or FULL OUTER JOIN)

A FULL JOIN returns all records when there is a match in either the left or right table. If there’s no match, it returns NULL for the non-matching side.

Example:

sql
SELECT employees.name, departments.department_name FROM employees FULL OUTER JOIN departments ON employees.department_id = departments.department_id;
  • This query returns all employees and all departments, even if some employees don’t belong to a department or some departments have no employees.

5. CROSS JOIN

A CROSS JOIN returns the Cartesian product of both tables, meaning it returns all possible combinations of rows from the two tables. This join does not require a condition. It’s rarely used in practice unless you specifically need this kind of result.

Example:

sql
SELECT employees.name, departments.department_name FROM employees CROSS JOIN departments;
  • If there are 3 employees and 5 departments, this query will return 3 × 5 = 15 rows (every employee with every department).

6. SELF JOIN

A SELF JOIN is a join where a table is joined with itself. It’s useful when you need to compare rows within the same table.

Example:

Consider an employees table where each employee has a manager_id which is a reference to another employee_id (the manager).

sql
SELECT e1.name AS Employee, e2.name AS Manager FROM employees e1 LEFT JOIN employees e2 ON e1.manager_id = e2.employee_id;
  • This query returns a list of employees with their respective managers. If an employee doesn’t have a manager, the Manager column will be NULL.

Summary of SQL JOIN Types:

Join TypeDescription
INNER JOINReturns rows with matching values in both tables.
LEFT JOINReturns all rows from the left table, and matching rows from the right table (or NULL if no match).
RIGHT JOINReturns all rows from the right table, and matching rows from the left table (or NULL if no match).
FULL JOINReturns all rows when there is a match in one of the tables.
CROSS JOINReturns the Cartesian product of both tables (all possible combinations of rows).
SELF JOINJoins a table with itself, useful for hierarchical data like employees and managers.

Performance Considerations:

  • INNER JOIN is often faster than outer joins because it doesn’t need to include unmatched rows.
  • LEFT/RIGHT JOINs can be slower because they need to handle NULL values for unmatched rows.
  • CROSS JOIN can generate large results and may severely impact performance when dealing with large datasets.

Share:

More Posts

Browser DevTools Tips

Browser DevTools are a powerful set of tools that allow developers to inspect, debug, and optimize web pages. Whether you’re a front-end developer or someone

ES6 Features

ES6 (ECMAScript 2015) introduced a number of significant updates to JavaScript, which made the language more powerful, easier to work with, and more consistent. Here’s

Responsive Design

Responsive design is a web design approach aimed at creating websites that provide optimal viewing and interaction experiences across a wide range of devices. Whether

Express Routing

Express Routing is a powerful feature of the Express.js framework, a popular web application framework for Node.js. It provides a way to define how your

SCSS Nesting

SCSS Nesting is a feature in Sass (Syntactically Awesome Style Sheets), which is a CSS preprocessor that extends CSS with features like variables, mixins, inheritance,

API Rate Limiting

API rate limiting is a technique used to control the amount of incoming requests to a server or service over a specific period of time.

CSS Animations

CSS animations allow you to animate the transitions of various properties, like colors, sizes, positions, or even more complex transformations. They can be keyframed or

CSS Grid Layout

CSS Grid Layout is a powerful layout system that allows you to create complex, flexible grid-based designs with minimal effort. It provides a way to

Semantic HTML

This post provides a brief overview of Semantic HTML in modern web development. More detailed content can be added here.

CSS Pseudo Elements

This post provides a brief overview of CSS Pseudo Elements in modern web development. More detailed content can be added here.

Progressive Web Apps

This post provides a brief overview of Progressive Web Apps in modern web development. More detailed content can be added here.

Axios vs Fetch

This post provides a brief overview of Axios vs Fetch in modern web development. More detailed content can be added here.

Babel Explained

This post provides a brief overview of Babel Explained in modern web development. More detailed content can be added here.

Async/Await in JS

This post provides a brief overview of Async/Await in JS in modern web development. More detailed content can be added here.

Fetch API Usage

This post provides a brief overview of Fetch API Usage in modern web development. More detailed content can be added here.

Firebase Authentication

This post provides a brief overview of Firebase Authentication in modern web development. More detailed content can be added here.

GraphQL Intro

This post provides a brief overview of GraphQL Intro in modern web development. More detailed content can be added here.

LocalStorage vs SessionStorage

This post provides a brief overview of LocalStorage vs SessionStorage in modern web development. More detailed content can be added here.

Nuxt.js Intro

This post provides a brief overview of Nuxt.js Intro in modern web development. More detailed content can be added here.

Tailwind CSS Basics

This post provides a brief overview of Tailwind CSS Basics in modern web development. More detailed content can be added here.

JS Unit Testing

This post provides a brief overview of JS Unit Testing in modern web development. More detailed content can be added here.

REST API Design

This post provides a brief overview of REST API Design in modern web development. More detailed content can be added here.

Web Accessibility (a11y)

This post provides a brief overview of Web Accessibility (a11y) in modern web development. More detailed content can be added here.

Microservices Overview

This post provides a brief overview of Microservices Overview in modern web development. More detailed content can be added here.

Service Workers

This post provides a brief overview of Service Workers in modern web development. More detailed content can be added here.

DOM Manipulation

This post provides a brief overview of DOM Manipulation in modern web development. More detailed content can be added here.

Intro to TypeScript

This post provides a brief overview of Intro to TypeScript in modern web development. More detailed content can be added here.

Authentication in Web Apps

This post provides a brief overview of Authentication in Web Apps in modern web development. More detailed content can be added here.

Svelte for Beginners

This post provides a brief overview of Svelte for Beginners in modern web development. More detailed content can be added here.

JavaScript Closures

This post provides a brief overview of JavaScript Closures in modern web development. More detailed content can be added here.

Vue Directives

This post provides a brief overview of Vue Directives in modern web development. More detailed content can be added here.

Node.js Modules

This post provides a brief overview of Node.js Modules in modern web development. More detailed content can be added here.

HTML Basics

This post provides a brief overview of HTML Basics in modern web development. More detailed content can be added here.

OAuth 2.0 Basics

This post provides a brief overview of OAuth 2.0 Basics in modern web development. More detailed content can be added here.

JWT Authentication

This post provides a brief overview of JWT Authentication in modern web development. More detailed content can be added here.

Flexbox Tricks

This post provides a brief overview of Flexbox Tricks in modern web development. More detailed content can be added here.

CORS Explained

This post provides a brief overview of CORS Explained in modern web development. More detailed content can be added here.

SEO for Developers

This post provides a brief overview of SEO for Developers in modern web development. More detailed content can be added here.

WebSockets Overview

This post provides a brief overview of WebSockets Overview in modern web development. More detailed content can be added here.

React Hooks

This post provides a brief overview of React Hooks in modern web development. More detailed content can be added here.

Debounce vs Throttle

This post provides a brief overview of Debounce vs Throttle in modern web development. More detailed content can be added here.

Bootstrap Grid System

This post provides a brief overview of Bootstrap Grid System in modern web development. More detailed content can be added here.

Next.js Routing

This post provides a brief overview of Next.js Routing in modern web development. More detailed content can be added here.

Webpack Basics

This post provides a brief overview of Webpack Basics in modern web development. More detailed content can be added here.

SASS Variables

This post provides a brief overview of SASS Variables in modern web development. More detailed content can be added here.

Cookies vs Tokens

This post provides a brief overview of Cookies vs Tokens in modern web development. More detailed content can be added here.

MongoDB Queries

This post provides a brief overview of MongoDB Queries in modern web development. More detailed content can be added here.

HTML5 Forms

This post provides a brief overview of HTML5 Forms in modern web development. More detailed content can be added here.

Web Security Tips

This post provides a brief overview of Web Security Tips in modern web development. More detailed content can be added here.

Ultimate Web Development Roadmap

Ultimate Web Development Roadmap

Ultimate Web Development Roadmap: Step-by-Step Guide to Building Modern, Scalable Websites Introduction Web technology changes fast. New tools and best practices appear all the time.

Web Development future in 2026

Web Development future in 2026

So yeah, I’ve been thinking a lot about where web development is heading. Not in the “buzzwordy LinkedIn post” kind of way, but more like…

Send Us A Message