SQL Operators
An operator is a symbol or word that works on values: + adds, > compares, AND combines, || joins text. You've already used many of them. This lesson gathers them in one place, shows the arithmetic surprises (integer division, remainders, dividing by zero), and ends with the precedence table: the order in which SQL applies operators when an expression has several.
In One Minute
- Arithmetic:
+,-,*,/and%(remainder). Two integers divide to an integer:7 / 2is3. - Comparison:
=,<>,<,>,<=,>=, giving true, false or unknown. - Logical:
AND,OR,NOT. Text:||joins text. - Dividing by zero is an error, not infinity.
- Precedence decides what happens first:
2 + 3 * 4is14. Parentheses make your meaning certain.
Arithmetic Operators
| Operator | Means | 7 ○ 2 |
|---|---|---|
+ | Add | 9 |
- | Subtract | 5 |
* | Multiply | 14 |
/ | Divide | 3 (both integers) |
% | Remainder after division | 1 |
SELECT 7 + 2 AS plus, 7 - 2 AS minus, 7 * 2 AS times, 7 / 2 AS divide, 7 % 2 AS remainder; plus | minus | times | divide | remainder
------+-------+-------+--------+-----------
9 | 5 | 14 | 3 | 1
(1 row)Integer Division
When both numbers are integers, / gives an integer and cuts off the fraction, toward zero. Write one side as a decimal to keep the fraction. With money columns this happens automatically, because numeric values divide as decimals:
SELECT 7 / 2 AS integers, 7 / 2.0 AS decimals, round(7 / 2.0, 1) AS rounded; integers | decimals | rounded
----------+--------------------+---------
3 | 3.5000000000000000 | 3.5
(1 row)SELECT payment_id, amount_usd, amount_usd / 2 AS half
FROM payments
WHERE payment_id IN (1, 6)
ORDER BY payment_id; payment_id | amount_usd | half
------------+------------+----------------------
1 | 450.00 | 225.0000000000000000
6 | 225.00 | 112.5000000000000000
(2 rows)The long run of zeros is PostgreSQL showing the extra decimal places that numeric division kept; round(amount_usd / 2, 2) shows it as money.
The Remainder
% gives what's left over after dividing. 7 % 2 is 1, because 7 is three 2s with 1 left. It's handy for tests such as "is this number even?" (n % 2 = 0). With a negative number, the remainder takes the sign of the left side: -7 % 3 is -1 in PostgreSQL. Oracle writes the remainder as MOD(7, 2) (Oracle only); PostgreSQL accepts mod() too.
Dividing by Zero
Dividing by zero is an error. The whole query fails, rather than returning infinity or NULL:
SELECT title, fee_usd / 0 AS per_nothing
FROM courses;ERROR: division by zeroWhen a divisor can be zero, guard it. A common way is NULLIF(divisor, 0), which turns a zero into NULL, so the division gives NULL instead of failing; you'll learn it in NULL Functions.
Comparison and Logical Operators
You met these in WHERE and AND, OR and NOT. As a reminder:
| Kind | Operators | Gives |
|---|---|---|
| Comparison | =, <> (or !=), <, >, <=, >= | true, false, or unknown when a NULL is involved |
| Logical | AND, OR, NOT | Combines or flips conditions |
| Special comparisons | IN, BETWEEN, LIKE, IS NULL | Covered in the next lessons |
Text Operator
|| joins text, as you learned in SELECT: first_name || ' ' || last_name. If either side is NULL, the result is NULL. PostgreSQL, Oracle and SQLite support ||; MySQL normally uses CONCAT() (there || means "or" unless PIPES_AS_CONCAT is on), and SQL Server has traditionally used + and CONCAT(). Tabs showing each database's version are coming to this course.
Other Operators You May See
| Operator | Means | Example | Note |
|---|---|---|---|
^ | Raise to a power | 2 ^ 3 is 8 | PostgreSQL only; power(2, 3) is the portable form |
& | Bitwise AND | 5 & 3 is 1 | Works on the bits of integers; rarely needed |
| | Bitwise OR | 5 | 3 is 7 | Same |
In most databases, ^ means something else (bitwise XOR in MySQL and SQL Server), so prefer power() in code that may move between databases.
Operator Precedence
When an expression has several operators, SQL applies them in a fixed order. Multiplication comes before addition, just as in school math:
SELECT 2 + 3 * 4 AS no_parentheses, (2 + 3) * 4 AS with_parentheses; no_parentheses | with_parentheses
----------------+------------------
14 | 20
(1 row)This simplified table lists the operators from this lesson, highest precedence first. Operators on the same row are applied left to right:
| Order | Operators |
|---|---|
| 1 | Parentheses ( ) |
| 2 | Unary minus -x |
| 3 | *, /, % |
| 4 | +, - |
| 5 | || (text) |
| 6 | Comparisons: =, <>, <, >, <=, >=, and IN, BETWEEN, LIKE, IS |
| 7 | NOT |
| 8 | AND |
| 9 | OR |
PostgreSQL's full table has more operators and a few finer distinctions, but you don't need to memorize any of it. When in doubt, add parentheses: they never hurt, and they make the meaning certain for every reader.
Common Mistakes
Expecting 7 / 2 to be 3.5. Write 7 / 2.0, or make sure one side is a decimal.
Forgetting precedence. fee_usd + 10 * 2 adds 20, not doubles the total. Write (fee_usd + 10) * 2.
Dividing by a value that can be zero. The query fails. Guard the divisor.
Using ^ for powers in another database. In MySQL and SQL Server it's bitwise XOR. Use power().
Practice
Challenge: Each payment was split into installments of equal size. Show each payment's payment_id, amount_usd, and two calculated columns: half (the amount divided by 2, rounded to cents) and is_even_id (true when payment_id is even, using %). Sort by payment_id.
Show solution
SELECT payment_id, amount_usd, round(amount_usd / 2, 2) AS half, payment_id % 2 = 0 AS is_even_id
FROM payments
ORDER BY payment_id; payment_id | amount_usd | half | is_even_id
------------+------------+--------+------------
1 | 450.00 | 225.00 | f
2 | 250.00 | 125.00 | t
3 | 250.00 | 125.00 | f
4 | 550.00 | 275.00 | t
5 | 200.00 | 100.00 | f
6 | 225.00 | 112.50 | t
7 | 225.00 | 112.50 | f
(7 rows)payment_id % 2 is 0 for even ids, so the comparison gives t for payments 2, 4 and 6. Precedence makes % happen before =, so no parentheses are needed, though adding them would be fine.
Key Takeaways
| You want to | Write |
|---|---|
| Keep the fraction | 7 / 2.0 |
| The remainder | 7 % 2 |
| Check for even | n % 2 = 0 |
| A power, portably | power(2, 3) |
| Force an order | (2 + 3) * 4 |
Was this tutorial helpful?