loading...

June 2, 2024

Mastering SQL Filtering: WHERE, LIKE, AND, OR, IS NULL

Hey there, fellow code adventurers!

As your friendly neighborhood school principal turned coding enthusiast, I’m back with another thrilling chapter from my “Intermediate SQL” saga on DataCamp. Today, we’re diving into the wild world of SQL filtering – it’s like being a detective, but instead of magnifying glasses, we’re using WHERE clauses!

The WHERE Clause: Your SQL Superpower

Basic Usage: The Bread and Butter

Picture this: you’re at a buffet (the database), but you only want the desserts from 2020 (because that’s when we all needed extra sweetness, right?). Here’s how you’d do it in SQL:

SELECT *
FROM films
WHERE release_year = 2020;

Comparison Operators: The Spice of SQL Life

Greater Than (>): For When You’re Feeling Futuristic

SELECT *
FROM films
WHERE release_year > 2010;

Less Than (<): Nostalgia, Anyone?

SELECT *
FROM films
WHERE release_year < 2000;

Equal To (=): The Goldilocks of Operators

SELECT *
FROM films
WHERE release_year = 1995;

Greater Than or Equal To (>=): For Overachievers

SELECT *
FROM films
WHERE release_year >= 2015;

Less Than or Equal To (<=): When You’re Feeling Inclusive

SELECT *
FROM films
WHERE release_year <= 1980;

Not Equal To (<> or !=): For Rebels

SELECT *
FROM films
WHERE release_year <> 1990;

String Filtering: Because Words Matter Too!

SELECT *
FROM films
WHERE title = 'Inception';

Logical Operators: The AND, OR, BETWEEN Love Triangle

OR: When You Can’t Make Up Your Mind

SELECT *
FROM films
WHERE release_year = 1995 OR release_year = 2005;

AND: For the Picky Data Connoisseur

SELECT *
FROM films
WHERE release_year > 1990 AND release_year < 2000;

BETWEEN: The Lazy Way (We Love It!)

SELECT *
FROM films
WHERE release_year BETWEEN 1990 AND 2000;

Pattern Matching: LIKE, NOT LIKE, IN (The Cool Kids’ Club)

LIKE: For the “Kinda, Sorta” Moments

SELECT *
FROM films
WHERE title LIKE 'Star%';

NOT LIKE: When You’re Feeling Exclusive

SELECT *
FROM films
WHERE title NOT LIKE 'Star%';

IN: The VIP List

SELECT *
FROM films
WHERE release_year IN (1995, 2005, 2015);

Wildcard Placement: The SQL Magic Tricks

  • %: The “Whatever” Wildcard (matches zero or more characters)
  • _: The “One and Only” Wildcard (matches exactly one character)

Dealing with Missing Values: The SQL Ghost Busters

IS NULL: Finding the Invisible

SELECT *
FROM films
WHERE director IS NULL;

IS NOT NULL: When You’re Tired of the Invisible

SELECT *
FROM films
WHERE director IS NOT NULL;

The Grand Finale: Summary Table

ConceptDescriptionExample
WHEREFilters records based on a conditionSELECT * FROM films WHERE release_year = 2020;
Comparison OperatorsFilters using >, <, =, >=, <=, <>SELECT * FROM films WHERE release_year > 2010;
WHERE with StringsFilters records based on string valuesSELECT * FROM films WHERE title = 'Inception';
ORCombines multiple conditions (either true)SELECT * FROM films WHERE release_year = 1995 OR 2005;
ANDCombines multiple conditions (both true)SELECT * FROM films WHERE release_year > 1990 AND < 2000;
BETWEENFilters within a rangeSELECT * FROM films WHERE release_year BETWEEN 1990 AND 2000;
LIKEPattern matching with wildcardsSELECT * FROM films WHERE title LIKE 'Star%';
NOT LIKEOpposite of LIKESELECT * FROM films WHERE title NOT LIKE 'Star%';
INFilters based on multiple valuesSELECT * FROM films WHERE release_year IN (1995, 2005);
IS NULLChecks for missing valuesSELECT * FROM films WHERE director IS NULL;
IS NOT NULLChecks for non-missing valuesSELECT * FROM films WHERE director IS NOT NULL;

And there you have it, folks! We’ve successfully navigated the treacherous waters of SQL filtering. With these tools in your belt, you’re now ready to wrangle data like a pro. Remember, in the world of databases, you’re the director, and these SQL commands are your A-list actors – use them wisely!

Stay curious, keep coding, and remember: in SQL, as in education, every query is a learning opportunity!

Until next time, this is your Principal Developer, signing off!

Posted in Data ScienceTaggs:
Write a comment