codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

22 / 67 topics
22Joins Basics23INNER JOIN24LEFT JOIN25RIGHT JOIN26FULL OUTER JOIN27Self Join
Tutorials/SQL & Databases/Joins Basics
🗄️SQL & Databases

Joins Basics

Updated 2026-04-20
4 min read

Joins Basics

Introduction

In relational databases, tables are often related to each other through common columns known as foreign keys. Understanding how to join these tables is crucial for retrieving meaningful data that spans multiple tables. This tutorial will cover the basics of SQL joins, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN, along with their use cases and best practices.

What is a Join?

A join in SQL is an operation used to combine rows from two or more tables based on a related column between them. This allows you to access data that is spread across multiple tables as if it were stored in a single table.

Types of Joins

  1. INNER JOIN: Returns only the rows where there is a match in both tables.
  2. LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table, and the matched rows from the right table. If there is no match, the result is NULL on the side of the right table.
  3. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table, and the matched rows from the left table. If there is no match, the result is NULL on the side of the left table.
  4. FULL OUTER JOIN: Returns all rows when there is a match in either left or right table. If there is no match, the result is NULL on the side without a match.

INNER JOIN

An INNER JOIN returns only the records that have matching values in both tables involved in the join.

Syntax

SELECT column1, column2, ...
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;

Example

Consider two tables: employees and departments.

employees

employee_idfirst_namelast_namedepartment_id
1JohnDoe1
2JaneSmith2

departments

department_iddepartment_name
1HR
3Finance
SELECT employees.first_name, employees.last_name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;

Result

first_namelast_namedepartment_name
JohnDoeHR

LEFT JOIN

A LEFT JOIN returns all records from the left table, and the matched records from the right table. If there is no match, the result is NULL on the side of the right table.

Syntax

SELECT column1, column2, ...
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;

Example

Using the same employees and departments tables:

SELECT employees.first_name, employees.last_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;

Result

first_namelast_namedepartment_name
JohnDoeHR
JaneSmithNULL

RIGHT JOIN

A RIGHT JOIN returns all records from the right table, and the matched records from the left table. If there is no match, the result is NULL on the side of the left table.

Syntax

SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;

Example

Using the same employees and departments tables:

SELECT employees.first_name, employees.last_name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;

Result

first_namelast_namedepartment_name
JohnDoeHR
NULLNULLFinance

FULL OUTER JOIN

A FULL OUTER JOIN returns all records when there is a match in either left or right table. If there is no match, the result is NULL on the side without a match.

Syntax

SELECT column1, column2, ...
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;

Example

Using the same employees and departments tables:

SELECT employees.first_name, employees.last_name, departments.department_name
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id;

Result

first_namelast_namedepartment_name
JohnDoeHR
JaneSmithNULL
NULLNULLFinance

Best Practices

  1. Use Aliases: Use table aliases to shorten table names and make queries more readable.
  2. Specify Columns: Always specify the columns you want to retrieve instead of using SELECT *. This improves performance and clarity.
  3. Avoid Ambiguity: Ensure that column names are unique across tables or use table aliases to avoid ambiguity.
  4. Indexing: Index the columns used in join conditions to improve query performance.
  5. Optimize Joins: Use INNER JOINs where possible, as they are generally faster than OUTER JOINs.

Conclusion

Understanding and effectively using joins is essential for working with relational databases. By mastering INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN, you can retrieve complex data sets that span multiple tables efficiently. Always consider the specific requirements of your query and choose the appropriate join type to ensure optimal performance and accurate results.

Additional Resources

  • SQL Joins Explained Visually
  • W3Schools SQL JOIN Tutorial

By following this tutorial, you should have a solid foundation in the basics of SQL joins and be able to apply them effectively in your database queries.


PreviousHAVING ClauseNext INNER JOIN

Recommended Gear

HAVING ClauseINNER JOIN