Filtering data with WHERE Clause

The SQL WHERE clause is one of the most essential components of SQL queries. It allows you to filter rows from a database table based on specific conditions. Filtering data helps in retrieving only the relevant data, which can improve performance and reduce unnecessary data transmission.

In this chapter, we’ll take a deep dive into the WHERE clause from basic to advanced concepts. We’ll explore various conditions, operators, and techniques, so you get a comprehensive understanding of how to filter data efficiently.

The Basics of the WHERE Clause

The WHERE clause comes after the FROM clause in SQL and restricts the rows that the query returns. Without WHERE, a SELECT query would return all rows from a table.

Syntax:

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

				
			
  • column1, column2: The columns you want to select.
  • table_name: The table from which to retrieve data.
  • condition: A logical expression that determines whether a row is included in the result set.

Example 1: Basic Filtering

Let’s consider a table employees:

idnamedepartmentsalary
1John DoeHR5000
2Jane SmithIT6000
3Bob BrownIT7000
4Mary WhiteHR5000
5David GreenSales5500
				
					SELECT name, department
FROM employees
WHERE department = 'IT';

				
			

Output :

namedepartment
Jane SmithIT
Bob BrownIT

The WHERE clause filters out only those employees who belong to the IT department.

Using Comparison Operators

Comparison operators allow you to compare values in a WHERE clause. The most common comparison operators are:

  • =: Equals
  • <> or !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Example 2: Filtering with Comparison Operators

Suppose we want to retrieve all employees with a salary greater than 5500.

Query:

				
					SELECT name, salary
FROM employees
WHERE salary > 5500;

				
			

Output :

NameSalary
Jane Smith$6000
Bob Brown$7000

This query filters employees with salaries greater than 5500, returning only Jane Smith and Bob Brown.

Combining Conditions with AND, OR, and NOT

You can combine multiple conditions in the WHERE clause using logical operators:

  • AND: Returns rows when all conditions are true.
  • OR: Returns rows when any condition is true.
  • NOT: Reverses the result of a condition.

Example 3: Using AND Operator

Let’s filter employees who work in the IT department and have a salary greater than 6000.

Query:

				
					SELECT name, salary
FROM employees
WHERE department = 'IT' AND salary > 6000;

				
			

Output :

NameSalary
Bob Brown$7000

Both conditions (department = 'IT' and salary > 6000) must be true for the row to be returned.

Example 4: Using OR Operator

Now, let’s get all employees who work in either the HR department or have a salary greater than 5500.

Query:

				
					SELECT name, department, salary
FROM employees
WHERE department = 'HR' OR salary > 5500;

				
			

Output :

NameDepartmentSalary
John DoeHR$5000
Jane SmithIT$6000
Bob BrownIT$7000
Mary WhiteHR$5000
David GreenSales$5500

This query returns employees from the HR department or those whose salary is greater than 5500.

Example 5: Using NOT Operator

Let’s filter employees who are not in the IT department.

Query:

				
					SELECT name, department
FROM employees
WHERE NOT department = 'IT';

				
			

Output :

NameDepartment
John DoeHR
Mary WhiteHR
David GreenSales

The NOT operator excludes employees from the IT department.

Filtering with BETWEEN, IN, and LIKE

SQL provides special operators to simplify certain kinds of filtering.

BETWEEN

The BETWEEN operator filters a range of values.

Example:

Let’s filter employees with salaries between 5000 and 6000.

				
					SELECT name, salary
FROM employees
WHERE salary BETWEEN 5000 AND 6000;

				
			

Output:

NameSalary
John Doe$5000
Jane Smith$6000
Mary White$5000
David Green$5500

IN

The IN operator allows filtering based on a set of values.

Example:

Filter employees working in the HR or Sales department.

				
					SELECT name, department
FROM employees
WHERE department IN ('HR', 'Sales');

				
			

Output:

NameDepartment
John DoeHR
Mary WhiteHR
David GreenSales

LIKE

The LIKE operator is used for pattern matching. % represents zero or more characters, and _ represents a single character.

Example:

Filter employees whose names start with ‘J’.

				
					SELECT name
FROM employees
WHERE name LIKE 'J%';

				
			

Output:

Name
John Doe
Jane Smith

Handling NULL Values

In SQL, NULL represents missing or unknown data. To filter NULL values, you must use IS NULL or IS NOT NULL.

Example:

Let’s filter employees whose department is not NULL.

Query:

				
					SELECT name, department
FROM employees
WHERE department IS NOT NULL;

				
			

Output:

NameDepartment
John DoeHR
Jane SmithIT
Bob BrownIT
Mary WhiteHR
David GreenSales

This ensures that all rows with NULL in the department are excluded.

Advanced Filtering Techniques

Using Subqueries in WHERE Clause

A subquery (also known as an inner query or nested query) is a query within another SQL query. Subqueries are often used to filter data based on complex conditions.

Example:

Get employees whose salary is above the average salary.

				
					SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

				
			

Output:

NameSalary
Bob Brown$7000

The inner query calculates the average salary, and the outer query filters employees with a salary greater than this average.

Filtering with EXISTS

The EXISTS operator checks for the existence of rows in a subquery.

Example:

Find employees who work in departments where there are more than one employee.

				
					SELECT name, department
FROM employees e
WHERE EXISTS (SELECT 1 FROM employees WHERE department = e.department GROUP BY department HAVING COUNT(*) > 1);

				
			

Output:

NameDepartment
John DoeHR
Jane SmithIT
Bob BrownIT
Mary WhiteHR

The WHERE clause is a fundamental part of SQL, allowing you to filter rows based on specific conditions. From simple comparisons to complex subqueries, mastering the WHERE clause can significantly enhance your ability to retrieve relevant data efficiently.By understanding how to use operators like AND, OR, NOT, IN, BETWEEN, and LIKE, along with handling NULL values and using subqueries, you’ll be well-equipped to filter data in almost any scenario.This comprehensive guide ensures that you can use the WHERE clause confidently, from basic filtering to more advanced techniques. Keep experimenting with different conditions to further strengthen your understanding! Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India