Docs
Docs/SQL Fundamentals/SQL Operators

SQL Operators

Arithmetic, comparison, and logical operators

SQL provides various operators for calculations, comparisons, and combining conditions.

Arithmetic Operators

OperatorDescriptionExample
+Additionprice + tax
-Subtractiontotal - discount
*Multiplicationprice * quantity
/Divisiontotal / count
%Modulo (remainder)id % 2
-- Calculate order totals
SELECT 
    product_name,
    price,
    quantity,
    price * quantity AS subtotal,
    price * quantity * 0.1 AS tax,
    price * quantity * 1.1 AS total
FROM order_items;

-- Find even/odd IDs
SELECT * FROM users WHERE id % 2 = 0;  -- Even IDs

Comparison Operators

OperatorMeaningExample
=Equal tostatus = 'active'
!= or <>Not equal torole != 'admin'
<Less thanage < 18
>Greater thanprice > 100
<=Less than or equalquantity <= 0
>=Greater than or equalrating >= 4

Logical Operators

-- AND: Both conditions must be true
SELECT * FROM products 
WHERE price > 50 AND category = 'Electronics';

-- OR: Either condition can be true
SELECT * FROM users 
WHERE role = 'admin' OR role = 'moderator';

-- NOT: Negates a condition
SELECT * FROM orders WHERE NOT status = 'cancelled';

-- Combining with parentheses (IMPORTANT!)
SELECT * FROM products 
WHERE category = 'Electronics' 
  AND (price < 100 OR on_sale = TRUE);

Tip: AND has higher precedence than OR. Use parentheses to make your intent clear!

Need help?

Join our Discord community for support and discussions.

Join Discord