Whats The Difference Between Having Multiple Tables in FROM and Using JOIN
Whats The Difference Between Having Multiple Tables in FROM and Using JOIN
sql
learn sql
joins
What’s your approach to joining tables in SQL? In this article, we discuss two approaches
and explain why many SQL developers have a definite preference for JOIN.
Do you prefer to list multiple tables in FROM and use WHERE to set the join conditions?
Or do you use the JOIN keyword? SQL supports both, but there are significant
differences between them. Let’s look at each one in detail and then discuss why JOIN is
generally preferred.
Toys
1/6
id name brand price
Sales
1 5 3 2020-07-01 1
2 1 1 2020-07-01 1
3 3 1 2020-07-02 1
4 6 3 2020-07-03 1
5 2 3 2020-07-03 1
We can simply list both tables in the FROM clause and state in the WHERE clause that
the id from the toy table should match the toy_id from the sales table:
Everything worked as intended – we’ve joined two tables and got the results we were
expecting.
2/6
This is definitely a working solution for joining tables in SQL. However, it uses an old
syntax that was common before the SQL-92 standard introduced the JOIN keyword. Let’s
now see how we can get the same result using a modern approach to joining tables.
This syntax was introduced in the SQL-92 standard. Here, we have only one table in the
FROM clause; the table we want to join is listed in the JOIN clause. Then, we have the
ON keyword to specify the columns to be used for joining these tables.
The result of this query is the same as above. However, this approach has a number of
advantages that make it more prevalent among SQL practitioners. If you prefer to use
the old syntax for joining tables, check out the following arguments. They might change
your mind.
When you use the old syntax, your join conditions and filtering conditions are physically
grouped together in the WHERE clause. This can cause some confusion.
For example, let’s say that we want to see only the sales from July 3rd. We’ll need to add
one more condition to the WHERE clause:
Now we have two conditions in WHERE, but only one of them is a true filtering condition.
The other is just used for specifying the columns on which to join the tables.
With the JOIN keyword, the joining conditions are separated from the filtering conditions:
3/6
Here, we specify the join condition in ON and use WHERE to filter the results.
Both queries will output the same result, but the new syntax makes it clear where you are
joining the tables and where you are filtering the results.
Employees
id name
1 Rob Stevens
2 Jane White
3 Sofia Clark
To join the same three tables with the JOIN syntax, we’d use this query:
4/6
id name brand price quantity date employee
The “chain” of tables we want to join is more visible in the new syntax, where we first join
two tables on specific columns and then join the third table using another join condition.
With the old syntax, we have all three tables listed together in FROM, and then both join
conditions specified (in any order) in WHERE. Which condition corresponds to which join,
especially if we need to join more than three tables, quickly gets confusing in this syntax.
Let’s say we want to join the toys and sales tables so that we have all toys displayed
in the result, even if there were no sales for a toy during the given period. You can do this
join easily in any database by simply using LEFT JOIN:
The result will include all toys and the corresponding sales information, when applicable:
If you’re new to LEFT JOINs, read this comprehensive guide on left-joining multiple
tables.
And what about the old syntax? That’s complicated. Some databases may allow certain
tricks for left-joining tables without using the JOIN keyword, but there is no consistent
solution that will work in all cases.
5/6
For example, in Oracle you can use the (+) operator to make a left join:
At the same time, when you use the SQL-92 standard to join tables, you’ll get a syntax
error if you omit the join condition. Of course, you can cross-join your tables in the new
syntax, but only if you explicitly use the corresponding keyword (CROSS JOIN).
In addition to exercises, you may find it useful to refresh your knowledge of SQL JOINs
with our beginner-friendly guides:
Happy learning!
6/6