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.

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.

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.

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.

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.

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.

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:



















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!