SQL Operators

Updated · 8 min read
easyPostgreSQL 18sqloperatorsarithmeticprecedence

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 / 2 is 3.
  • 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 * 4 is 14. Parentheses make your meaning certain.

Arithmetic Operators

OperatorMeans7 ○ 2
+Add9
-Subtract5
*Multiply14
/Divide3 (both integers)
%Remainder after division1
SQL▶ Try it
SELECT 7 + 2 AS plus, 7 - 2 AS minus, 7 * 2 AS times, 7 / 2 AS divide, 7 % 2 AS remainder;
Output
 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:

SQL▶ Try it
SELECT 7 / 2 AS integers, 7 / 2.0 AS decimals, round(7 / 2.0, 1) AS rounded;
Output
 integers |      decimals      | rounded
----------+--------------------+---------
        3 | 3.5000000000000000 |     3.5
(1 row)
SQL▶ Try it
SELECT payment_id, amount_usd, amount_usd / 2 AS half
FROM payments
WHERE payment_id IN (1, 6)
ORDER BY payment_id;
Output
 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.

7 / 2 cuts off the fraction; 7 / 2.0 keeps it

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:

SQL▶ Try it
SELECT title, fee_usd / 0 AS per_nothing
FROM courses;
Output
ERROR:  division by zero

When 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:

KindOperatorsGives
Comparison=, <> (or !=), <, >, <=, >=true, false, or unknown when a NULL is involved
LogicalAND, OR, NOTCombines or flips conditions
Special comparisonsIN, BETWEEN, LIKE, IS NULLCovered 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

OperatorMeansExampleNote
^Raise to a power2 ^ 3 is 8PostgreSQL only; power(2, 3) is the portable form
&Bitwise AND5 & 3 is 1Works on the bits of integers; rarely needed
|Bitwise OR5 | 3 is 7Same

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:

SQL▶ Try it
SELECT 2 + 3 * 4 AS no_parentheses, (2 + 3) * 4 AS with_parentheses;
Output
 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:

OrderOperators
1Parentheses ( )
2Unary minus -x
3*, /, %
4+, -
5|| (text)
6Comparisons: =, <>, <, >, <=, >=, and IN, BETWEEN, LIKE, IS
7NOT
8AND
9OR
Higher rows are applied first; parentheses beat everything

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.

The Operator Families

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
SQL▶ Try it
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;
Output
 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.

Quick check: SQL operators

3 questions
1What does SELECT 7 / 2; return in PostgreSQL?
2What does SELECT 2 + 3 * 4; return?
3What happens with SELECT fee_usd / 0 FROM courses;?

Key Takeaways

You want toWrite
Keep the fraction7 / 2.0
The remainder7 % 2
Check for evenn % 2 = 0
A power, portablypower(2, 3)
Force an order(2 + 3) * 4

Was this tutorial helpful?

Lesson 17 of 115