N028-H2 Tier 3 · Intermediate · hard ecommerce · Brightlane

Return the name and price-per-unit-in-stock of every qualifying product

Part of COALESCE and NULLIF in SQL

The problem

Brightlane's inventory team is flagging high-exposure products — items where the listed price per unit currently in stock exceeds $50.

Write a query to return the name and price-per-unit-in-stock of every qualifying product.

Assumptions:

  • A product's price-per-unit-in-stock is its price divided by its stock_qty.
  • Some products have a stock_qty of 0; those products do not have a defined price-per-unit-in-stock and should not appear in the report.
  • Only products whose price-per-unit-in-stock exceeds $50 should appear.

Output:

  • One row per qualifying product, with columns name and price_per_unit.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  name,
  price / NULLIF(stock_qty, 0) AS price_per_unit
FROM
  products
WHERE
  price / NULLIF(stock_qty, 0) > 50

The shape

NULLIF(stock_qty, 0) makes the division safe in both the SELECT list and the WHERE clause, and NULL's behavior in a comparison handles the rest. A sold-out product yields a NULL price / NULLIF(stock_qty, 0), and NULL > 50 is not true, so the row drops out of the filter on its own. No separate stock_qty <> 0 check is needed.

Clause by clause

  • SELECT name, price / NULLIF(stock_qty, 0) AS price_per_unit returns the name and per-unit price for each row that survives the filter. The NULLIF swaps a zero stock value for NULL before the division, so the expression yields a real number for in-stock products and NULL for sold-out ones.
  • FROM products reads the catalog. Sold-out products will be filtered out by the WHERE clause downstream.
  • WHERE price / NULLIF(stock_qty, 0) > 50 re-evaluates the same guarded division and keeps only the rows where the result exceeds 50. For a sold-out product, the division returns NULL, and NULL > 50 returns NULL, which WHERE treats as not-true. Those rows are silently dropped, which is exactly the behavior the report needs.

Why repeat the expression in WHERE and not just WHERE price / stock_qty > 50

The unguarded division in the WHERE would raise a division-by-zero error the moment PostgreSQL reaches a sold-out product, and the whole query would fail. Wrapping stock_qty in NULLIF(stock_qty, 0) in both places keeps the divisor non-zero everywhere it appears. The duplication reads as repetition but is load-bearing: each occurrence of the division needs its own guard.

The trap

The filter looks like it might also keep sold-out rows with price_per_unit showing as NULL, since the SELECT list produces NULL for them. It does not. WHERE keeps a row only when its condition evaluates to true; NULL is not true. A WHERE predicate that returns NULL drops the row exactly as if it had returned false. This is the same NULL-vs-three-valued-logic rule that governs every WHERE comparison, and knowing it means an explicit WHERE stock_qty <> 0 is unnecessary because the NULL produced by NULLIF already does that work.

You practiced combining a NULLIF-guarded division with a threshold check — undefined calculations evaluate to missing and drop out of the comparison cleanly.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.