SQL Querying Employees with Department & Salary Criteria (WHERE, AND, BETWEEN)
To retrieve employees from an Employees table that match specific criteria (such as a particular department and a salary range), you typically use a SELECT statement with a WHERE clause. Commonly, you combine conditions with AND and filter numeric ranges using BETWEEN.
A canonical template is:
1SELECT * 2FROM Employees 3WHERE department = 'Engineering' 4 AND salary BETWEEN 70000 AND 100000;
This returns all rows where the employee is in the specified department and their salary falls within the inclusive bounds.
SQL WHERE clause and filtering (tutorial)
Key concepts you’ll use
- WHERE restricts which rows are returned.
- AND combines multiple filters.
- BETWEEN makes range filtering concise.
- NULL values require care (plain comparisons with
=don’t matchNULL).
[CalloutBlock]s below highlight practical gotchas when writing these queries.
Write the employee-filtering query (department + salary range)
- 1Step 1
Use
SELECT *to return all columns, or list specific columns likeemployee_id, name, department, salaryfor clarity and efficiency. - 2Step 2
Specify the table:
FROM Employees. - 3Step 3
Use
department = 'SomeDepartment'(match the exact values stored in the table). - 4Step 4
Use
salary BETWEEN min AND max(range boundaries are inclusive in SQL’sBETWEEN). - 5Step 5
Join department and salary filters with
ANDinside the sameWHEREclause. - 6Step 6
Confirm whether rows with boundary salaries should be included; also check whether
salaryordepartmentcan beNULLand decide whether to exclude them.
Example queries
1) Department + salary range using BETWEEN
1SELECT * 2FROM Employees 3WHERE department = 'Sales' 4 AND salary BETWEEN 50000 AND 90000;
2) Department + salary range (alternative with >= and <=)
Some teams prefer explicit comparisons:
1SELECT * 2FROM Employees 3WHERE department = 'Sales' 4 AND salary >= 50000 5 AND salary <= 90000;
3) Multiple criteria examples
- Multiple departments:
1SELECT * 2FROM Employees 3WHERE department IN ('Sales', 'Marketing') 4 AND salary BETWEEN 50000 AND 90000;
- Salary upper bound only:
1SELECT * 2FROM Employees 3WHERE department = 'Engineering' 4 AND salary <= 120000;
Pro Tip: Prefer explicit column lists
Using SELECT * is fine for practice, but in production prefer SELECT employee_id, name, department, salary to reduce transferred data and avoid surprises if the schema changes.
Beware NULLs in filtered columns
If department or salary can be NULL, then conditions like department = 'Sales' or salary BETWEEN 50000 AND 90000 will not match NULL rows. If you need to include them, you must handle NULL explicitly with IS NULL (or decide to exclude them).
How criteria narrow the result set
Conceptual example of filtering steps
From requirement to SQL
Define criteria
1Identify the department value and the salary min/max boundaries."
Select & from
2Choose columns and set FROM Employees."
Filter with WHERE
3Add WHERE department = ... AND salary BETWEEN ...."
Test and validate
4Verify inclusive/exclusive behavior on boundaries and check for NULL handling."
Common questions & edge cases
Knowledge Check
Which SQL query returns employees in the 'Sales' department with salaries from 50000 to 90000?
Explore Related Topics
Master Class: SQL Window Functions
SQL window functions let you calculate aggregates (e.g., sums, ranks) over a defined set of rows while preserving each row’s identity, enabling running totals, moving averages, and ranking without collapsing the result set.
- Syntax follows
FUNCTION() OVER (PARTITION BY … ORDER BY … [FRAME_CLAUSE]), where PARTITION groups rows and ORDER defines their sequence. - Ranking functions differ:
ROW_NUMBER()always increments,RANK()skips numbers on ties, andDENSE_RANK()gives consecutive ranks despite ties. - The query planner processes window functions after FROM/JOIN, WHERE, GROUP BY/HAVING, then sorts by partition and order before applying the window logic.
- FRAME clauses (
ROWSorRANGE) control the exact sliding window, e.g.,ROWS BETWEEN 2 PRECEDING AND CURRENT ROW. - Because they require sorting, window functions can be costly on large tables; appropriate indexes on partition and order columns are essential.
Designing an ER Diagram for a Hospital Management System (Patients, Doctors, Departments, Appointments)
Object Query Language (OQL) and Its Relation to SQL