Count unique values and compare counts
COUNT(*) counts rows, COUNT(col) skips NULL, COUNT(DISTINCT col) counts different values.
SQL · Count unique values and compare countsIntermediate
SELECT COUNT(*) AS rows_total,
COUNT(city) AS with_city,
COUNT(DISTINCT city) AS cities
FROM customers;
SELECT customer_id, COUNT(DISTINCT status) AS different_statuses
FROM orders
GROUP BY customer_id
HAVING COUNT(DISTINCT status) > 1;count distinctuniquereport