114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
48% Percent Complete
By Kevin McAleer, 6 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.
CASE
AND
OR
In 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.
WHERE
Example: 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';
Example: Retrieve users who are either named “Alice” or have an age of 30.
SELECT * FROM users WHERE name = 'Alice' OR age = 30;
You 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
The IN operator allows you to specify a list of values for SQL to search for in a column, simplifying queries with multiple OR conditions.
Example: 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.
BETWEEN
The BETWEEN operator is used to filter data within a specific range. It’s often used with numeric or date values.
Example: Retrieve users aged between 20 and 30 (inclusive).
SELECT * FROM users WHERE age BETWEEN 20 AND 30;
Tip: BETWEEN includes the boundary values, so this query retrieves users who are exactly 20 or 30 years old as well as those in between.
LIKE
The LIKE operator enables pattern matching with the % and _ wildcards:
%
_
Example: 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_';
The 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.
IF
SELECT column1, CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE result END AS new_column FROM table_name;
Let’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:
The 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.
contact_info
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:
WHERE age > 25 AND name LIKE 'A%'
WHERE age IN (20, 25, 30)
WHERE age BETWEEN 20 AND 30
WHERE name LIKE 'A%'
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.
< Previous Next >