Skip to content

Day 50: Using the IN operator

Utilizing the IN Operator in SQL: Checking Values Within a Specific Range

The IN operator in SQL is a useful tool for checking if values fall within a specific range. For instance, if you want to find books with ratings between 3 and 4, you can use the IN operator.

In this context, we use the SELECT keyword followed by an asterisk (*), which means we're selecting all columns from the books table. We then specify that we're looking for rows where the rating is within the range of 3 and 4. In other words, we're looking for books with ratings between 3 and 4.

Checking Values Within a Range with SQL

Here's how you can find books with ratings between 3 and 4 using SQL and Python:

import sqlite3

# Connect to the bookstore.db database
conn = sqlite3.connect("bookstore.db")

# Create a cursor to execute SQL commands
cursor = conn.cursor()

# Get books within a range of ratings
cursor.execute("""
SELECT *
FROM books
WHERE rating IN (3, 4)
""")

result = cursor.fetchone()

# Close the database connection
conn.close()

In this Python script, we connect to the bookstore.db database and create a cursor to execute SQL commands. We then use the IN operator to find books with ratings between 3 and 4. The result is fetched and finally, we close the database connection.

By using the IN operator, we can efficiently check if values fall within a specific range in our database, enhancing our data analysis capabilities and improving the readability of our code.

Learn More

Want to learn more about SQL for Machine Learning? Check out the full course HERE.


Need help mastering Machine Learning?

Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.