MYSQL

3.Mastering SQL WHERE Clause Operators: A Complete Guide with Employee & Department Examples

🔍 Mastering SQL WHERE Clause Operators: A Complete Guide with Employee & Department Examples

The WHERE clause in SQL is one of the most important features when it comes to filtering and retrieving specific records from a database. It acts like a gatekeeper — only returning rows that meet the conditions you define.

In this detailed tutorial, we’ll explore all the commonly used operators in the WHERE clause. We’ll explain how each one works using practical examples with our familiar employees and departments tables.


📊 Database Schema Overview

Let’s assume the following two tables exist in your database:

🔹 departments Table

CREATE TABLE departments (
    department_id INTEGER PRIMARY KEY,
    department_name VARCHAR(30),
    location_id INTEGER
);

🔹 employees Table

CREATE TABLE employees (
    employee_id INTEGER,
    first_name VARCHAR(20),
    last_name VARCHAR(25),
    email VARCHAR(25),
    phone_number VARCHAR(20),
    hire_date DATE,
    job_id VARCHAR(10),
    salary INTEGER,
    commission_pct INTEGER,
    manager_id INTEGER,
    department_id INTEGER,
    CONSTRAINT pk_emp PRIMARY KEY (employee_id),
    CONSTRAINT fk_deptno FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

Now let’s fill them with sample data to run our queries.


✅ SQL WHERE Clause Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;

This condition can include comparison, range, pattern, and logical operators. Let’s break each one down.


1️⃣ = Equal To Operator

Returns records that match an exact value.

SELECT * FROM employees
WHERE department_id = 1;

🟢 Use Case: Find employees in the IT department.


2️⃣ > Greater Than Operator

Returns records where a column value is greater than the specified value.

SELECT * FROM employees
WHERE salary > 60000;

🟢 Use Case: Get high-salaried employees.


3️⃣ < Less Than Operator

Returns records where the value is less than the specified number.

SELECT * FROM employees
WHERE age < 30;

🟢 Use Case: Find younger employees or freshers.


4️⃣ >= Greater Than or Equal Operator

Matches values greater than or equal to a number.

SELECT * FROM employees
WHERE salary >= 50000;

🟢 Use Case: Include employees with ₹50,000 or more salary.


5️⃣ <= Less Than or Equal Operator

Matches values that are less than or equal to the given value.

SELECT * FROM employees
WHERE commission_pct <= 10;

🟢 Use Case: Get employees with low commission rates.


6️⃣ <> or != Not Equal To

Returns rows that do not match the specified value.

SELECT * FROM employees
WHERE department_id <> 2;

🔁 Some SQL engines also allow:

WHERE department_id != 2;

🟢 Use Case: Exclude employees from the HR department.


7️⃣ BETWEEN Operator

Selects values within a range (inclusive).

SELECT * FROM employees
WHERE salary BETWEEN 40000 AND 70000;

🟢 Use Case: Find mid-level earners.


8️⃣ LIKE Pattern Matching

Searches for patterns using % (wildcard for multiple characters) and _ (wildcard for one character).

🔸 Names starting with ‘A’

SELECT * FROM employees
WHERE first_name LIKE 'A%';

🔸 Names ending in ‘n’

SELECT * FROM employees
WHERE last_name LIKE '%n';

🔸 Names with 5 characters

SELECT * FROM employees
WHERE first_name LIKE '_____';

🟢 Use Case: Search for name patterns, email filters, phone prefixes, etc.


9️⃣ IN Operator

Checks if the value exists in a list of values.

SELECT * FROM employees
WHERE department_id IN (1, 2);

🟢 Use Case: Get employees from multiple departments.


🔟 Combining Conditions with AND, OR, and NOT

🔹 AND Operator

SELECT * FROM employees
WHERE salary > 40000 AND department_id = 2;

🔹 Result: Employees with salary > ₹40,000 in HR.


🔹 OR Operator

SELECT * FROM employees
WHERE job_id = 'IT_PROG' OR job_id = 'HR_REP';

🔹 Result: Employees who are either programmers or HR reps.


🔹 NOT Operator

SELECT * FROM employees
WHERE NOT department_id = 1;

🔹 Result: Everyone not in the IT department.


🔁 Using Parentheses for Complex Filters

SQL respects logical precedence, but you can control it using parentheses.

SELECT * FROM employees
WHERE department_id = 1 AND (salary > 50000 OR commission_pct > 10);

🟢 Use Case: Employees in IT department who either earn a high salary or have a high commission.


📚 Example: Filtering with JOIN

Combine tables using JOIN and apply WHERE filters.

SELECT e.first_name, e.salary, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.department_name = 'Sales' AND e.salary > 60000;

🟢 Use Case: Sales department employees with high salaries.


📝 Practice Queries

Here are some SQL queries to test your skills:

  1. Get employees whose name starts with ‘D’ and are in department 3.
SELECT * FROM employees
WHERE first_name LIKE 'D%' AND department_id = 3;
  1. Find employees hired before 2020.
SELECT * FROM employees
WHERE hire_date < '2020-01-01';
  1. Find employees with job_id ‘MGR’ or ‘CEO’.
SELECT * FROM employees
WHERE job_id IN ('MGR', 'CEO');

🧠 Summary of WHERE Clause Operators

Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> / != Not equal to
BETWEEN Range (inclusive)
LIKE Pattern matching with wildcards
IN Match one of multiple values
AND Combine multiple true conditions
OR At least one condition must be true
NOT Negates a condition

🏁 Final Thoughts

Using the WHERE clause effectively is key to writing precise and powerful SQL queries. Whether you’re filtering based on salaries, departments, dates, or patterns — the operators in the WHERE clause give you full control over the data you retrieve.

Practice combining multiple conditions and using joins with filters to simulate real-world reporting queries.

📌 Pro Tip: Always test queries with sample data and consider indexes on columns used in WHERE conditions to improve performance.

Leave a Reply

Your email address will not be published. Required fields are marked *