Numbers and Calculations in SQL
Number columns can be sorted, compared, and combined into new calculated values.
Numeric columns behave like numbers, not text. SQL can compare them with
>
and
<, sort them from largest to smallest, and use them in calculations.
Calculated columns are useful when the dataset stores the raw pieces but not
the answer you need. In the products table, margin is the difference between
price
and
cost.
The pattern uses
WHERE
from Lesson 1.5 to keep rows, and
AS
to give the calculated column a name.
select column_one, number_a - number_b as new_number
from table_name
where number_a - number_b >= 10
order by new_number desc Schema · Garden Shop Table · products 9 columns · 24 rows
One row per product, with price, cost, and inventory levels.
Show each product's product name, price, cost, and calculated margin. Keep products where the margin is at least 10, with the largest margin first and ties broken by product name.
Run your query to inspect the result.
Expected answer
- Columns: product_name, price, cost, margin.
- Rows: 6 products.
- The largest margin should be Fiddle-Leaf Fig at 14.
Related
Date parts, math, and truncation.
Length, case, trimming, slicing, and concatenation.
IS NULL, COALESCE, NULLIF, and null-safe logic.
See why text, numbers, dates, and booleans behave differently.
Filter text values with string literals.
Compare and sort date values chronologically.