Aggregation and Reports
Aggregation turns rows into answers: totals, counts, averages, grouped reports, and anomaly checks.
Aggregate Functions
SELECT
COUNT(*) AS total_orders,
SUM(total_amount) AS revenue,
AVG(total_amount) AS average_order_value
FROM orders
WHERE status = 'PAID';
GROUP BY
Use GROUP BY when you need one result per category.
SELECT status, COUNT(*) AS orders
FROM orders
GROUP BY status
ORDER BY orders DESC;
HAVING
WHERE filters rows before grouping. HAVING filters groups after aggregation.
SELECT customer_id, COUNT(*) AS failed_payments
FROM payments
WHERE status = 'FAILED'
GROUP BY customer_id
HAVING COUNT(*) >= 3;
QA Use Cases
- Compare UI dashboard numbers with database totals.
- Find duplicate records with grouped counts.
- Validate batch jobs, invoices, reports, and API summaries.