A customer report has a neat number in each row. It also silently drops two customers. The join is the first place to look. Decide what one row means before giving the query a permanent name.
Give the view a grain
Imagine a training database with customers and orders. Your page needs one record for every customer and the number of their closed orders. Keep the customer as the left side of the join. Put the order-state condition with the relationship that selects qualifying orders.
CREATE VIEW customer_closed_orders AS
SELECT
c.id AS customer_id,
COUNT(o.id) AS closed_order_count
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
AND o.status = 'closed'
GROUP BY c.id;
This example assumes there is one row per customer ID and per order ID. A customer with zero qualifying orders still has one result row. The order ID is NULL in the unmatched row, so COUNT(o.id) yields zero. A COUNT(*) would count the unmatched row itself. The MySQL CREATE VIEW reference describes how a saved view definition is created and used.
Keep two cases in the fixture
Try three customers in a scratch database. Give the first customer two closed orders and one open order. Give the second an open order only. Leave the third without any orders. The expected numbers are two, zero and zero, in customer-ID order when you sort the query explicitly.
Move the status condition into a WHERE clause in a separate test query. The unmatched cases disappear because there is no matching order status for the NULL rows. That is a different question worth naming separately. If you add a join to order items later, repeat the fixture and add an order containing several items. An extra relationship can multiply rows before the count.
Save the fixture and the one-line definition of a result row beside any report that relies on this view. The name stays meaningful when someone has to change the query next month.
