SQL
Database Management
Programming
JOIN Statements
INNER JOIN

What is the difference between JOIN and INNER JOIN?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In SQL, the terms "JOIN" and "INNER JOIN" are often used interchangeably, but it is important to understand their usage and implications in database operations, particularly in relational databases where combining rows from two or more tables based on a related column is frequently necessary.

Definition of JOIN and INNER JOIN

JOIN: In SQL, the term JOIN is generally used to refer to a clause that combines rows from two or more tables based on a related column between them. However, it's important to note that a JOIN operation without any further specification typically defaults to an INNER JOIN in most SQL databases.

INNER JOIN: An INNER JOIN specifically returns rows when there is at least one match in both tables being joined. If there are rows in one table that do not have corresponding matches in the other table, those rows will not be included in the result set.

How Does INNER JOIN Work?

The INNER JOIN keyword in SQL is used to select records that have matching values in both tables involved in the query. Here's a basic example to illustrate:

sql
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

In this example:

  • Orders and Customers are two different tables.
  • The INNER JOIN clause finds matching pairs of OrderID and CustomerID in the two tables.
  • The query returns OrderID from the Orders table and CustomerName from the Customers table where there's a match on the CustomerID.

Comparison Table between JOIN and INNER JOIN

FeatureJOININNER JOIN
Default TypeUsually defaults to INNER JOINExplicitly specifies an inner join
Result SetIncludes only matching pairs from joined tablesIncludes only matching pairs from joined tables
SyntaxTypically used without specifying the typeMust use the keyword INNER JOIN
Use CaseGeneral usage when a specific type of join is not designatedUsed when specifically needing to ensure only matching records are returned

Technical Considerations

When using JOIN or INNER JOIN, consider the following:

  • Performance: Both JOIN and INNER JOIN have similar performance characteristics because, by default, JOIN acts like an INNER JOIN unless specified otherwise.
  • Readability: Using INNER JOIN can make the intentions of your code clearer to other developers who may read your SQL queries.
  • Compatibility: The default behavior of JOIN can vary between different SQL database systems. Always check documentation to understand how your specific SQL database handles JOIN.

Examples in Different Contexts

Let’s explore these joins with another example focusing on a scenario involving a third table.

Suppose you have three tables: Orders, Customers, and Products. You want to list all orders along with customer names and the products ordered. Here's how you might approach it with an INNER JOIN:

sql
1SELECT Orders.OrderID, Customers.CustomerName, Products.ProductName
2FROM Orders
3INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
4INNER JOIN Products ON Orders.ProductID = Products.ProductID;

In this query:

  • Orders is joined with Customers to match orders with customer names.
  • Orders is also joined with Products to match each order with the product ordered.
  • Only orders that have a corresponding customer and product record will be displayed.

Conclusion

While JOIN without specification defaults to INNER JOIN in most systems, explicitly using INNER JOIN in your queries can enhance readability and reduce ambiguity in multi-developer environments. Understanding the nuances between these two can significantly impact how you design and optimize your database queries.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.