HTAP Summit 2024 session replays are now live!Access Session Replays

Understanding SQL Joins is fundamental for anyone interacting with relational databases. Whether you are a database administrator, a software developer, or simply curious about database operations, mastering SQL JOIN commands will dramatically improve your ability to manipulate and understand your data efficiently. This article will walk you through the major types of JOINs you need to know, using both theory and practical examples, particularly focusing on how they are implemented in TiDB, a distributed SQL database designed to support critical business applications.

Understanding SQL JOIN

A SQL JOIN clause is used to combine rows from two or more tables, based on a related column between them. It enables databases to provide a more comprehensive dataset that couldn’t otherwise be derived from querying single tables. This operation is pivotal for relational database systems, where normalization is a practice, and data is stored across multiple tables.

SQL JOIN plays a critical role in database management and application development by allowing for the retrieval of information dispersed across several tables. Without JOINs, maintaining data integrity and constructing meaningful queries from a normalized database would be cumbersome, if not impossible. They bring the relational in “relational databases,” enabling complex transactions and sophisticated data analysis.

Types of SQL JOIN

There are several types of SQL JOINs that you can use depending on your specific needs. Let’s take a closer look at the most common join types.

INNER JOIN

An INNER JOIN produces a result set that includes only the rows that match the join condition from both participating tables. If there is no matching rows, the rows are not returned. This is the default behavior for JOIN, so INNER JOIN is the same as JOIN.

SQL JOIN-INNER JOIN

Example of INNER JOIN

Considering the following example, using TiDB:

-- Assuming two tables, 'products' and 'orders'
SELECT products.name, orders.quantity
FROM products
INNER JOIN orders ON products.id = orders.product_id
WHERE orders.status = 'delivered';

This query fetches product names and order quantities for all delivered orders, exemplifying INNER JOIN by linking orders to their respective products.

Note: If you do a join without an ON clause, you will do what is sometimes called a CROSS JOIN, but not the INNER JOIN. A CROSS JOIN combines each row of the first table with every row of the second table, often resulting in a large Cartesian product. This difference is crucial for understanding the potential data volume and processing implications of each join type.

Want to try the SQL JOINs on a powerful MySQL-compatible database?

Sign Up for TiDB Serverless Now

LEFT JOIN

A LEFT JOIN (or LEFT OUTER JOIN) returns all records from the left table, and the matched records from the right table. The result is NULL on the right side if there is no match.

SQL JOIN-LEFT OUTER JOIN

Example of LEFT JOIN

-- With 'employees' as the left table and 'departments' as the right table
SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;

This query will list all employees, including those without a department, showcasing the utility of LEFT JOIN in comprehensive data retrieval across related tables.

RIGHT JOIN

A RIGHT JOIN (or RIGHT OUTER JOIN) operates like a LEFT JOIN, but returns all records from the right table, and the matched records from the left table.

SQL JOIN-RIGHT OUTER JOIN

Example of RIGHT JOIN

-- Assuming 'students' on the right, and 'courses' on the left
SELECT courses.title, students.name
FROM courses
RIGHT JOIN students ON courses.id = students.course_id;

This query ensures all students are listed, illustrating RIGHT JOIN’s role in scenarios where completeness from one side is essential.

FULL JOIN

FULL JOIN combines LEFT JOIN and RIGHT JOIN, returning rows when there is a match in one of the tables. It provides a full outer join of the two tables, leaving no record behind.

Example of FULL JOIN

-- Joining 'authors' and 'publications'
SELECT authors.name, publications.title
FROM authors
FULL JOIN publications ON authors.id = publications.author_id;

This query presents a complete bridging of authors to their publications, demonstrating FULL JOIN’s capability to merge disjoined datasets into a coherent whole.

Summary

SQL JOINs are indispensable tools in the database management toolkit. They empower developers and database administrators to weave together data stored across the schema into a tapestry of meaningful information. Among SQL databases, TiDB provides an interesting case study due to its distributed nature, enhancing the traditional capabilities of SQL JOIN with its horizontal scalability and engineering for critical business applications. By mastering JOIN operations, you can harness the full power of relational databases, turning discrete tables of data into insightful, actionable information.

Through these examples, we’ve barely scratched the surface of what’s possible with SQL JOINs. To learn even more about joins, you can check out our Course: Learn SQL with TiDB as well as our docs on Multi-table Join Queries.

Try SQL JOINs on TiDB Serverless today and experience seamless data integration!

Sign Up for Free


Last updated May 23, 2024

Spin up a Serverless database with 25GiB free resources.

Start Right Away