Sub topic 6
Comparing Values: `=`, `<>`, `<`, `<=`, `>`, `>=`
Assume you already understand tables, rows, columns, and basic SELECT queries. This chapter uses an employees table with columns such as first_name, department, salary, and hire_date.
A comparison condition checks the value in one row against another value. In a WHERE clause, the database keeps a row when the condition is true.
The comparison operators
| Operator | Meaning | Example |
|---|---|---|
| `=` | Equal to | `department = 'Sales'` |
| `<>` | Not equal to | `department <> 'Sales'` |
| `<` | Less than | `salary < 50000` |
| `<=` | Less than or equal to | `salary <= 50000` |
| `>` | Greater than | `salary > 50000` |
| `>=` | Greater than or equal to | `salary >= 50000` |
The equality operator is a single equals sign. SQL does not use == for equality.
SELECT first_name, last_name, salary
FROM employees
WHERE salary = 50000;This query returns employees whose salary is exactly 50000. The comparison includes the value's type and meaning. A number such as 50000 is different from a text value such as '50000', although some database systems convert values automatically. Use the type that matches the column.
Numbers, text, and dates
Numeric comparisons use normal mathematical ordering.
SELECT first_name, salary
FROM employees
WHERE salary >= 60000;This keeps salaries of 60000 and higher. The >= operator includes the boundary value. By contrast, > excludes it:
SELECT first_name, salary
FROM employees
WHERE salary > 60000;Text comparisons compare strings according to the database's collation rules. A collation is the rule set used to compare and sort text, including choices about case and accents.
SELECT first_name, last_name
FROM employees
WHERE last_name <> 'Smith';This excludes rows where last_name equals Smith. It does not necessarily exclude smith, because case handling differs between databases and collations.
Dates can be compared chronologically when they are stored in a suitable date type or a consistently sortable format.
SELECT first_name, hire_date
FROM employees
WHERE hire_date < '2022-01-01';This returns employees hired before January 1, 2022. The exact date literal syntax can vary by database, so follow the dialect used by your database.
How a comparison filters rows
flowchart TD
A[Each row in employees] --> B{salary >= 60000?}
B -->|True| C[Include the row]
B -->|False| D[Discard the row]The database evaluates the condition separately for each row. A condition such as salary >= 60000 does not change the stored data. It only controls which rows appear in the result.
Boundary values and common errors
Check whether your boundary belongs in the result. Use >= when the boundary should be included and > when it should not.
A common error is reversing the comparison:
SELECT first_name, salary
FROM employees
WHERE 60000 < salary;This is valid and means the same as salary > 60000, but writing the column first usually makes the query easier to read.
Another error is using <> to find missing values. A missing value is NULL, which means unknown or absent. Ordinary comparisons with NULL do not produce true, including salary <> 60000. Use IS NULL or IS NOT NULL when testing for missing values.
Exercise
Write a query that returns the first and last names of employees whose salary is less than or equal to 45000.
Show answer
SELECT firstname, lastname FROM employees WHERE salary <= 45000;