18. Combining filters

So far we’ve learned how to filter signups by country (signups from the US):

SELECT COUNT(*)
FROM users
WHERE
  country = 'us'

and by date (signups after 1, Jan 2018):

SELECT COUNT(*)
FROM users
WHERE
  signup_date >= '2018-01-01'

What if we want to combine both and count how many users signed up after 1, Jan 2018 in the US)? This is where SQL’s AND operator comes in handy:

SELECT COUNT(*)
FROM users
WHERE
  country = 'us'
  AND signup_date >= '2018-01-01'

To combine filters we just need to put AND between them. In the same way, we can combine more than two filters and answer questions like “How many customers from the US signed up after a certain date?”:

SELECT COUNT(*)
FROM users
WHERE
  country = 'us'
  AND signup_date >= '2018-01-01'
  AND status = 'customer'

It’s a common practice in Database Architecture to use a specific string column to represent a user’s state. In Bindle’s dataset state column has a name status (don’t ask why not just state 😄, in early days startups don’t have analytics departments so there’s no one who really controls how tables and columns will be named). The status column could have one of these values (there will be more, but let’s focus only on these for now):

  • free – a user who has signed up and hasn’t purchased a subscription yet
  • customer – a user who purchased a subscription

Wow, you’re moving so fast! It’s just the beginning of the course and you already can make complex filters and count records. 🚀

Anatoli Makarevich, author of SQL Habit About SQL Habit

Hi, it’s Anatoli, the author of SQL Habit. 👋

SQL Habit is a course (or, as some of the students say, “business simulator”). It’s based on a story of a fictional startup called Bindle. You’ll play a role of their Data Analyst 📊 and solve real-life challenges from Business, Marketing, and Product Management.

SQL Habit course is made of bite-sized lessons (you’re looking at one atm) and exercises. They always have a real-life setting and detailed explanations. You can immediately apply everything you’ve learned at work. 🚀

“well worth the money”

Fluent in SQL in a month

Master Data Analysis with SQL with real life examples from Product Management, Marketing, Finance and more.
-- Type your query here, for example this one -- lists all records from users table: SELECT * FROM users
LIMIT 500
Loading chart... ⏳