Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >