Skip to main content

Command Palette

Search for a command to run...

Practice Exercise: Using WHERE

Published
3 min read
M

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.

With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.

In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.

I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.

Let’s do a quick practice exercise to make sure the idea of WHERE is crystal clear.

Once again, we are working with a table called phones.

Each row in this table represents a different phone and contains information like:

  • phone name

  • manufacturer

  • price

  • units sold


🎯 Goal of This Exercise

Write a SQL query that will:

  • print the name

  • and price

  • of every phone that sold more than 5000 units

In simple words:

We only want phones where units_sold is greater than 5000.


🧠 How to Think About It

Ask yourself these questions:

  1. Which table am I selecting from?
    phones

  2. Which columns do I want to display?
    name, price

  3. What condition should filter the rows?
    units_sold > 5000


✍️ Your Task

Try writing the SQL query yourself before looking at the solution.

Use what you’ve learned:

  • SELECT to choose columns

  • FROM to choose the table

  • WHERE to filter rows

Take a moment and give it a try 👇

(If you get stuck, no stress — we’ll walk through the solution next.)

Solution: Filtering with WHERE

Let’s walk through the solution step by step and make sure everything makes sense.


Step 1: Decide What We’re Doing

We are retrieving data from the database, so we know our query must start with the SELECT keyword.

From the problem statement, we want:

  • the name

  • and the price
    of phones

So we begin with:

select name, price

Step 2: Decide Where the Data Comes From

All of this data lives inside the phones table.

So next, we add:

from phones

At this point, if we stopped here, PostgreSQL would return all phones — but that’s not what we want.


Step 3: Filter the Rows

The requirement was very clear:

Only include phones that sold more than 5000 units

So we use the WHERE clause to filter rows based on the units_sold column.

where units_sold > 5000

✅ Final Query

Putting everything together, the final query looks like this:

select name, price
from phones
where units_sold > 5000;

✅ Result

When we run this query:

  • PostgreSQL first looks at the phones table

  • filters rows where units_sold is greater than 5000

  • and finally returns only the name and price columns

The output shows only the phones that meet the condition — exactly what we wanted.


Key Takeaway

  • SELECT decides which columns

  • FROM decides which table

  • WHERE decides which rows

You’re now officially comfortable filtering rows using WHERE 🎉