Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Using the WHERE Clause for Filtering in SQL


The WHERE clause in SQL is used to filter records and retrieve only the rows that meet specific conditions. It allows you to define criteria to select data that matches particular values or patterns, making it a powerful tool for querying databases efficiently.

1. Basic Syntax of the WHERE Clause

The WHERE clause is used in conjunction with the SELECT, UPDATE, DELETE, and other SQL statements to filter data based on specified conditions.

Syntax:

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

The condition defines the criteria used to filter the rows. Conditions can include comparisons, logical operators, and pattern matching, among other things.

2. Filtering with Comparison Operators

SQL provides several comparison operators that can be used in the WHERE clause to filter data based on specific criteria. The most common comparison operators are:

Example: Using Comparison Operators

      -- Select all employees who have a salary greater than 50000
      SELECT Name, Salary
      FROM Employees
      WHERE Salary > 50000;
    

This query retrieves the 'Name' and 'Salary' of employees whose salary is greater than 50,000.

Example: Not Equal to

      -- Select all employees whose department is not 'Sales'
      SELECT Name, Department
      FROM Employees
      WHERE Department != 'Sales';
    

This query retrieves the 'Name' and 'Department' of employees who are not in the 'Sales' department. The != operator is used to filter out those rows where the department is 'Sales'.

3. Using Logical Operators

Logical operators allow you to combine multiple conditions in the WHERE clause. The most common logical operators are:

Example: Using AND

      -- Select employees who are in the 'HR' department and have a salary greater than 60000
      SELECT Name, Department, Salary
      FROM Employees
      WHERE Department = 'HR' AND Salary > 60000;
    

This query retrieves the 'Name', 'Department', and 'Salary' of employees who are in the 'HR' department and have a salary greater than 60,000. Both conditions must be true for the row to be included.

Example: Using OR

      -- Select employees who are either in the 'HR' department or have a salary greater than 60000
      SELECT Name, Department, Salary
      FROM Employees
      WHERE Department = 'HR' OR Salary > 60000;
    

This query retrieves the 'Name', 'Department', and 'Salary' of employees who are either in the 'HR' department or have a salary greater than 60,000. At least one condition must be true for the row to be included.

Example: Using NOT

      -- Select employees whose department is not 'HR'
      SELECT Name, Department
      FROM Employees
      WHERE NOT Department = 'HR';
    

This query retrieves the 'Name' and 'Department' of employees whose department is not 'HR'. The NOT operator negates the condition.

4. Filtering with Pattern Matching (LIKE)

The LIKE operator in SQL is used to filter data based on a specified pattern. It is often used for filtering text or string data.

Example: Using LIKE for Pattern Matching

      -- Select all employees whose name starts with 'J'
      SELECT Name
      FROM Employees
      WHERE Name LIKE 'J%';
    

This query retrieves the 'Name' of all employees whose names begin with the letter 'J'. The % symbol is used as a wildcard to match any number of characters.

Example: Using LIKE with a Single Character

      -- Select all employees whose name is 5 characters long and starts with 'J'
      SELECT Name
      FROM Employees
      WHERE Name LIKE 'J____';
    

This query retrieves the 'Name' of all employees whose name is exactly 5 characters long and starts with the letter 'J'. The _ symbol is used as a wildcard for a single character.

5. Filtering with Range (BETWEEN)

The BETWEEN operator is used to filter data within a specific range. It can be used with numbers, dates, and text.

Example: Using BETWEEN for Numeric Range

      -- Select all products with prices between 10 and 50
      SELECT ProductName, Price
      FROM Products
      WHERE Price BETWEEN 10 AND 50;
    

This query retrieves the 'ProductName' and 'Price' of products whose price is between 10 and 50, inclusive. The BETWEEN operator makes it easy to specify a range of values.

Example: Using BETWEEN for Date Range

      -- Select all orders placed between January 1st and January 31st
      SELECT OrderID, OrderDate
      FROM Orders
      WHERE OrderDate BETWEEN '2024-01-01' AND '2024-01-31';
    

This query retrieves the 'OrderID' and 'OrderDate' of orders placed within the month of January 2024. The BETWEEN operator is inclusive, meaning the dates '2024-01-01' and '2024-01-31' are included in the results.

6. Filtering with NULL Values

In SQL, NULL represents a missing or undefined value. To filter records with NULL values, you use the IS NULL or IS NOT NULL conditions.

Example: Checking for NULL Values

      -- Select employees who do not have a manager (NULL ManagerID)
      SELECT Name
      FROM Employees
      WHERE ManagerID IS NULL;
    

This query retrieves the 'Name' of employees who do not have a manager assigned, as indicated by a NULL value in the 'ManagerID' column.

Example: Checking for Non-NULL Values

      -- Select employees who have a manager assigned
      SELECT Name
      FROM Employees
      WHERE ManagerID IS NOT NULL;
    

This query retrieves the 'Name' of employees who have a manager assigned, as indicated by a non-NULL value in the 'ManagerID' column.

7. Conclusion

The WHERE clause is one of the most powerful tools in SQL, allowing you to filter records based on various conditions. By using comparison operators, logical operators, pattern matching, ranges, and NULL checks, you can retrieve specific data from your tables, making your queries more efficient and effective. Understanding how to use the WHERE clause is essential for anyone working with SQL.



Advertisement

Advertisement

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java