Write a SELECT statement that returns the category_name column from the Categories table. Return one row for each category that has never been assigned to any product in the Products table. To do that, use a subquery introduced with the NOT EXISTS operator.

Answered on

Certainly! Assuming you have a database with a Categories table and a Products table, and you want to retrieve the category_name from Categories for categories that have never been assigned to any product, you can use a NOT EXISTS subquery. Here's an example using SQL:

In this example:

  • We alias the Categories table as c for brevity.
  • The subquery checks for the existence of any product in the Products table with the same category_id as the current category in the outer query (c.category_id).
  • The NOT EXISTS operator ensures that only categories with no corresponding products are included in the result set.

Adjust the table and column names based on your actual database schema.







Related Questions