SQL Practice Logo

SQLPractice Online

COUNT, SUM, AVG, MIN, MAX: Interview

Module: Aggregate Functions & Grouping

What is the fundamental difference between COUNT(*) and COUNT(column)?

COUNT(*) counts all rows including those with NULL values, while COUNT(column) counts only non-NULL values in that specific column.

Write a query to find the top 3 customers by total spending.

SELECT

customer_id,

COUNT(*) AS order_count,

SUM(total_amount) AS total_spending

FROM orders

GROUP BY customer_id

ORDER BY total_spending DESC

LIMIT 3;

Groups orders by customer, calculates count and sum per customer, sorts by spending descending and limits to top 3.