๐ฏ๐๐๐ ๐๐๐ซ๐ข๐๐ฌ | ๐๐ฉ. ๐ โ ๐๐๐๐๐ ๐๐ & ๐๐๐๐๐๐๐๐: ๐๐ง๐๐๐ซ๐ฌ๐ญ๐๐ง๐๐ข๐ง๐ ๐๐ง๐ข๐ช๐ฎ๐๐ง๐๐ฌ๐ฌ ๐ข๐ง ๐๐๐ญ๐
๐๐ก๐ฒ ๐๐จ๐ฎ๐ง๐ญ๐ข๐ง๐ ๐จ๐ง๐๐ ๐ฏ๐ฌ ๐ฆ๐๐ง๐ฒ ๐ญ๐ข๐ฆ๐๐ฌ ๐ฆ๐๐ญ๐ญ๐๐ซ๐ฌ ๐ฆ๐จ๐ซ๐ ๐ญ๐ก๐๐ง ๐ฒ๐จ๐ฎ ๐ญ๐ก๐ข๐ง๐ค
๐ง ๐๐จ๐จ๐ค:
Imagine youโre analyzing the ๐๐ฅ๐ฒ๐ฆ๐ฉ๐ข๐ ๐๐๐ฆ๐๐ฌ dataset.
You want to find ๐ฐ๐ก๐ข๐๐ก ๐๐ฅ๐ฒ๐ฆ๐ฉ๐ข๐ ๐๐๐ข๐ญ๐ข๐จ๐ง ๐ก๐๐ ๐ญ๐ก๐ ๐ก๐ข๐ ๐ก๐๐ฌ๐ญ ๐ง๐ฎ๐ฆ๐๐๐ซ ๐จ๐ ๐ฎ๐ง๐ข๐ช๐ฎ๐ ๐๐ญ๐ก๐ฅ๐๐ญ๐๐ฌ.
If you used ๐๐๐๐๐(*), youโd count every row (including duplicates).
But what if the same athlete appears more than once in different events?
Thatโs where understanding uniqueness in SQL becomes critical.
๐ก ๐๐จ๐ง๐๐๐ฉ๐ญ ๐๐ซ๐๐๐ค๐๐จ๐ฐ๐ง:
1๏ธโฃ ๐๐๐๐๐๐๐๐ helps you capture one-of-each: unique values or combinations.
2๏ธโฃ ๐๐๐๐๐ ๐๐ helps you aggregate by categories: one row per group, summarized values.
3๏ธโฃ When you combine them: you get metrics like โnumber of unique athletes per Olympic editionโ.
๐๐ฌ๐ ๐๐๐ฌ๐ ๐ซ๐ฎ๐ฅ๐ ๐จ๐ ๐ญ๐ก๐ฎ๐ฆ๐:
โก๏ธ Use ๐๐๐๐๐๐๐๐ when you care about how many unique entities.
โก๏ธ Use ๐๐๐๐๐ ๐๐ when you care about how many / how much per category.
๐งฉ ๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐ ๐๐ซ๐จ๐ฆ ๐๐ญ๐ซ๐๐ญ๐๐๐๐ซ๐๐ญ๐๐ก:
๐ ๐๐๐ซ๐ ๐๐ฌ๐ญ ๐๐ฅ๐ฒ๐ฆ๐ฉ๐ข๐๐ฌ: https://lnkd.in/e_tZYS6W
๐๐ฎ๐ฌ๐ข๐ง๐๐ฌ๐ฌ ๐ช๐ฎ๐๐ฌ๐ญ๐ข๐จ๐ง:
โFind the Olympics edition (year + season) with the highest number of unique athletes.โ
Get Somyaguptaโs stories inย yourย inbox
Join Medium for free to get updates fromย thisย writer.
๐๐จ๐ฅ๐ฎ๐ญ๐ข๐จ๐ง ๐ฌ๐ค๐๐ญ๐๐ก:
SELECT
games,
COUNT(DISTINCT id) AS athlete_count
FROM olympics_athletes_events
GROUP BY games
ORDER BY athlete_count DESC
LIMIT 1;
โก๏ธ ๐๐๐๐๐(๐๐๐๐๐๐๐๐ ๐ข๐) ensures each athlete is counted once per โgamesโ.
โก๏ธ ๐๐๐๐๐ ๐๐ games allows us to summarise by each edition.
โก๏ธ ๐๐๐๐๐ ๐๐ โฆ DESC picks the edition with most unique athletes.
๐ ๐๐ก๐ฒ ๐ญ๐ก๐ข๐ฌ ๐ฆ๐๐ญ๐ญ๐๐ซ๐ฌ:
By properly combining ๐๐๐๐๐๐๐๐ and ๐๐๐๐๐ ๐๐, you avoid double-counting and get correct metrics.
๐
๐จ๐ซ ๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐:
1. How many distinct customers bought each category?
2. Which quarter had the most unique new users?
3. Which product was launched by most distinct companies?
These are common real-world questions โ and if you answer them incorrectly, youโll mislead business decisions.
๐งฉ ๐๐ซ๐ฒ ๐ญ๐ก๐๐ฌ๐ ๐ฉ๐ซ๐จ๐๐ฅ๐๐ฆ๐ฌ:
1๏ธโฃ Marketing Campaign Success โ Advanced โ segment performance, unique users per campaign.
2๏ธโฃ Actor Rating Difference Analysis โ unique actors, movies, ratings across time.
Have you ever faced a metric that seemed โsimpleโ but produced wrong results because of duplicates or missing grouping?
Share your story โ letโs learn from each other. ๐
๐ ๐๐จ๐ฆ๐ข๐ง๐ ๐๐๐ฑ๐ญ:
๐๐๐ ๐๐๐ซ๐ข๐๐ฌ | ๐๐ฉ. ๐ โ ๐๐๐๐๐๐: ๐
๐ข๐ฅ๐ญ๐๐ซ๐ข๐ง๐ ๐๐ซ๐จ๐ฎ๐ฉ๐ฌ, ๐๐จ๐ญ ๐๐จ๐ฐ๐ฌ






