Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
VAT is 20% of the order total. Accounting wants the orders where it comes to more than 20.00, with the VAT shown alongside.
Return one row per qualifying order, largest VAT first, ordered by order_id when two are equal.
Result columns · in this order
order_id | The order. |
order_total | Its total. |
vat | 20% of the total, to 2 decimal places. |
How to approach it
Repeat the expression where the alias is not available yet, and use the alias where it is.
Sample input
| order_id | customer_id | placed_at | status | order_total |
|---|---|---|---|---|
| 1001 | c1 | 2026-01-05 10:00:00 | paid | 120 |
| 1002 | c2 | 2026-01-12 14:30:00 | Paid | 80 |
| 1003 | c1 | 2026-01-20 09:15:00 | pending | 45 |
| 1004 | c3 | 2026-01-31 23:30:00 | PAID | 200 |
| 1005 | c2 | 2026-02-01 00:15:00 | paid | 60 |
| 1006 | c4 | 2026-02-03 11:00:00 | paid | 150 |
| 1007 | c3 | 2026-02-10 16:45:00 | refunded | 90 |
| 1008 | c1 | 2026-02-14 12:00:00 | paid | 300 |
| 1009 | c5 | 2026-02-18 08:30:00 | Paid | 75 |
| 1010 | c4 | 2026-02-25 19:20:00 | pending | 85 |
| 1011 | c5 | 2026-02-28 21:00:00 | paid | 130 |
| 1012 | c2 | 2026-03-02 10:10:00 | paid | 95 |
12 rows — scroll inside the table to see them all.
Expected output
| order_id | order_total | vat |
|---|---|---|
| 1008 | 300 | 60 |
| 1004 | 200 | 40 |
| 1006 | 150 | 30 |
| 1011 | 130 | 26 |
| 1001 | 120 | 24 |
5 rows — all rows shown.
Constraints
vat is 20% of order_total, rounded to 2 decimal places.vat descending, then by order_id so the result is reproducible.Worked example
Order 1008 is worth 300, so its VAT is 60 and it belongs in the result. Writing WHERE vat > 20 to find it fails with no such column: vat.
SQL evaluates FROM, then WHERE, then GROUP BY, then HAVING, then SELECT, and finally ORDER BY. The alias is created in SELECT, which has not run when WHERE is evaluated — so the name does not exist yet. ORDER BY runs last, which is why the same alias works perfectly well there.
What this tests
Clause evaluation order, and the practical consequence that a SELECT alias is visible to ORDER BY but not to WHERE.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.