MODULO SQL & Database Design Master SeriesLAB

"Filtering Frenzy: Mastering the WHERE Clause and Beyond"

Filtering in SQL: The WHERE Clause

In SQL, filtering is the process of narrowing down the data in your result set to only include the rows that meet certain conditions. The most basic way to filter data in SQL is by using the WHERE clause.

Syntax: WHERE Clause

The WHERE clause is used to filter records based on conditions. The basic syntax is as follows:

SELECT column1, column2
FROM tablename
WHERE condition;

Example: WHERE Clause

Suppose we have a table called employees with columns name, age, and salary. We can use the WHERE clause to select only the employees who are older than 30:

SELECT name, age, salary
FROM employees
WHERE age > 30;

Combining Conditions: AND, OR, NOT

In many cases, you'll want to filter data based on multiple conditions. That's where the AND, OR, and NOT operators come in.

  • AND: Used to combine two or more conditions. All conditions must be true for the row to be included in the result set.
  • OR: Used to combine two or more conditions. At least one condition must be true for the row to be included in the result set.
  • NOT: Used to negate a condition.

Example: AND, OR, NOT

Suppose we want to select employees who are older than 30 and have a salary greater than $50,000:

SELECT name, age, salary
FROM employees
WHERE age > 30 AND salary > 50000;

Or, suppose we want to select employees who are either older than 30 or have a salary greater than $50,000:

SELECT name, age, salary
FROM employees
WHERE age > 30 OR salary > 50000;

Finally, suppose we want to select employees who are not older than 30:

SELECT name, age, salary
FROM employees
WHERE NOT age > 30;

BETWEEN Operator

The BETWEEN operator is used to select values within a given range. The range is inclusive, meaning that the values at the start and end of the range are included in the result set.

Syntax: BETWEEN Operator

The basic syntax is as follows:

SELECT column1, column2
FROM tablename
WHERE columnname BETWEEN value1 AND value2;

Example: BETWEEN Operator

Suppose we want to select employees who have a salary between $40,000 and $60,000:

SELECT name, age, salary
FROM employees
WHERE salary BETWEEN 40000 AND 60000;

LIKE Operator

The LIKE operator is used to select values that match a specified pattern. The pattern can include wildcards, such as % and _.

  • %: Matches any number of characters.
  • _: Matches a single character.

Syntax: LIKE Operator

The basic syntax is as follows:

SELECT column1, column2
FROM tablename
WHERE columnname LIKE pattern;

Example: LIKE Operator

Suppose we want to select employees whose name starts with "J":

SELECT name, age, salary
FROM employees
WHERE name LIKE 'J%';

Or, suppose we want to select employees whose name contains "son":

SELECT name, age, salary
FROM employees
WHERE name LIKE '%son%';

IN Operator

The IN operator is used to select values that are in a specified list.

Syntax: IN Operator

The basic syntax is as follows:

SELECT column1, column2
FROM tablename
WHERE columnname IN (value1, value2, ...);

Example: IN Operator

Suppose we want to select employees who have a salary of either $40,000 or $50,000:

SELECT name, age, salary
FROM employees
WHERE salary IN (40000, 50000);

EXISTS Operator

The EXISTS operator is used to test whether a subquery returns any rows.

Syntax: EXISTS Operator

The basic syntax is as follows:

SELECT column1, column2
FROM tablename
WHERE EXISTS (subquery);

Example: EXISTS Operator

Suppose we want to select employees who have a salary greater than the average salary:

SELECT name, age, salary
FROM employees e
WHERE EXISTS (
  SELECT 1
  FROM employees
  WHERE salary > (SELECT AVG(salary) FROM employees)
  AND name = e.name
);

⚔️ YOUR MISSION

Mission: Filtering Frenzy

Your mission is to write a SQL query that selects employees who meet the following conditions:

  • Are older than 30
  • Have a salary greater than $50,000
  • Have a name that starts with "J"
  • Have a salary that is not equal to $60,000

Use the employees table and the following columns: name, age, and salary.

Good luck!

SQL practice editor
Read the exercise prompt
SELECT name, age, salary
FROM employees
WHERE age > 30
  AND salary > 50000
  AND name LIKE 'J%'
  AND salary != 60000;
WAITING FOR SIGNAL...
BACKNEXT STAGE