How do you write a query to return rows where a column value falls within a specific range?
Posted by MaryJns
Last Updated: June 03, 2024
To write a query that returns rows where a column value falls within a specific range, you typically use the SQL SELECT statement combined with the WHERE clause. You can specify the range using the BETWEEN operator or by using comparison operators (>= and <=). Here's how you can do it using both methods:
Using the BETWEEN Operator
SELECT *
FROM table_name
WHERE column_name BETWEEN lower_value AND upper_value;
Using Comparison Operators
SELECT *
FROM table_name
WHERE column_name >= lower_value AND column_name <= upper_value;
Example
Assuming you have a table named employees with a column salary, and you want to return all employees whose salary is between 50000 and 80000, you can write the query as follows:
Using BETWEEN:
SELECT *
FROM employees
WHERE salary BETWEEN 50000 AND 80000;
Using Comparison Operators:
SELECT *
FROM employees
WHERE salary >= 50000 AND salary <= 80000;
Both queries will yield the same result set, returning all rows where the salary is within the specified range.
Related Content