Lesson 2

Arithmetic in SQL

Use SQL as a calculator — add, subtract, multiply and divide directly in queries.

SQL can do math

You don't always need a table to run a query. SQL can evaluate expressions directly — useful for quick calculations and understanding how expressions work.

sql
-- Addition
SELECT 25 + 40;

-- Subtraction
SELECT 100 - 37;

-- Multiplication
SELECT 25 * 40;

-- Division
SELECT 200 / 4;

Giving results a name

Without an alias, SQL names the column after your expression. That looks messy. Use AS to label it clearly.

sql
SELECT 25 * 40 AS total_revenue;

SELECT 100 - 37 AS remaining_stock;

Multiple calculations at once

Separate each expression with a comma.

sql
SELECT
  500 * 12   AS annual_revenue,
  500 * 0.18 AS gst_amount,
  500 + 90   AS price_with_gst;
Real use: In ecommerce queries, you'll constantly calculate totals, discounts, margins and tax amounts this way — just with column names instead of fixed numbers.

Order of operations

SQL follows standard BODMAS/PEMDAS — brackets first, then multiply/divide, then add/subtract.

sql
SELECT (200 + 50) * 0.1 AS discount_amount;

SELECT 200 + 50 * 0.1 AS different_result;

These two return different results. Brackets change the order.

Practice
Exercise 1 Easy
Write a query that calculates 12 multiplied by 15, and labels the result as "total".
Hint: Use SELECT with * for multiplication and AS to label
Solution
SELECT 12 * 15 AS total
Exercise 2 Medium
A product costs 899. GST is 18%. Write a single query that returns the GST amount and the final price (cost + GST), labeled as "gst_amount" and "final_price".
Hint: GST amount = 899 * 0.18, final price = 899 + (899 * 0.18)
Solution
SELECT 899 * 0.18 AS gst_amount, 899 + (899 * 0.18) AS final_price