Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Healthy Shop database
MULTIPLE JOINs – Exercises
5. Exercise 1

Instruction

Here's a little tip for you regarding table aliasing. You'll often work with many different tables. Some tables may have very long names, and some columns may have the same name across a few tables.

Preceding every ambiguous column name with a long table name may be cumbersome and time consuming. We can instead alias a table to create a shorter name. Take a look:

SELECT
  p.revenue,
  c.revenue
FROM sales_previous_year AS p
JOIN sales_current_year AS c
  ON p.product_id = c.product_id

If you alias a table, you must refer to it by its alias name throughout the whole query!

Exercise

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 it can be found (name the column department).

Stuck? Here's a hint!

Type:

SELECT
  p.name AS product,
  d.name AS department
FROM department d
JOIN product p
  ON d.id = p.department_id
JOIN nutrition_data nd
  ON nd.product_id = p.id
WHERE nd.calories < 150