Expanding Filters with AND and OR
Precision Filtering with AND
The AND Operator
The AND operator is used when you want to filter rows that satisfy all given conditions. If any single condition is false, the row will not appear.
Syntax Template:
SELECT column1, column2 FROM table_name WHERE condition1 AND condition2;
Welcome back! In this lesson, we will move beyond simple filters to master complex queries using logical operators. Here is our Movie table. Often, you'll need to find data that meets multiple criteria at once, such as a specific genre AND a specific release year. The AND operator ensures that only rows meeting every single condition are returned. Let's see it in action. By filtering for Genre 'Sci-Fi' AND Year greater than 1990, we narrow the list down to only the movies that fit both descriptions perfectly.
- AND narrows your search results.
- Both conditions must be TRUE for a row to be included.
- Useful for combining text and numeric filters.
Practice: Coding with AND
Your Turn
Write a query to find the Title and Year of all movies that are in the 'Sci-Fi' genre AND were released before the year 2000.
Let's put your knowledge into practice. Use the code editor to write a query that finds Sci-Fi movies released before the year 2000. Pay close attention to your column names and single quotes for text values.
- Filter: Genre = 'Sci-Fi'
- Filter: Year < 2000
- Select specific columns: Title, Year
Broadening Results with OR
The OR Operator
The OR operator is used when you want to include rows that satisfy at least one of the conditions. This expands your results rather than narrowing them.
Syntax Template:
SELECT column1, column2 FROM table_name WHERE condition1 OR condition2;
While AND narrows your search, the OR operator expands it. Imagine you're in the mood for either a Sci-Fi movie OR an Action movie. Using OR tells the database to give you rows that match the first condition, the second condition, or both. Watch the table. When we search for Genre 'Sci-Fi' OR 'Action', we get a much larger list because the database includes every movie that fits either category.
- OR expands your search results.
- Only ONE condition needs to be TRUE for a row to be included.
- Useful for searching across multiple categories.
Excluding Data with Inequality
Inequality Operators
Sometimes it is easier to define what you don't want. SQL provides two symbols for 'not equal to': != and <>.
Example:
SELECT Title, RatingCode FROM Movie WHERE RatingCode != 'R';
Sometimes, the easiest way to find what you want is to define what you DON'T want. SQL uses the 'not equal' symbols, either the exclamation mark with an equals sign or the diamond-shaped brackets. For example, if you want to see all movies except those rated 'R', you would use an inequality operator. Notice how the result changes. By saying 'RatingCode is not equal to R', we effectively hide The Godfather from our list while keeping everything else.
- != and <> both mean 'Not Equal To'.
- Useful for filtering out specific values.
- Works for both text and numeric data.
Precedence and Quotes
Common Pitfalls
- Operator Precedence: SQL evaluates AND before OR. Use parentheses
()to group logic. - Quotes: Text values require single quotes (
'Sci-Fi'), but numbers do not (1990).
As your queries get more complex, keep two things in mind. First, SQL is like math—it has an order of operations. It always processes AND before OR. If you want to change that, use parentheses. Second, remember that text values are always wrapped in single quotes, while numbers stand alone.
- AND has higher priority than OR.
- Use parentheses to control order of operations.
- Always quote strings/text.
Final Challenge: The Movie Scout
The Final Challenge
The studio needs a list of movies for a marathon. Write a query to find the Title and Genre of movies that are either 'Drama' OR 'Action', but they must NOT have been released in 1972.
Time for the final challenge! You need to find movies that are Drama or Action, but exclude anything from the year 1972. Think carefully about how to combine your operators. Good luck!
- Combine OR and Inequality.
- Select Title and Genre.
- Use parentheses for logic clarity.