SQL WHERE COUNT 3: Master Filtering Like a Pro

Ever needed to filter your SQL data to show only those records where a certain condition appears exactly three times? You're in the right place. Today, we're going to explore how to use the WHERE clause in SQL to count and filter rows efficiently. Let's dive right in.

SQL Cheat Sheet in 60 seconds 👇
SQL Cheat Sheet in 60 seconds 👇

Before we start, let's set the scene. Suppose you have an `orders` table with columns `order_id`, `customer_name`, and `product`. You want to find all customers who have ordered exactly three times in the past week. Let's tackle this step by step.

Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts

Understanding `COUNT` in SQL

In SQL, the `COUNT` function is used to count the number of rows that meet a specific criterion. It's a bit different from other aggregate functions like `SUM` or `AVG` because it simply counts the number of rows, not their values.

SQL queries don
SQL queries don

Here's a simple example. Let's find out how many orders we have in total over the past week:

```sql SELECT COUNT(*) FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK); ```

This query will return the total number of orders made in the past week.

SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide

Counting per Group

Now, what if you want to count the number of orders per customer? You'll use `COUNT` with a `GROUP BY` clause. Here's how:

```sql SELECT customer_name, COUNT(*) FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK) GROUP BY customer_name; ```

The result will be a list of customers and the number of orders they made in the past week.

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio

Filtering by Count: `HAVING` Clause

Great! Now let's find the customers who have made exactly three orders. To do this, we'll use the `HAVING` clause, which works like `WHERE` but after aggregate functions:

```sql SELECT customer_name, COUNT(*) FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK) GROUP BY customer_name HAVING COUNT(*) = 3; ```

Wow, that's exactly what we wanted! We've found all customers who have made exactly three orders in the past week.

SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners

Filtering with Subqueries

Remember, in SQL, you can use a subquery to filter data based on another query's results. Let's use this approach to achieve the same result as before:

SQL Query Execution Order Explained | Logical Order of SQL Query Processing
SQL Query Execution Order Explained | Logical Order of SQL Query Processing
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL Cheatsheet In 2026 🚀
SQL Cheatsheet In 2026 🚀
an image of a diagram with the words,'the sq circle'in different languages
an image of a diagram with the words,'the sq circle'in different languages
CQF - SQL
CQF - SQL
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Aggregate Functions Cheat Sheet for Beginners
SQL Aggregate Functions Cheat Sheet for Beginners
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
Learn SQL just by investing 30 minutes a day within 30 Days
Learn SQL just by investing 30 minutes a day within 30 Days
Impress Your Coworkers by Using SQL UNPIVOT!
Impress Your Coworkers by Using SQL UNPIVOT!
a diagram showing how to use the scl chart
a diagram showing how to use the scl chart
#sql #dataanalytics #powerbi #datavisualization #businessintelligence… | Vipin Chandra | 12 comments
#sql #dataanalytics #powerbi #datavisualization #businessintelligence… | Vipin Chandra | 12 comments
a blue and white poster with the names of different types of letters on it's side
a blue and white poster with the names of different types of letters on it's side
a poster with the names and symbols for different types of letters
a poster with the names and symbols for different types of letters
167K views · 467 reactions | Brought to you by programmingvalley  #SQL #database | Programming Valley
167K views · 467 reactions | Brought to you by programmingvalley #SQL #database | Programming Valley
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide

First, we'll write a subquery to count the orders for each customer. Then, we'll use this result to filter the original `orders` table:

```sql SELECT customer_name, order_date FROM orders WHERE (customer_name, Count(order_id)) IN (SELECT customer_name, COUNT(order_id) FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK) GROUP BY customer_name HAVING COUNT(order_id) = 3); ```

Again, we get the desired result.

finesse with Window Functions

Lastly, let's explore a more advanced approach using window functions. Window functions allow us to perform operations across a set of table rows that are related to the current row. Here's how you might solve this problem using a window function:

```sql SELECT customer_name, order_date FROM ( SELECT *, COUNT(*) OVER (PARTITION BY customer_name) as order_count FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK) ) temp_table WHERE order_count = 3; ```

This query gives us the same result, but with a bit more SQL finesse.

And there you have it! You've learned several ways to count and filter rows in SQL to find those cases where a condition appears exactly three times. Happy coding, and until next time!

Related Articles

Mysql W3schools Quiz Mysql W3schools Data Types Sql Merge With Insert Microsoft Sql Server W3 W3 Sql Case Statement Sql W3schools Difference Between Sql And Sql Plus W3schools Mysql Https //www.w3schools.com/sql/try Sql.asp Filename=trysql_select_all Mysql W3schools