postgresql 如何确定哪个表应位于联接的左侧?

jk9hmnmh  于 2022-12-12  发布在  PostgreSQL
关注(0)|答案(1)|浏览(118)

I am practicing SQL joins on learnsql.com and when joining three tables, I'm having trouble determining which table should be to the left of the join clause and which should be to the right.
Show the name of each product and its calorific value for all products that in the ‘dairy’ department.
Since the question begins by asking for the name of each product, I made the assumption that my answer should be ... FROM product p .... and then I would left join the nutrition_data and department data on it.
List all products that have fewer than 150 calories. For each product show its name (rename the column to product) and the department in which can be found (name the column department).
I interpreted the second question the same way, that it should be a list of all the products ( ... FROM product ... ) and then the department and nutrition_data would be joined to that.
Below is a picture of the tables and the answer to the first question, the wrong answer to the second question and the right answer to the second question.
https://imgur.com/a/qLqpjGo
Because the question asked for a list of all the products, I thought the product table should be the first table after the FROM clause and then all the other tables would be joined to it. This logic worked for the first question, and since the second question is similar in nature, I assumed it was the same logic. However for the second, the solution is ... FROM department ... and then the product and nutrition_data is joined to the department table.
How do I determine which table should be to the left of my join?

jjhzyzn0

jjhzyzn01#

"all products" is what calls an outer join: all products even when they don't have calorific information
"all products that have fewer than 150 calories" cannot show the products without calorific information. Then I see no outer join here. Except if no calorific information means no calories

相关问题