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.

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.

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.

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.

Precedence and Quotes

Common Pitfalls

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.

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!