SQL Operators

Operators are the symbols and keywords SQL uses to compare values, perform calculations, and combine conditions inside your queries.

What is an operator?

An operator is a symbol or keyword that tells the database to perform an operation on one or more values. Operators appear throughout SQL: in the SELECT list to calculate values, in the WHERE clause to filter rows, and in the ON clause of a join to match records. Understanding the main groups of operators is essential because almost every non-trivial query relies on them.

SQL operators fall into a few broad categories: arithmetic operators for math, comparison operators for testing relationships between values, logical operators for combining conditions, and bitwise operators for working at the bit level. This page focuses on the categories you will use most often.

Arithmetic operators

Arithmetic operators perform standard mathematical calculations on numeric values. You can use them to derive new columns, such as multiplying a unit price by a quantity to get a line total.

OperatorMeaningExample
+Additionprice + tax
-Subtractiontotal - discount
*Multiplicationquantity * unit_price
/Divisionrevenue / months
%Modulo (remainder)id % 2

Calculating values with arithmetic operators

SELECT
  product_name,
  quantity,
  unit_price,
  quantity * unit_price AS line_total
FROM order_items;

Comparison operators

Comparison operators test the relationship between two values and return a true or false result. They are the backbone of the WHERE clause, where they decide which rows are included in the output.

OperatorMeaningExample
=Equal tostatus = 'active'
<> or !=Not equal tocountry <> 'US'
>Greater thanage > 18
<Less thanprice < 100
>=Greater than or equal toscore >= 60
<=Less than or equal tostock <= 5

Filtering rows with comparison operators

SELECT customer_name, order_total
FROM orders
WHERE order_total >= 500
  AND status <> 'cancelled';
Note: The standard SQL operator for 'not equal' is <>, and most databases also accept !=. Prefer <> when you want the most portable code.

Logical and special operators

Logical operators combine or negate conditions, letting you build filters from several tests. SQL also provides special keyword operators that make common comparisons easier to read than a chain of arithmetic tests.

  • AND returns rows only when every condition is true.
  • OR returns rows when at least one condition is true.
  • NOT reverses the result of the condition that follows it.
  • BETWEEN checks whether a value falls within an inclusive range.
  • IN checks whether a value matches any value in a list.
  • LIKE tests a value against a text pattern using wildcards.
  • IS NULL checks whether a value is missing rather than comparing it with =.

Combining conditions with logical operators

SELECT product_name, category, price
FROM products
WHERE category IN ('Books', 'Music')
  AND price BETWEEN 10 AND 50
  AND product_name LIKE 'The %';
Note: You cannot test for missing data with = NULL, because comparing anything to NULL yields an unknown result rather than true. Always use IS NULL or IS NOT NULL to check for missing values.

When several operators appear in one expression, SQL follows an order of precedence: arithmetic operators run first, then comparison operators, then NOT, then AND, and finally OR. To make your intent clear and avoid surprises, wrap combined conditions in parentheses so they are evaluated in the order you expect.

Exercise: SQL Operators

How does the AND operator differ from OR when combining two conditions?