easy

Products with Zero Sales

You are working as a Data Analyst for an e-commerce company. The inventory team wants to identify products that have never been sold so they can decide whether to discontinue them or run promotional campaigns. Your job is to query the database and return all products that have no associated sales records.

The database contains two tables: products — which stores product information including product_id, product_name, and category; and order_items — which records individual line items for each order, containing order_item_id, product_id, quantity, and unit_price. A product is considered "never sold" if it does not appear in the order_items table at all.

  • Return the product_id, product_name, and category of every product that has never been sold.

  • A product is unsold if it has no matching rows in the order_items table.

  • Order the results by product_name in ascending alphabetical order.

Hint: This is a classic use case for a LEFT JOIN combined with a WHERE ... IS NULL filter, or alternatively a NOT EXISTS / NOT IN subquery. Be cautious with NOT IN if the subquery could return NULL values — it can silently filter out all rows. Prefer LEFT JOIN ... IS NULL or NOT EXISTS for safety and clarity.

1
2
3
4
5
Preparing...
Products with Zero Sales

You are working as a Data Analyst for an e-commerce company. The inventory team wants to identify products that have never been sold so they can decide whether to discontinue them or run promotional campaigns. Your job is to query the database and return all products that have no associated sales records. The database contains two tables: products — which stores product information including product_id , product_name , and category ; and order_items — which records individual line items for each order, containing order_item_id , product_id , quantity , and unit_price . A product is considered "never sold" if it does not appear in the order_items table at all. Return the product_id , product_name , and category of every product that has never been sold. A product is unsold if it has no matching rows in the order_items table. Order the results by product_name in ascending alphabetical order. Hint: This is a classic use case for a LEFT JOIN combined with a WHERE ... IS NULL filter, or alternatively a NOT EXISTS / NOT IN subquery. Be cautious with NOT IN if the subquery could return NULL values — it can silently filter out all rows. Prefer LEFT JOIN ... IS NULL or NOT EXISTS for safety and clarity.

Table Setup (SQL Schema)

CREATE TABLE products (
  product_id SERIAL PRIMARY KEY,
  product_name VARCHAR(100) NOT NULL,
  category VARCHAR(50) NOT NULL
);

CREATE TABLE order_items (
  order_item_id SERIAL PRIMARY KEY,
  product_id INT NOT NULL REFERENCES products(product_id),
  quantity INT NOT NULL,
  unit_price NUMERIC(10, 2) NOT NULL
);

INSERT INTO products (product_name, category) VALUES
  ('Wireless Mouse', 'Electronics'),
  ('Mechanical Keyboard', 'Electronics'),
  ('USB-C Hub', 'Electronics'),
  ('Noise Cancelling Headphones', 'Electronics'),
  ('Standing Desk Mat', 'Office'),
  ('Ergonomic Chair', 'Office'),
  ('Desk Lamp', 'Office'),
  ('Water Bottle', 'Lifestyle'),
  ('Yoga Mat', 'Lifestyle'),
  ('Resistance Bands', 'Lifestyle');

INSERT INTO order_items (product_id, quantity, unit_price) VALUES
  (1, 2, 29.99),
  (1, 1, 29.99),
  (2, 1, 89.99),
  (4, 1, 199.99),
  (4, 2, 199.99),
  (5, 3, 45.00),
  (7, 1, 34.99),
  (8, 4, 19.99),
  (10, 2, 15.49);
Home Videos Blog