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 SalesPreviousYear AS P
JOIN SalesCurrentYear AS C
  ON P.ProductId = C.ProductId

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.DepartmentId
JOIN NutritionData ND
  ON ND.ProductId = P.Id
WHERE ND.Calories < 150