N004-H1 Tier 1 · Foundations · hard

Return the exact per-department budget

Part of Literal Values, Data Types, and Type Casting in SQL

The problem

Helix Systems' finance director is finalizing the annual training budget of $50,000, which must be allocated equally across 7 departments. Each department's share must be calculated to full decimal precision — the amount cannot be rounded or truncated.

Write a query to return the exact per-department budget.

Output:

  • A single row with one column, per_department_budget, expressed as a decimal value.

Run previews · Check grades

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

Worked solution Try it yourself first
Solution query
SELECT
  50000::NUMERIC / 7 AS per_department_budget

The shape

Casting one side of the division to numeric is what preserves the fractional part. Without the cast, dividing 50000 by 7 would drop everything past the decimal point and the finance director would lose precision the budget calculation depends on.

Clause by clause

  • 50000::numeric casts the integer literal 50000 to numeric. The training budget itself is a whole-dollar amount, but writing it as 50000 alone keeps it in integer arithmetic. The cast on the left of the / is what flips the whole expression's result type.
  • / 7 divides the cast value by the integer 7. Once one operand is numeric, PostgreSQL promotes the other to numeric to match, and the division runs at full decimal precision. The result is 7142.857142857143 — the repeating decimal carried out to PostgreSQL's default scale for an unsized numeric division.
  • AS per_department_budget labels the output column. The finance director reads the result as a per-department allocation rather than an anonymous quotient.

Why this and not 50000 / 7

Without the cast, both operands are integers and PostgreSQL stays in integer arithmetic. 50000 / 7 returns 7142 — the .857142857143 is gone, and the seven departmental shares add up to 49994, not 50000. Six dollars vanish into the truncation. That's a rounding error in a budget reconciliation, and the prompt explicitly forbids it.

The cast can sit on either operand: 50000 / 7::numeric returns the same 7142.857142857143, as does 50000::numeric / 7::numeric and 50000.0 / 7. PostgreSQL only needs one side to be numeric to promote the whole expression. Pick whichever spelling reads cleanest in the surrounding query.

The trap

Integer division drops the decimal silently. There is no error, no warning, no flag in the result. 50000 / 7 runs, returns a number that looks like a per-department share, and the finance director reconciles against 49994 total instead of 50000. The missing six dollars surface later — in a variance report, an audit, or never. Any time the fractional part of a quotient is load-bearing, at least one operand must be a decimal type. The cast is the executable rule; forgetting it is the trap.

You practiced casting one operand to numeric to force decimal division. Whenever a quotient must keep its fractional part, the cast on either side flips the result type.

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.