MySQL for Developers

Subqueries in MySQL: a powerful tool for filtering data

In MySQL, subqueries allow you to run a separate query inside your main query, and they can be super powerful in situations where joins might not work exactly as you want them to. In this video, we'll dive into subqueries and explore how they can help you filter data efficiently.

Exploring subqueries in MySQL

Let's take a look at an example to better understand subqueries. Suppose you are working with customer data and payments data, and you need to find a list of customers who have made payments greater than $5.99. To achieve this, you can join the two tables and filter down to customers who have spent more than $5.99. However, after trimming down the data, you might find duplicate customer records in the results. Here is the code to join customer and payment tables:

INNER JOIN payment ON = payment.customer_id
payment.amount > 5.99;

To remove the duplicate records, you can use a DISTINCT statement as follows:

DISTINCT customer.first_name, customer.last_name
INNER JOIN payment ON = payment.customer_id
payment.amount > 5.99;

This method works, but it can be a pain to use DISTINCT, and you might end up creating a lot of data just to remove it. So, how do we solve this problem? This is where subqueries can come in handy.

Using subqueries in MySQL

Instead of a join, we will use a subquery in this case. Here is the code:

SELECT * FROM customer
id IN (
SELECT customer_id FROM payment WHERE amount > 5.99

The subquery returns only the customer IDs who match this criterion of having spent more than 5.99, and the outer query returns all of the corresponding customer data for the IDs that match the inner query.

One advantage of using subqueries is that you don't have to join all the data together and perform a DISTINCT operation after trimming it down. This approach can also improve the overall performance of your queries, as MySQL can use a semi-join optimization technique to process the data more efficiently.

Final thoughts

Subqueries in MySQL are a powerful tool for filtering data efficiently. MySQL can optimize your subqueries using the semi-join and anti-join optimization techniques. It's important to optimize your queries and test them with your dataset to ensure they perform well under various conditions!

If you want to learn more about subqueries in MySQL, check out Chapter 8 on optimizations in the MySQL documentation. This resource is a treasure trove of information that can help you become an expert in MySQL.

About this lesson

Subqueries are a powerful tool for eliminating or including records based on the results of a secondary query. Fortunately MySQL includes optimizations for this very situation!

Closed captioned

Meet your instructor, Aaron Francis

Developer Educator at PlanetScale

I’m a dad to one-year-old twins and a software developer with a keen interest in processes and automation.

Feedback or questions? Reach out to our team at

By submitting your email, you agree to the processing of your personal information by PlanetScale as described in the Privacy Policy.