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 between price and cost.

The pattern uses WHERE from Lesson 1.5 to keep rows, and AS to give the calculated column a name.

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 Shop Table · products 9 columns · 24 rows
Table · products

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

9 columns · 24 rows
product_id int product_name text category_id int supplier_id int price decimal cost decimal quantity_on_hand int reorder_level int discontinued 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 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.