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
| plus | minus | times | divide | remainder |
| 9 | 5 | 14 | 3 | 1 |
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_divide | also_decimal |
| 3.5 | 3.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_name | job_title |
| Daniel Kim | Marketing Lead |
| Hannah Schmidt | Content 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
| name | with_pipes | with_concat |
| Elena García | City: Madrid | City: Madrid |
| Farah Haddad | NULL | City: |
Literals
| Kind | Example |
| Integer | 42, -7 |
| Decimal | 3.14, 1.5e3 |
| Text | 'It''s here' — double a single quote to include it |
| Null | NULL |
| Boolean | TRUE, 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;
Comparisons produce values too
SQL
SELECT name, price, price > 40 AS expensive
FROM products
WHERE category = 'Books';
Result 3 rows
| name | price | expensive |
| The Patient Gardener | 24.9 | 0 |
| Learning SQL Step by Step | 32.5 | 0 |
| Night Sky Atlas | 45 | 1 |
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
| Priority | Operators |
| Highest | Unary -, + |
| || |
| *, /, % |
| +, - |
| Comparisons: =, <, LIKE, IN, BETWEEN, IS |
| NOT |
| AND |
| Lowest | OR |
SQL
SELECT 2 + 3 * 4 AS no_parens, (2 + 3) * 4 AS with_parens;
Result 1 row
| no_parens | with_parens |
| 14 | 20 |
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_id | line_total |
| 10 | 119.98 |
| 15 | 98 |
| 12 | 97.5 |
| 5 | 89 |
| 7 | 89 |
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.