In SQL, comparison operators are used in the WHERE clause to compare values and establish conditions for filtering data. These operators allow you to perform comparisons based on equality, inequality, greater than, less than, and other relationships between values. Here’s an overview of commonly used comparison operators in SQL.
Table of Contents
ToggleEquality Operator (=)
The equality operator is used to compare if two values are equal.
SELECT *
FROM employees
WHERE department = 'Sales';
This query retrieves all columns from the “employees” table where the department is ‘Sales’.
LIKE Operator
The LIKE operator is used to perform pattern matching comparisons on string values. It allows you to search for values based on patterns using wildcard characters.
SELECT *
FROM customers
WHERE name LIKE 'J%';
In this example, query retrieves all columns from the “customers” table where the name starts with the letter ‘J’. The ‘%’ symbol represents all other letters following letter ‘J’.
BETWEEN Operator
The BETWEEN operator is used to specify a range of values to include in the result set.
SELECT *
FROM employees
WHERE salary BETWEEN 50000 AND 70000;
This query retrieves all columns from the “employees” table where the salary is between 50,000 and 70,000.
IN Operator
The IN operator is used to specify multiple values for comparison. It allows you to match a value against a list of predefined values.
SELECT *
FROM employees
WHERE department IN ('Sales', 'Marketing');
The query retrieves all columns from the “employees” table where the department is either ‘Sales’ or ‘Marketing’.
These are just a few examples of comparison operators that can be used in the WHERE clause. SQL offers a wide range of operators to handle various comparison scenarios. By utilizing these operators effectively, you can filter and retrieve data that meets specific conditions, enabling you to extract meaningful insights from your database.