KevsRobots Learning Platform
By Kevin McAleer, 5 Minutes
In this lesson, we’ll explore advanced filtering techniques and conditional logic with SQL. You’ll learn how to refine your queries using multiple conditions and implement conditional expressions with the CASE statement.
AND and ORIn SQL, you can combine multiple conditions in a WHERE clause to narrow down results. The AND and OR operators are used to create more complex filters.
AND: All conditions must be true for the row to be included in the results.OR: At least one condition must be true for the row to be included in the results.AND to Refine ResultsExample: Retrieve users over the age of 25 who have an email ending with “example.com”.
SELECT * FROM users
WHERE age > 25 AND email LIKE '%example.com';
OR to Broaden ResultsExample: Retrieve users who are either named “Alice” or have an age of 30.
SELECT * FROM users
WHERE name = 'Alice' OR age = 30;
AND and ORYou can combine AND and OR in the same query, but use parentheses to control the logic and ensure accurate results.
Example: Retrieve users named “Alice” who are older than 25, or users whose age is exactly 30.
SELECT * FROM users
WHERE (name = 'Alice' AND age > 25) OR age = 30;
IN for List FilteringThe IN operator allows you to specify a list of values for SQL to search for in a column, simplifying queries with multiple OR conditions.
INExample: Retrieve users aged 25, 30, or 35.
SELECT * FROM users
WHERE age IN (25, 30, 35);
This query is equivalent to using OR conditions but is more concise.
BETWEENThe BETWEEN operator is used to filter data within a specific range. It’s often used with numeric or date values.
BETWEENExample: Retrieve users aged between 20 and 30 (inclusive).
SELECT * FROM users
WHERE age BETWEEN 20 AND 30;
Tip:
BETWEENincludes the boundary values, so this query retrieves users who are exactly 20 or 30 years old as well as those in between.
LIKEThe LIKE operator enables pattern matching with the % and _ wildcards:
%: Matches any sequence of characters._: Matches a single character.LIKE with WildcardsExample: Retrieve users whose names start with “A”.
SELECT * FROM users
WHERE name LIKE 'A%';
Example: Retrieve users with a 3-letter name where the second letter is “o”.
SELECT * FROM users
WHERE name LIKE '_o_';
CASEThe CASE statement in SQL allows you to add conditional logic to your queries. It’s often used to create calculated fields based on certain conditions, similar to IF statements in other languages.
CASE SyntaxSELECT column1,
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE result
END AS new_column
FROM table_name;
CASELet’s say you want to categorize users based on their age group.
Example: Create an age category for each user.
SELECT name, age,
CASE
WHEN age < 18 THEN 'Minor'
WHEN age BETWEEN 18 AND 65 THEN 'Adult'
ELSE 'Senior'
END AS age_category
FROM users;
In this example:
CASE with Conditions in WHEREThe CASE statement can also be used to dynamically choose values based on conditions.
Example: Mark users who don’t have an email with “No Email”.
SELECT name,
CASE
WHEN email IS NULL THEN 'No Email'
ELSE email
END AS contact_info
FROM users;
In this example, users without an email will display “No Email” in the contact_info column.
Retrieve all users whose age is either 20, 25, or 30 using the IN operator.
SELECT * FROM users
WHERE age IN (20, 25, 30);
Retrieve users with an age between 18 and 30 who also have a name starting with “B”.
SELECT * FROM users
WHERE age BETWEEN 18 AND 30 AND name LIKE 'B%';
Categorize users into “Young Adult” if they are under 25, “Adult” if between 25 and 40, and “Middle-aged” if over 40 using CASE.
SELECT name, age,
CASE
WHEN age < 25 THEN 'Young Adult'
WHEN age BETWEEN 25 AND 40 THEN 'Adult'
ELSE 'Middle-aged'
END AS age_group
FROM users;
Display all users, but show “Unknown Email” for any user who does not have an email address.
SELECT name,
CASE
WHEN email IS NULL THEN 'Unknown Email'
ELSE email
END AS contact_info
FROM users;
Here’s a recap of the advanced filtering techniques and conditional logic covered in this lesson:
| Operator / Function | Description | Example |
|---|---|---|
AND / OR |
Combines multiple conditions | WHERE age > 25 AND name LIKE 'A%' |
IN |
Filters for values within a specified list | WHERE age IN (20, 25, 30) |
BETWEEN |
Filters for values within a range | WHERE age BETWEEN 20 AND 30 |
LIKE |
Matches patterns with wildcards | WHERE name LIKE 'A%' |
CASE |
Adds conditional logic in queries | CASE WHEN age < 18 THEN 'Minor' END |
By mastering these advanced filtering techniques and conditional logic, you can create more precise and flexible SQL queries. In the next lesson, we’ll move on to working with joins to retrieve data from multiple tables.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments