EQUAL TO Operator (=) - SQL


Overview


The Equal to operator (=) in SQL is used to compare two expressions. It returns true (1) if the expressions are equal and false (0) otherwise. It is commonly used in SQL queries for filtering records.

Syntax:

SELECT column1 = value

column1 = value checks if the value in column1 is equal to value.

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 Equal to operator is used to evaluate whether the expression 5 = 5 is true. Since 5 is equal to 5, the query will return an output of 1, indicating that it is true.

Example:

SELECT 5 = 5


Example: Hard-coded values (FALSE) value


In this example, the Equal to operator is used to evaluate whether the expression 5 = 2 is true. Since 5 is not 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


We are returning the vacation_days column and a column that checks if each value in the vacation_days column is equal to 2. The final output is the original vacation_days column alongside the column checking whether the value is equal to 2 or not. If the value is equal to 2, it will return a 1 and if not it will return a 0.

Example:

SELECT vacation_days, vacation_days = 2
FROM company.employees


Example: Query with the WHERE Statement


In this example, we are filtering for only rows in department_id 1. We are returning the vacation_days column and a column that checks if each value in the vacation_days column is equal to 2. The final output is the original vacation_days column alongside the column checking whether the value is equal to 2 or not. If the value is equal to 2, it will return a 1 and if not it will return a 0.

Example:

SELECT vacation_days, vacation_days = 2
FROM company.employees
WHERE department_id = 1