Helix Systems' payroll administrator is preparing a monthly compensation statement for one of the sales representatives. The rep has a base salary of $5,200, earned commission at 8% on $9,500 in closed sales this month, and received a $350 performance bonus.
Write a query to return the base salary, commission amount, bonus, and total compensation in a single row.
Output:
- A single row with four columns, in this order:
base_salary,commission,bonus, andtotal_compensation.
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
5200 AS base_salary,
9500 * 0.08 AS commission,
350 AS bonus,
5200 + 9500 * 0.08 + 350 AS total_compensation The shape
Four labeled expressions, one for each piece of the compensation statement, with the total composed inline from the same numbers the breakdown columns are computing.
Clause by clause
5200 AS base_salaryis a literal — the rep's base, named as the domain quantity. Numeric literals don't need any operator; they just need a label to read as a column.9500 * 0.08 AS commissioncomputes 8% commission on $9,500 in closed sales, returning760.00. The decimal on0.08keeps the result in dollars-and-cents precision.350 AS bonusis another labeled literal, the performance bonus.5200 + 9500 * 0.08 + 350 AS total_compensationis the roll-up: base plus commission plus bonus. Standard operator precedence runs the multiplication before the additions, so the expression evaluates as5200 + 760 + 350and lands at6310.00.
Why this and not total = base_salary + commission + bonus
The first three aliases — base_salary, commission, bonus — can't be reused inside the fourth expression. SQL evaluates every expression in the SELECT list against the same input at the same time; the aliases only come into being once the whole SELECT finishes, and at that point the row is already on its way out. So the arithmetic for the total has to be written out in full from the underlying literals.
The trade-off is real: the total expression repeats the multiplication that already lives in the commission expression. Once SQL has more tools available, there are tidier ways to define a value once and reuse it. At this level, the repetition is the price of working entirely inside SELECT.
You practiced composing several derived columns in a single SELECT and using one of them as a roll-up of the others. Same-row breakdown-plus-total composition recurs anywhere a query needs to show detail and summary together.