iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

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

OpMeans
+ - * /Add, subtract, multiply, divide
%Modulo (most engines)
||String concatenation (PostgreSQL, SQLite, Oracle, standard SQL)
CONCAT()String concatenation (MySQL, SQL Server)

Comparison

OpMeans
=   <>   !=Equal, not equal
<   >   <=   >=Magnitude
BETWEEN … AND …Range (inclusive)
IN (…)List membership
LIKE / ILIKEPattern match
IS NULL / IS NOT NULLNull check

Logical

OpMeans
ANDBoth true
OREither true
NOTInverts
EXISTSTrue if subquery returns any rows

Precedence (highest to lowest)

  1. * / %
  2. + -
  3. Comparisons (= <> < > …)
  4. NOT
  5. AND
  6. OR
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';

Test yourself

Q1. Standard string concatenation operator is…
Q2. Highest-precedence arithmetic in SQL is…
Q3. Logical precedence from highest to lowest is…

Discussion

Loading…