Operators & Expressions

An expression is anything that produces a value: a column, a literal, a calculation, a function call. This lesson collects SQL's operators — arithmetic, text concatenation, comparison and logic — and the rules that decide how they combine.

Arithmetic

SQL
SELECT 7 + 2 AS plus, 7 - 2 AS minus, 7 * 2 AS times, 7 / 2 AS divide, 7 % 2 AS remainder;
Result 1 row
plusminustimesdivideremainder
951431

Look closely at 7 / 2: dividing two integers gives an integer in SQLite, PostgreSQL and SQL Server (the fraction is discarded). MySQL and Oracle return 3.5. To get a decimal result everywhere, make one side a decimal:

SQL
SELECT 7 / 2.0 AS decimal_divide, CAST(7 AS REAL) / 2 AS also_decimal;
Result 1 row
decimal_dividealso_decimal
3.53.5
Common bug: calculating a percentage as part / total * 100 with integer columns gives 0. Write 100.0 * part / total instead.

Concatenating text

SQL
SELECT first_name || ' ' || last_name AS full_name, job_title
FROM employees
WHERE department_id = 3;
Result 2 rows
full_namejob_title
Daniel KimMarketing Lead
Hannah SchmidtContent Writer

|| is the standard concatenation operator (SQLite, PostgreSQL, Oracle). MySQL uses CONCAT(a, b, …) (where || means OR by default), and SQL Server uses + or CONCAT. CONCAT is also available in PostgreSQL and recent SQLite, and it treats NULL as an empty string.

SQL
SELECT name, 'City: ' || city AS with_pipes, concat('City: ', city) AS with_concat
FROM customers
WHERE id IN (5, 6);
Result 2 rows
namewith_pipeswith_concat
Elena GarcíaCity: MadridCity: Madrid
Farah HaddadNULLCity:

Literals

KindExample
Integer42, -7
Decimal3.14, 1.5e3
Text'It''s here' — double a single quote to include it
NullNULL
BooleanTRUE, FALSE (stored as 1 and 0 in SQLite and MySQL)
Date'2026-03-01', or the standard DATE '2026-03-01' in PostgreSQL, MySQL and Oracle
SQL
SELECT 'It''s here' AS quoted, TRUE AS yes, FALSE AS no;
Result 1 row
quotedyesno
It's here10

Comparisons produce values too

SQL
SELECT name, price, price > 40 AS expensive
FROM products
WHERE category = 'Books';
Result 3 rows
namepriceexpensive
The Patient Gardener24.90
Learning SQL Step by Step32.50
Night Sky Atlas451

A comparison is an expression with a true/false result. In SQLite and MySQL you'll see 1 and 0; PostgreSQL shows true and false.

Operator precedence

PriorityOperators
HighestUnary -, +
||
*, /, %
+, -
Comparisons: =, <, LIKE, IN, BETWEEN, IS
NOT
AND
LowestOR
SQL
SELECT 2 + 3 * 4 AS no_parens, (2 + 3) * 4 AS with_parens;
Result 1 row
no_parenswith_parens
1420

Precedence varies slightly between databases, so use parentheses whenever an expression could be read two ways.

Expressions everywhere

SQL
SELECT oi.order_id,
       oi.quantity * oi.unit_price AS line_total
FROM order_items AS oi
WHERE oi.quantity * oi.unit_price > 60
ORDER BY line_total DESC;
Result 5 rows
order_idline_total
10119.98
1598
1297.5
589
789

Expressions can appear in the select list, WHERE, ORDER BY, GROUP BY, HAVING, join conditions, UPDATE … SET and INSERT values.

Try it yourself

Show each employee's monthly salary to two decimal places, and what percentage of the CEO's salary (150,000) they earn.

Try it yourself
Result