10. Filtering records from a table

“Can’t believe we’ve made so much progress with SQL!” said Gessica. “We’ve learned how to select records, sort them, and specify how many records we want as an output. But I still don’t know how much we can really learn with it. Last week I saw a German magazine published an article about us. How can we check how many users signed up from Germany that day?”.

Gessica’s request is a classic example of filtering in SQL. This is where SQL shines and really shows its power – imagine how much effort for an accountant would be to check all transactions which exceed a certain amount of money? It’s crazy 💥

Let’s look at the query which returns all users who signed up from Germany:

SELECT *
FROM users
WHERE
  country = 'de'

As you can see it starts with a query we’ve already learned: select all records from the users table. It reads all records from the users table and prints it as a response.

SELECT *
FROM users

WHERE keyword tells SQL which filters (we can also call them conditions) to apply. In our case, the first query could be translated as “Give me all records from the users table where country exactly equals ‘de’“. We can think about it like so: it’ll go through each record in the users table and gives us only those records which match conditions specified in the WHERE clause.

Inside WHERE clause we can use any column (one or many times), we can combine multiple conditions (not only equals condition = but many more). During the course, we’ll learn so many different filters and their combinations and how they help us answer business questions.

Now let’s switch to the SQL editor and run the queries from this lesson. 🚀

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... ⏳