TExcelTAD
Home › SQL › SQL Operators

SQL Operators

Operators combine values in expressions. You have met many of them already; this lesson collects them in one place.

Arithmetic operators

Operator Meaning Example Result
+ addition 7 + 2 9
- subtraction 7 - 2 5
* multiplication 7 * 2 14
/ division 7 / 2 3 (integers!)
% remainder (modulo) 7 % 2 1
sql · try it yourself
SELECT 7 + 2 AS plus, 7 - 2 AS minus, 7 * 2 AS times,
       7 / 2 AS int_div, 7 / 2.0 AS real_div, 7 % 2 AS modulo;
plus | minus | times | int_div | real_div | modulo
-----+-------+-------+---------+----------+-------
9    | 5     | 14    | 3       | 3.5      | 1
(1 row)

Applied to columns — units left after an imaginary sale of 5 of each, and whether the stock is even:

sql · try it yourself
SELECT name, stock, stock - 5 AS after_sale, stock % 2 = 0 AS even_stock
FROM products
WHERE category_id = 5
ORDER BY name;
name               | stock | after_sale | even_stock
-------------------+-------+------------+-----------
Dark Chocolate Bar | 200   | 195        | 1
Salted Almonds     | 64    | 59         | 1
Tortilla Chips     | 85    | 80         | 0
(3 rows)

Comparison operators

Operator Meaning
= (SQLite also ==) equal
<>, != not equal
>, <, >=, <= greater / less (or equal)
IS, IS NOT equal, treating NULLs as comparable values

In SQLite a comparison is itself a value: 1 (true), 0 (false) or NULL (unknown).

sql · try it yourself
SELECT 3 > 2 AS a, 3 = 2 AS b, 'abc' < 'abd' AS c, NULL = NULL AS d, NULL IS NULL AS e;
a | b | c | d    | e
--+---+---+------+--
1 | 0 | 1 | NULL | 1
(1 row)

Logical operators

Operator True when
AND both conditions are true
OR at least one condition is true
NOT the condition is false
BETWEEN a AND b value is in the inclusive range
IN (…) value equals one in the list
LIKE / GLOB text matches a pattern
EXISTS (…) the subquery returns a row
IS NULL value is NULL

String operator

|| concatenates text (in MySQL use CONCAT(); in SQL Server +):

sql · try it yourself
SELECT first_name || ' ' || last_name || ' (' || title || ')' AS badge
FROM employees
WHERE employee_id IN (1, 10)
ORDER BY employee_id;
badge
----------------------------
Grace Holt (CEO)
Elena Popescu (Data Analyst)
(2 rows)

Bitwise operators

& (and), | (or), ~ (not), << and >> (shift) work on the binary form of integers. They are rare in everyday queries but useful for flag columns: 6 & 3 is 2, 6 | 3 is 7.

Operator precedence

From highest to lowest (operators on the same line have equal priority):

Level Operators
1 ~ (bitwise not), unary -, unary +
2 `
3 *, /, %
4 +, -
5 &, `
6 <, <=, >, >=
7 =, ==, <>, !=, IS, IN, LIKE, GLOB, BETWEEN
8 NOT
9 AND
10 OR
sql · try it yourself
SELECT 2 + 3 * 4 AS no_parens, (2 + 3) * 4 AS with_parens,
       1 OR 0 AND 0 AS and_first, (1 OR 0) AND 0 AS or_first;
no_parens | with_parens | and_first | or_first
----------+-------------+-----------+---------
14        | 20          | 1         | 0
(1 row)

Tip: When in doubt, add parentheses. They cost nothing and make your intent obvious.

Test yourself

What is 17 % 5?

  1. 3.4
  2. 3
  3. 2
  4. 12

Which is evaluated first in a OR b AND c?

  1. a OR b
  2. b AND c
  3. left to right
  4. it is an error

In SQLite, what is 7 / 2?

  1. 3.5
  2. 3
  3. 4
  4. An error