LESS THAN Operator (<) - SQL
Overview
The Less than operator (<) is used to compare two expressions. It returns true if the left expression is less than the right expression. It can be used in SQL queries to filter records based on a comparison.
Syntax:
SELECT number1 < number2
number1 < number2
checks if the value number1 is less than number2.
Sample Data:
first_name | vacation_days |
---|---|
Frank | 5 |
Jane | 2 |
Ashley | 3 |
Glenn | -3 |
Kelly | 2 |
Richard | -7 |
George | 2 |
Kyle | 1 |
James | -2 |
Gustavo | -10 |
Example: Hard-coded values (TRUE) value
In this example, the Less than operator is used to evaluate whether the 5 < 10 is true. Since 5 is less than 10, the query will return an output of 1, indicating that it is true.
Example:
SELECT 5 < 10
Example: Hard-coded values (FALSE) value
In this example, the Less than operator is used to evaluate whether the 5 < 2 is true. Since 5 is not less than 2, the query will return an output of 0, indicating that it is false.
Example:
SELECT 5 < 2
Example: Query without the WHERE Statement
In this example, we are returning the vacation_days column and a column that checks if each value in the vacation_days column is less than 2. The final output is the original vacation_days column alongside the column checking whether it’s less than 2.
Example:
SELECT vacation_days, vacation_days < 2
FROM company.employees
Example: Query with the WHERE Statement
In this example, we are filtering for rows in the department_id column using the less than operator, we only want department_id less than 3. We are also returning the vacation_days column and a column that checks if each value in the vacation_days column is less than 2. The final output is the original vacation_days column alongside the column checking whether it’s less than 2.
Example:
SELECT vacation_days, vacation_days < 2
FROM company.employees
WHERE department_id < 3