An operator combines or compares values. This page gathers SQL’s operators in one place, with links to the pages that cover them in detail. The examples use the Invoices and Crew tables from the example database.
Arithmetic
Section titled “Arithmetic”| Operator | Meaning |
|---|---|
+ |
add |
- |
subtract |
* |
multiply |
/ |
divide |
% |
remainder after division; Oracle uses MOD(a, b) instead |
Arithmetic works on columns as well as fixed numbers, and the result can be given a name with an alias. Each invoice with 20% VAT added:
SELECT InvoiceID, Amount, Amount * 1.2 AS WithVATFROM InvoicesWHERE InvoiceID IN (1001, 1002);| InvoiceID | Amount | WithVAT |
|---|---|---|
| 1001 | 24,600.00 | 29,520.00 |
| 1002 | 3,180.00 | 3,816.00 |
Comparison
Section titled “Comparison”| Operator | Meaning |
|---|---|
= |
equal to |
<> |
not equal to; most databases also accept != |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
These are covered on SQL Where. None of them is ever true when one side is NULL; use IS NULL for that.
Logical
Section titled “Logical”| Operator | True when |
|---|---|
AND |
both conditions are true (SQL And, Or, Not) |
OR |
at least one condition is true |
NOT |
the condition is false |
BETWEEN |
the value lies in a range, ends included (SQL Between) |
IN |
the value is one of a list (SQL In) |
LIKE |
the text matches a pattern (SQL Like) |
EXISTS |
a subquery returns at least one row (SQL Exists) |
ANY |
the comparison holds for at least one value from a subquery (SQL Any, All) |
ALL |
the comparison holds for every value from a subquery |
SOME |
another name for ANY |
Bitwise and compound operators
Section titled “Bitwise and compound operators”Some databases also provide operators that work on the individual bits of whole numbers, such as & (and), | (or) and ^ (exclusive or) in SQL Server and MySQL. SQL Server additionally has compound operators such as +=, so SET DayRate += 25 is short for SET DayRate = DayRate + 25. Both are specific to particular databases and rarely needed in everyday queries.