LESS THAN OR EQUAL TO Operator (<=) - SQL


Overview


The Less than or equal to operator (<=) is used to compare two expressions. It returns true if the left expression is less than or equal to 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 or equal to 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 or equal to operator is used to evaluate whether 5 <= 10 is true. Since 5 is less than or equal to 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 or equal to operator is used to evaluate whether 5 <= 2 is true. Since 5 is not less than or equal to 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 or equal to 2. The final output is the original vacation_days column alongside the column checking whether it’s less than or equal to 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 or equal to operator, we only want department_id less than or equal to 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 or equal to 2. The final output is the original vacation_days column alongside the column checking whether it’s less than or equal to 2.

Example:

SELECT vacation_days, vacation_days <= 2
FROM company.employees
WHERE department_id <= 3