SQL Lesson 2: Queries with constraints

April 3, 2025 | Categories: Information

In the previous lesson, we explored how to retrieve data using the SELECT statement. Now it’s time to make our queries smarter by filtering the data to return only the rows we’re interested in. This is where the WHERE clause comes into play.

Imagine you’re working with a large dataset of Oscar-winning movies. Instead of scrolling through every single film, wouldn’t it be nice to ask questions like:

All of these questions can be answered using WHERE.

🔎 Filtering Rows with WHERE

The WHERE clause is used to apply conditions to your query results. SQL checks each row to see if it matches the condition(s)—and only includes the ones that do.

Basic format:

SELECT column1, column2
FROM oscar_movies
WHERE condition;

Common Comparison Operators

Operator Description Example
= Equal to director = 'James Cameron'
!= Not equal to year != 1994
< Less than year < 2010
> Greater than rating > 8.5
<= Less than or equal to length_minutes <= 120
>= Greater than or equal to year >= 2000

You can use comparison operators such as =, !=, <, >, <=, and >= to define the condition. For example, you could filter for movies directed by a specific person, or movies released after a certain year.

🔗 Combining Conditions: AND / OR

To filter based on more than one condition, you can use logical operators like AND and OR. For instance, you might want to return movies that were released after the year 1999 and have a runtime longer than 100 minutes. Or, you might look for movies released in either 1997 or 2009. Combining conditions gives you more control over the data you retrieve.

You can combine multiple conditions using AND and OR:

SELECT column, another_column
FROM oscar_movies
WHERE condition AND/OR another_condition
;

🎯 Working with Ranges: BETWEEN / NOT BETWEEN

If you're checking whether a value falls within a certain range—like a span of years—you can use the BETWEEN operator. It’s inclusive of both the start and end values.

To get the opposite effect, you can use NOT BETWEEN.

Operator Condition SQL Example
=, !=, <, <=, >, >= Standard numerical operators col_name != 4
BETWEEN … AND … Number is within range of two values (inclusive) col_name BETWEEN 1.5 AND 10.5
NOT BETWEEN … AND … Number is not within range of two values (inclusive) col_name NOT BETWEEN 1 AND 10
IN (…) Number exists in a list col_name IN (2, 4, 6)
NOT IN (…) Number does not exist in a list col_name NOT IN (1, 3, 5)

 

To check if a value falls within a range:

SELECT column, another_column
FROM oscar_movies
WHERE condition BETWEEN another_condition AND another_condition
;

📋 Using IN for Lists

When you're checking for multiple specific values in a column (such as multiple genres or directors), use the IN keyword with a comma-separated list. This is cleaner and easier than writing multiple OR conditions.

NOT IN works the same way, but excludes the listed values.


💡 Tip:

Even though SQL isn’t case-sensitive, it’s a best practice to capitalize SQL keywords like SELECT, FROM, WHERE, and AND. It helps make your queries easier to read and understand.


✅ Key Takeaways


📝 Exercise Overview

In this lesson’s exercises, you’ll be working with the oscar_movies table to practice applying constraints with the WHERE clause.

You'll get hands-on practice with:

These tasks are designed to help you build confidence writing queries with real-world conditions. Try variations, tweak the values, and see how the output changes. The more you experiment, the better your SQL instincts will become.

Leave a comment:

Comments: