MySQL
SQL JOIN
database management
SQL tutorial
JOIN ON vs USING

MySQL JOIN ON vs USING?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When working with relational databases, combining data from two or more tables is a common task. In SQL, the JOIN clause is utilized to achieve this. MySQL, like most SQL engines, provides different ways to define the condition under which the tables will be joined: using ON or USING. Although both seem similar, they serve slightly different needs and understanding these can lead to more efficient and clearer SQL queries. In this article, we will delve into the differences between JOIN ... ON and JOIN ... USING.

Basic Concepts

JOIN Clause

The JOIN clause is used to combine rows from two or more tables based on a related column between them. Different types of JOINs include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.

JOIN ... ON

The ON clause is generally used with a JOIN when you have to specify the condition explicitly. This condition is typically a comparison between columns of the tables being joined.

Example:

sql
1SELECT employees.name, departments.name
2FROM employees
3INNER JOIN departments
4ON employees.department_id = departments.id;

JOIN ... USING

The USING clause is a more straightforward approach for joining tables. It can be used when both tables share a column with the same name and you want to join them based on this shared column.

Example:

sql
1SELECT employees.name, departments.name
2FROM employees
3INNER JOIN departments
4USING (department_id);

Technical Explanation

Both ON and USING provide a way to specify the join condition, but they differ in their syntax and utility.

JOIN ON Details

  • Flexibility: You can specify multiple conditions and even include expressions.
  • Explicitness: You define exactly which columns are compared, which is useful when columns have different names.
  • Complex Conditions: Supports complex conditions using operators like =, AND, OR.

Example with complex condition:

sql
1SELECT e.name, d.name
2FROM employees e
3INNER JOIN departments d
4ON e.department_id = d.id
5AND e.start_date < '2023-01-01';

JOIN USING Details

  • Simplicity: Less code and error-prone if the column names match.
  • Automatic Column Selection: Only columns with the same name and compatible data types should be used.
  • Column Disambiguation: The result will not carry duplicate columns with the same name; it automatically unifies selected columns.

Example with multiple shared columns:

sql
1SELECT *
2FROM orders
3INNER JOIN shipments
4USING (order_id, customer_id);

Usage Scenarios

When to Use JOIN ON

  1. Different Column Names: Use ON when joining columns with different names.
  2. Complex Conditions: When the join condition involves more than just equality checks.
  3. Multiple Conditions or Expressions: When join conditions should include multiple comparisons or expressions.

When to Use JOIN USING

  1. Same Column Names: When tables have the same column names that should be joined.
  2. Simplification: For straightforward joins where ON adds unnecessary verbosity.
  3. Avoiding Ambiguity: If automatic column selection fits your needs and helps avoid duplication.

Practical Example

Consider two tables, students and courses, each with a course_id.

sql
1-- Create tables
2CREATE TABLE students (
3    student_id INT PRIMARY KEY,
4    name VARCHAR(100),
5    course_id INT
6);
7
8CREATE TABLE courses (
9    course_id INT PRIMARY KEY,
10    course_name VARCHAR(100)
11);
12
13-- Using JOIN ... ON
14SELECT s.name, c.course_name
15FROM students s
16INNER JOIN courses c
17ON s.course_id = c.course_id;
18
19-- Using JOIN ... USING
20SELECT s.name, c.course_name
21FROM students s
22INNER JOIN courses c
23USING (course_id);

Summary Table

FeatureJOIN ... ONJOIN ... USING
Column Name MatchingNot RequiredRequired
Syntax ComplexityMore FlexibleSimpler
Join ConditionsSupports Complex Conditions and ExpressionsLimited to Equality on Same Named Columns
Column DisambiguationRequires Manual HandlingAutomatic Handling
Use CasesDifferent Column Names, Complex ConditionsSame Column Names, Simplicity

Additional Considerations

  • If your tables are designed with good naming conventions, USING can drastically simplify code.
  • Both have similar performance when used appropriately, but overusing one for scenarios suited for the other might result in suboptimal query performance.
  • It's good practice to use ON for more explicit control when designing more complex queries, especially as applications and database schemas grow.

In conclusion, both JOIN ... ON and JOIN ... USING are valuable tools in SQL arsenal. Choosing between them depends on the complexity of your queries, the naming conventions, and the degree of control you require in explicitly defining your join conditions. Understanding these distinctions will make your SQL query writing more efficient and easier to understand.


Course illustration
Course illustration

All Rights Reserved.