Skip to the workspace
Pebellslearn by building
Basic SQL Syntax  ›  Lesson

Operators

Operators 15 minOperators compare values, compute new ones and combine conditions, with AND binding tighter than OR and NULL testable only through IS NULL.

sql / Operators

Explore each scene at your own pace.

01 · The rule

The rule

Operators

Operators compare values, compute new ones and combine conditions, with AND binding tighter than OR and NULL testable only through IS NULL.

Compose predicates and arithmetic while accounting for precedence and NULL.

A support agent wants "pending orders, plus any shipped order with more than five items". She types a condition, runs it, and gets every shipped order in the table. Nothing is broken; she just combined AND and OR without knowing which one wins. Operators are small, but their rules decide which rows you get.

The rule

Compare, then combine with AND/OR — and test NULL only with IS.

Comparison operators produce true or false for each row: =, <> (not equal; most databases also accept !=), <, >, <=, >=.

Arithmetic operators compute values: +, -, *, /, and % for remainder in many databases. You can use them in the output as well as in conditions — SELECT name, price * 1.18 AS price_with_tax FROM products; shows each price with 18% tax added. One surprise: in PostgreSQL, SQL Server and SQLite, dividing one integer by another throws away the fraction, so 7 / 2 gives 3. Write 7.0 / 2 when you want 3.5.

Logical operators combine conditions: AND (both true), OR (at least one true), NOT (flip it).

Special operators save typing:

SELECT * FROM products WHERE price BETWEEN 100 AND 500;      -- inclusive at both ends
SELECT * FROM customers WHERE city IN ('Pune', 'Delhi');     -- any value in the list
SELECT * FROM customers WHERE name LIKE 'A%';                -- % = any run of characters
SELECT * FROM customers WHERE name LIKE '_a%';               -- _ = exactly one character
SELECT * FROM customers WHERE email IS NULL;                 -- the only way to test NULL

NULL means "unknown", so email = NULL is itself unknown for every row, and WHERE keeps only rows where the condition is true. That query returns nothing. Always write IS NULL or IS NOT NULL.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.