Loading lesson...
WHERE: your database bouncer
WHERE: your database bouncer
Topics covered: WHERE clause for filtering rows, Equals and not equals (=, !=), Comparison operators (<, >), IN and NOT IN for list matching, AND for combining conditions
SQL checks each row one by one. For every row, it asks: does this row meet my condition? If yes, keep it. If no, skip it. The SQL code tells the database to scan the status column for each device. When status equals "active", that row passes through to the results. All other rows are filtered out. Basic pattern: Example Here is a simple example filtering students by grade. Remember Filters are built with simple comparison operators: Which query correctly filters for active users? The Equals Oper
Sometimes you need to find everything except a specific value. The not equals operator is the inverse of exact matching. Example Equals and Not Equals Understanding when to use each operator is essential. One finds matches while the other excludes them. Side-by-Side Comparison This same pattern works for any exclusion filter. Here we exclude trial accounts to show only paying customers. Alternative Syntax: <> In SQL, you can write "not equal to" in two ways. Both produce identical results: Under
Numeric and date comparisons let you filter based on relative values rather than exact matches. These operators compare values to find things that are bigger, smaller, or within a range. They work with numbers and dates. Comparison operators: Example: Greater Than Working with Ranges Boundary conditions determine whether the exact threshold value is included or excluded from results. Inclusive vs Exclusive The difference matters when you hit the exact boundary number. Watch how the filter checks
List matching simplifies queries when you need to check against multiple possible values at once. If the column value matches ANY item in the list, the row is kept. Example The "food" row is the only one rejected because it does not appear in the list. Every other type matches at least one entry and passes through to the result. Why IN Is Powerful More Examples Problem: Show sales from three specific categories. Problem: Get data for four specific customer IDs. How does IN handle a longer list o
Example How AND Works A row only appears in results if it passes every single condition. If it fails any one check, it is excluded. Example: • User from Canada, active, logged in recently: EXCLUDED (fails country check) • User from US, inactive, logged in recently: EXCLUDED (fails status check) • User from US, active, logged in 2024: EXCLUDED (fails login date check) • User from US, active, logged in recently: INCLUDED (passes all checks) Practical Applications Which query correctly combines two