SQLite – 8 – Filtering Data with WHERE

Filtering data is a critical aspect of database management, allowing you to extract specific information from a large dataset. In SQLite, the WHERE clause is used to filter data from tables based on specified conditions. This clause allows you to retrieve only the rows that meet certain criteria, making it a powerful tool for extracting relevant information. In this discussion, we will explore how to filter data using the WHERE clause in SQLite, its syntax, and practical examples.

Basic Syntax for WHERE Clause:

The basic syntax of the WHERE clause in SQLite is as follows:

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

In this syntax:

  • column1, column2, ... specifies the columns you want to retrieve, and you can use * to select all columns.
  • table_name is the name of the table from which you want to retrieve data.
  • WHERE condition is the crucial part where you specify the filtering conditions.

Example 1: Filtering Data by a Single Condition

Suppose you have a table called Employees, and you want to retrieve employees with a salary greater than 50,000:

SELECT FirstName, LastName, Salary FROM Employees WHERE Salary > 50000;

In this query:

  • We specify the columns we want to retrieve as FirstName, LastName, and Salary.
  • We specify the table name as Employees.
  • We use the WHERE clause to filter data where the Salary column is greater than 50,000.

This query will return the names and salaries of employees who meet the specified condition.

Example 2: Filtering Data with Multiple Conditions

You can use logical operators like AND, OR, and NOT to create more complex conditions. For example, if you want to retrieve employees with a salary greater than 50,000 who are also in the “Sales” department:

SELECT FirstName, LastName, Salary FROM Employees WHERE Salary > 50000 AND Department = 'Sales';

In this query:

  • We use the AND operator to combine two conditions: Salary > 50000 and Department = 'Sales'.
  • This query will return the names and salaries of employees who meet both conditions.

Example 3: Filtering Data with String Patterns

The WHERE clause can be used to filter data based on string patterns using the LIKE operator and wildcard characters % (matches any sequence of characters) and _ (matches any single character). For instance, if you want to retrieve employees whose last names start with “Smi”:

SELECT FirstName, LastName FROM Employees WHERE LastName LIKE 'Smi%';

In this query:

  • We use the LIKE operator with the pattern 'Smi%' to match last names that start with “Smi.”

This query will return the first names and last names of employees with last names like “Smith,” “Smithson,” etc.

Example 4: Filtering Data with NULL Values

You can also use the WHERE clause to filter data based on NULL values. For example, if you want to retrieve employees who do not have an assigned manager:

SELECT FirstName, LastName FROM Employees WHERE ManagerID IS NULL;

In this query:

  • We use the IS NULL condition to filter rows where the ManagerID column is NULL.

This query will return the first names and last names of employees who do not have a manager assigned.

Example 5: Combining Multiple Conditions

You can create complex filtering conditions by combining multiple conditions with logical operators. For instance, if you want to retrieve employees who have a salary greater than 60,000 or are in the “Marketing” department:

SELECT FirstName, LastName, Salary, Department FROM Employees WHERE Salary > 60000 OR Department = 'Marketing';

In this query:

  • We use the OR operator to combine two conditions: Salary > 60000 and Department = 'Marketing'.

This query will return the names, salaries, and departments of employees who meet at least one of the specified conditions.

Conclusion:

The WHERE clause in SQLite is a powerful tool for filtering data from tables based on specified conditions. It allows you to extract relevant information from your database, whether it’s based on simple comparisons, string patterns, NULL values, or complex combinations of conditions. Understanding how to use the WHERE clause effectively is essential for database administrators and developers to retrieve meaningful insights from their data.