N011-H2 Tier 1 · Foundations · hard

Return the tax amount owed in a single column named `tax_amount`

Part of Arithmetic and Comparison Expressions in SQL

The problem

A tax system calculates liability on gross profit, not on total revenue. For one period, revenue was $8,000 and cost was $3,000. The tax rate on gross profit is 20%.

Write a query to return the tax amount owed in a single column named tax_amount.

Output:

  • A single row with one column, tax_amount, containing the calculated liability.

Run previews · Check grades

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

Worked solution Try it yourself first
Solution query
SELECT
  (8000 - 3000) * 0.20 AS tax_amount

The shape

The parentheses force the gross profit to be computed first — revenue minus cost — and the tax rate then applies to that difference. Without them, the tax would scale only the cost, which is a completely different calculation.

Clause by clause

  • SELECT (8000 - 3000) * 0.20 runs the parenthesised subtraction first: 8000 - 3000 returns 5000, the gross profit for the period. The multiplication then applies the 20% tax rate to that figure and returns 1000. The decimal 0.20 makes the rate a numeric, and PostgreSQL promotes the integer subtotal to match, so the calculation runs in decimal arithmetic. The exact result is 1000.00, which the audit will read as a dollar liability.
  • AS tax_amount labels the column as the figure the tax system needs. The result reads as a dollar amount, not a math expression.

Why this and not 8000 - 3000 * 0.20

Strip the parentheses and operator precedence rewrites the calculation. 3000 * 0.20 resolves first to 600.00, and the subtraction returns 7400.00. That number has no meaning in the prompt's business logic. It's neither the tax owed nor any other figure the system would report; it's revenue minus 20% of the cost, which is not a calculation anyone is asking for. But it looks plausible enough that a reconciliation might let it through.

The parentheses encode the prompt's rule — "the tax is calculated on gross profit" — into the SQL evaluation order. The shape (revenue - cost) * rate recurs everywhere: tax on gross profit, discount on subtotal after credits, fee on balance after deductions. Whenever a rate applies to a difference rather than to a single value, the difference has to be parenthesised first.

The trap

A missing pair of parentheses changes which value the rate scales without producing any error. The query runs, the result type is right (a dollar amount with the expected number of decimal places), and the figure looks like it could plausibly be the tax owed. A reviewer who doesn't recheck the algebra has no way to tell the calculation is wrong from the output alone. Any time a tax, discount, or fee applies to a difference, the difference has to be parenthesised before the rate touches it.

You practiced parenthesising a subtraction so it completes before the percentage scaling. Without parentheses, 8000 - 3000 * 0.20 would tax only the cost (silently producing a wrong but plausible-looking number) — the recurring trap whenever a tax, discount, or fee is applied to a difference rather than a single value.

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.