SQL Operators
SQL operators come in four flavours: arithmetic, comparison, logical, and bitwise. Used together, they make every WHERE, JOIN ON, and SELECT expression.
Arithmetic
| Op | Means |
|---|---|
+ - * / | Add, subtract, multiply, divide |
% | Modulo (most engines) |
|| | String concatenation (PostgreSQL, SQLite, Oracle, standard SQL) |
CONCAT() | String concatenation (MySQL, SQL Server) |
Comparison
| Op | Means |
|---|---|
= <> != | Equal, not equal |
< > <= >= | Magnitude |
BETWEEN … AND … | Range (inclusive) |
IN (…) | List membership |
LIKE / ILIKE | Pattern match |
IS NULL / IS NOT NULL | Null check |
Logical
| Op | Means |
|---|---|
AND | Both true |
OR | Either true |
NOT | Inverts |
EXISTS | True if subquery returns any rows |
Precedence (highest to lowest)
* / %+ -- Comparisons (
= <> < >…) NOTANDOR
Tip: If a condition has both
AND and OR, wrap the OR in parens. Even if precedence is on your side, parens make intent unmistakable.Example
Example
SELECT * FROM products WHERE price >= 10 AND price <= 50 AND name LIKE 'A%';Try it Yourself »
Exercise
Standard SQL string concatenation operator.
SELECT 'a'
'b';
Two vertical bars.
Discussion
Loading…