N001-M4 Tier 1 · Foundations · medium

Return the balance after the credit, the platform fee amount, and the final total the customer owes

Part of SELECT and Column Expressions in SQL

The problem

Brightlane's finance team is reconciling a customer order for a billing dispute. The cart total was $320, the customer applied a $50 store credit, and a 10% platform fee is calculated on the remaining balance after the credit is deducted.

Write a query to return the balance after the credit, the platform fee amount, and the final total the customer owes.

Output:

  • A single row with three columns, in this order: balance_after_credit, platform_fee, and final_total.

Run previews · Check grades

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

Worked solution Try it yourself first
Solution query
SELECT
  320 - 50 AS balance_after_credit,
  (320 - 50) * 0.10 AS platform_fee,
  (320 - 50) * 1.10 AS final_total

The shape

Parentheses force the subtraction to complete before the percentage scaling — the credit comes off the cart total first, then the fee applies to what's left.

Clause by clause

  • 320 - 50 AS balance_after_credit is the cart total minus the store credit, returning 270. Both operands are integers so the result is the integer 270; no decimal scale appears yet.
  • (320 - 50) * 0.10 AS platform_fee is the fee — 10% of the balance after the credit. The parentheses force the subtraction first, producing 270, then the multiplication by 0.10 returns 27.00. The decimal on 0.10 introduces a two-decimal-place scale that survives the operation.
  • (320 - 50) * 1.10 AS final_total is the balance after the credit scaled up by 1.10 — adding the 10% fee inline by multiplying by 1 + the rate instead of computing the balance and the fee separately and adding them. Same answer, fewer steps.

Why this and not 320 - 50 * 0.10

Without the parentheses, the query reads 320 - 50 * 0.10, which evaluates differently. SQL applies standard operator precedence: multiplication runs before subtraction. So 50 * 0.10 resolves first to 5.00, and the subtraction returns 315.00. That's the wrong calculation: a fee being subtracted from the cart total instead of the balance after a credit.

The parentheses are how the prompt's business logic — "the fee is calculated on the remaining balance after the credit is deducted" — gets enforced inside the SQL. Take them out and the query computes a different question.

The trap

Forget the parentheses on a multi-step calculation and PostgreSQL silently applies its default precedence. The query runs, returns a number that looks reasonable for an invoice line, and the finance team reconciles against the wrong figure. Any time a subtraction or addition needs to complete before a multiplication or division, the parentheses are mandatory. They control the order; without them the order defaults to PostgreSQL's precedence rules and the answer changes.

You practiced using parentheses to control the order of arithmetic — subtracting first, then applying the percentage. Parenthesized sub-expressions recur whenever a calculation has multiple stages that must run in a specific order.

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.