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 betweenprice and cost.
select column_one, number_a - number_b as new_number
from table_name
where number_a - number_b >= 10
order by new_number descSchema · Garden ShopTable · products9 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.
- Columns: product_name, price, cost, margin.
- Rows: 6 products.
- The largest margin should be Fiddle-Leaf Fig at 14.00.
Related
See why text, numbers, dates, and booleans behave differently.
Filter text values with string literals.
Compare and sort date values chronologically.
Use true/false columns in filters.