N009-H2 Tier 1 · Foundations · hard

Return both prices in a single row

Part of Column Aliases and Expression Naming in SQL

The problem

A seasonal pricing team is building a two-tier discount model from a base price of $400:

  • The member price is 80% of the base price.
  • The clearance price is 85% of the member price — calculated from the discounted member price, not from the original base.

Write a query to return both prices in a single row.

Output:

  • A single row with two columns: member_price and clearance_price.

Run previews · Check grades

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

Worked solution Try it yourself first
Solution query
SELECT
  400 * 0.80 AS member_price,
  400 * 0.80 * 0.85 AS clearance_price

The shape

The clearance price is the member price scaled by another 15% off, but the calculation has to be written from the base price both times — 400 * 0.80 for the member price, and 400 * 0.80 * 0.85 for the clearance price.

Clause by clause

  • 400 * 0.80 AS member_price is the first expression: 80% of the base price, returning 320. The decimal on 0.80 introduces a scale to the multiplication, but the result still lands on the whole number 320 because the underlying math is clean.
  • 400 * 0.80 * 0.85 AS clearance_price is the second expression: the base price scaled down by 20%, then by another 15%. SQL evaluates the chained multiplication left to right — 400 * 0.80 resolves to 320, then 320 * 0.85 resolves to 272. The two discounts compound rather than adding.

Why this and not member_price * 0.85 AS clearance_price

The natural shape would be to reference the member_price alias directly — that's how the business logic reads, and that's how the prompt describes the relationship. SQL won't allow it inside the same SELECT list. Every expression in SELECT evaluates against the same input at the same time, and the aliases only exist as labels on the way out. At the moment clearance_price is being computed, member_price is not yet a usable name. So the calculation has to be written from the underlying base price: 400 * 0.80 * 0.85.

This is the same constraint as on simpler multi-column queries, but it bites harder here because the second column genuinely depends on the first column's value. The fix at this level is to repeat the arithmetic.

The trap

The trap is the 80% + 15% combination. It is tempting to compute the clearance price as the base scaled by the sum of the two discounts — 400 * 0.65, since 20% + 15% = 35% — but that returns 260, not 272. The two discounts don't add; they compound, because the second one applies to the already-discounted price. The correct math is multiplicative: 0.80 * 0.85 = 0.68. Any time discounts stack on each other rather than on the original base, the calculation is a product of the scaling factors, not a sum of the percentages.

You practiced repeating an expression because aliases defined in SELECT are not visible to other items in the same SELECT list. The recurring fix at this scale is to repeat the arithmetic inline; once subqueries and CTEs enter the picture, those become the cleaner ways to compute a value once and reuse it.

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.