If you’ve written SQL queries before, you’re probably very familiar with JOINS. They’re the bread and butter of combining related data across multiple tables. But chances are you’ve also bumped into that awkward situation where two tables share a column name (like id or customer_id) and you end up writing long join conditions with table aliases everywhere.
That’s exactly where the USING clause can step in and make your life a little easier.
What is the USING Clause?
At its core, the USING clause is a shortcut for writing join conditions when the column you’re joining on has the same name in both tables. Instead of writing:
SELECT c.customer_id, c.name, o.order_date
FROM customers AS c
JOIN orders AS o
ON c.customer_id = o.customer_id;
You can let the database know you’re joining on the same column name using USING:
SELECT c.customer_id, c.name, o.orders_date
FROM customers As c
JOIN orders As o
USING (customer_id);
Notice how much cleaner that looks. No need to repeat yourself.
How It Works Behind the Scenes
When you use USING, SQL automatically knows the two tables share that column name. It also outputs the column only once (not c.customer_id and o.customer_id, just customer_id).
This neat little trick saves you from ambiguity in your results and keeps your query tidy.
A Practical Example
Imagine we’ve got two tables:
customers
- customer_id
- name
- city
orders
- order_id
- customer_id
- order_date
Now, if I want to see all customer names and the dates of their orders, I can say:
SELECT name, order_date
FROM customers
JOIN orders
USING (customer_id);
Here, SQL knows both tables share customer_id, takes care of the join condition for us, and makes the result set cleaner.
Multiple Columns in USING
The USING clause isn’t limited to a single column—if the tables share multiple columns with the same names, you can join on more than one:
SELECT *
FROM table_a
JOIN table_b
USING (col1, col2);
This will ensure both col1 and col2 match between the tables before creating the join.
Things to Keep in Mind
- Column names must match exactly in both tables for
USINGto work. If one table hascustomer_idbut the other hascust_id, you’ll need to go back to the ON syntax. USINGoutputs the join column only once in the result set. This is usually nice, but if you need to keep both versions for some reason,ONgives you more flexibility.- Works with different join types (
INNER JOIN,LEFT JOIN, etc.), so you’re not limited there.
When Should You Use It?
If you want cleaner queries and the column names already align, go for USING. It makes the intent of your join obvious and reduces typing.
But if you’re dealing with mismatched column names or need to include both columns in your results, stick with ON.
A good rule of thumb: reach for USING when column names match, fall back to ON when they don’t.
Wrapping It Up
Think of the USING clause as a friendly helper that cleans up your SQL queries when the stars (a.k.a. column names) align. It won’t replace ON entirely, but it’s a handy tool to keep in your querying toolkit.
