That is an alternate solution here, but it might not be once the dataset grows. Let me explain.
For this particular dataset, the query results are the same, albeit in a different order. However, with different data, there would be 2 distinct differences between the results of your query and the results of the Codecademy query.
First, your query uses a
LEFT JOIN, where the CC query uses a
JOIN (the standard join is an
INNER JOIN). Here, the results were the same, but only because the
orders tables doesn’t have orders from people who are not in the
customers table. A
JOIN would leave out any record that wasn’t in both tables, while a
LEFT JOIN would keep all records in
orders, but leave out any records that were only in
customers. With the data provided for this exercise, both joins only leave out the customer with the
10 (they did not make an order), but with the result could be different with other data.
The other thing to take note of is that in your query you are grouping by
customer_name rather than
customer_id. While this works here, you can imagine that it wouldn’t work in datasets where multiple people have the same name. An
id is always unique to the user, while a name may not be.
So yes, you are correct that in this exercise the
WITH...AS syntax wasn’t necessary. However, it’s good to understand how your alternate solution might not work as the data grows.
Great observation and happy coding!