Brightlane's promotions team is configuring a flash sale that applies a 10% discount to all products priced under $50.
Write a query to return the name, original price, and discounted price for each qualifying product.
Assumptions:
- The
productstable contains every item in Brightlane's catalog. - The
pricecolumn records each product's regular selling price in dollars. - The discounted price is the original price multiplied by 0.9 (a 10% reduction).
Output:
- One row per qualifying product, with columns
name,price, anddiscounted_price.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
name,
price,
price * 0.9 AS discounted_price
FROM
products
WHERE
price < 50 The shape
Two independent jobs in one query — WHERE decides which products qualify for the flash sale, and SELECT builds the discounted price for the ones that do.
Clause by clause
SELECT name, price, price * 0.9 AS discounted_pricereturns three columns per surviving row. The first two come straight from the table; the third is computed on the fly by multiplyingpriceby0.9, which is the same as taking 10% off. TheASalias labels the new column so the result reads as a domain quantity instead of?column?.FROM productsis the source — every item in Brightlane's catalog.WHERE price < 50filters down to the products under $50. Strict<excludes the boundary, so a product priced at exactly$50would not qualify. The filter runs against the rawpricecolumn, not the discounted one.
Why the filter uses price, not discounted_price
The alias discounted_price doesn't exist yet at the time WHERE runs. PostgreSQL evaluates FROM first to establish the population of rows, then WHERE to filter, and only later does SELECT produce the output columns and assign their aliases. By the time discounted_price is a name, the filtering is over. Trying to write WHERE discounted_price < 45 would raise an error: the column doesn't exist at that stage of the query. The filter has to be written against the source column the table actually has.
You practiced filtering rows AND computing a derived column in the same query. WHERE decides which rows go through, SELECT decides what each row looks like — they're independent layers.