SQL Operators
A database does not think like humans.
It only understands conditions.
Whenever we ask a database a question such as:
- Students older than 18
- Employees working in Delhi and earning more than 30,000
- Products priced between 1,000 and 5,000
We are actually asking SQL to compare values and apply logic. To perform these comparisons and checks, SQL uses Operators.
What Are Operators in SQL?
Operators in SQL are symbols or keywords used in the WHERE clause to:
- Compare values
- Combine multiple conditions
- Filter records precisely
Operators help SQL decide which rows should be selected and which should be ignored.
Types of Operators in SQL
SQL operators are commonly grouped into:
- Comparison Operators
- Logical Operators
- LIKE Operator
- BETWEEN Operator
- IN Operator
1. Comparison Operators
Why Comparison Operators Are Needed
In databases, data is stored as values:
- Numbers (salary, age)
- Text (name, city)
- Dates
Very often, we want to compare these values.
Examples
- Salary greater than 30,000
- Age less than 18
- City equal to Delhi
To perform such checks, SQL uses Comparison Operators.
What Comparison Operators Do
Comparison operators compare two values and return true or false for each row.
If the condition is true, the row is selected.
Common Comparison Operators
| Operator | Meaning |
|---|---|
| = | Equal to |
| != / <> | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example
SELECT *FROM employeesWHERE salary > 30000;Only employees whose salary is greater than 30,000 are selected.
2. Logical Operators
Why Logical Operators Are Needed
Real-life conditions are rarely single.
We often want:
- This AND that
- This OR that
- Everything except something
SQL needs a way to combine multiple conditions, and that is done using Logical Operators.
What Logical Operators Do
Logical operators combine two or more conditions and evaluate them together.
Common Logical Operators
| Operator | Meaning |
|---|---|
| AND | All conditions must be true |
| OR | Any one condition must be true |
| NOT | Reverses the condition |
Example
SELECT *FROM employeesWHERE city = 'Delhi' AND salary > 25000;Both conditions must be true for a record to be selected.
3. LIKE Operator
Why LIKE Operator Exists
Sometimes we do not know the exact value.
We only know:
- Starting letters
- Ending letters
- A part of the word
Example
- Names starting with “A”
- Cities ending with “pur”
Exact comparison (=) does not work here.
What LIKE Does
The LIKE operator is used to search for patterns in text data.
It works using wildcards.
Wildcards Used with LIKE
| Wildcard | Meaning |
|---|---|
| % | Any number of characters |
| _ | Exactly one character |
Example
SELECT *FROM employeesWHERE name LIKE 'A%';Selects names that start with the letter A.
4. BETWEEN Operator
Why BETWEEN Operator Is Needed
To filter values in a range, we could write:
salary >= 20000 AND salary <= 40000This works, but it is long and repetitive.
SQL provides BETWEEN to make range conditions simpler and cleaner.
What BETWEEN Does
The BETWEEN operator selects values within a specified range.
The range is inclusive (start and end values are included).
Example
SELECT *FROM employeesWHERE salary BETWEEN 20000 AND 40000;5. IN Operator
Why IN Operator Is Needed
When checking for multiple possible values, using many OR conditions reduces readability.
Example
city = 'Delhi' OR city = 'Mumbai' OR city = 'Pune'What IN Does
The IN operator allows checking a value against a list of values.
It makes queries shorter and more readable.
Example
Example
SELECT *FROM employeesWHERE city IN ('Delhi', 'Mumbai', 'Pune');Two Minute Drill
- Operators help SQL apply conditions
- Comparison operators compare values
- Logical operators combine conditions
- LIKE is used for pattern matching
- BETWEEN checks a range of values
- IN matches values from a list
- Operators are mostly used with WHERE