Skip to content
/Chapter 2 · Data Types Before Deep Querying
Lesson 2.2·garden_shop
Lesson 2.2

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.

Pattern
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 ShopTable · products9 columns · 24 rows
Table · products

One row per product, with price, cost, and inventory levels.

9 columns · 24 rows
product_id intproduct_name textcategory_id intsupplier_id intprice decimalcost decimalquantity_on_hand intreorder_level intdiscontinued bool
Your task

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.

SQL Workbench
query.sqlgarden_shop · SQL engine loading
⌘↵ to run
·
Expected answer
  • Columns: product_name, price, cost, margin.
  • Rows: 6 products.
  • The largest margin should be Fiddle-Leaf Fig at 14.00.