Sitemap
2 min readDec 3, 2025

--

๐ŸŽฏ ๐’๐๐‹ ๐’๐ž๐ซ๐ข๐ž๐ฌ | ๐„๐ฉ. ๐Ÿ“ โ€” ๐…๐ข๐ฅ๐ญ๐ž๐ซ๐ข๐ง๐  & ๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ ๐‹๐จ๐ ๐ข๐œ ๐ฐ๐ข๐ญ๐ก ๐‚๐€๐’๐„ ๐–๐‡๐„๐

Data rarely fits into neat boxes.
Sometimes you need to teach SQL to decide for itself.
Thatโ€™s where ๐‚๐€๐’๐„ ๐–๐‡๐„๐ comes in โ€” your logic gate for turning raw data into decisions.

๐Ÿงฉ ๐‹๐ž๐ญโ€™๐ฌ ๐’๐ž๐ž ๐ˆ๐ญ ๐ข๐ง ๐€๐œ๐ญ๐ข๐จ๐ง:
๐„๐ฑ๐š๐ฆ๐ฉ๐ฅ๐ž: ๐‚๐จ๐ฆ๐ฉ๐š๐ซ๐ข๐ง๐  ๐๐š๐ฒ๐ข๐ง๐  ๐ฏ๐ฌ. ๐๐จ๐ง-๐๐š๐ฒ๐ข๐ง๐  ๐”๐ฌ๐ž๐ซ๐ฌ
๐Ÿ”— ๐๐ซ๐ž๐ฆ๐ข๐ฎ๐ฆ ๐ฏ๐ฌ ๐…๐ซ๐ž๐ž๐ฆ๐ข๐ฎ๐ฆ โ€” https://lnkd.in/e2_5mUpr

๐Ÿ“Š ๐๐ฎ๐ฌ๐ข๐ง๐ž๐ฌ๐ฌ ๐๐ฎ๐ž๐ฌ๐ญ๐ข๐จ๐ง:
Find the total number of downloads for paying and non-paying users by date โ€” but only show days when non-paying users had more downloads than paying users.

๐Ÿง  ๐’๐ญ๐ž๐ฉ ๐Ÿ: ๐€๐ฌ๐ค ๐ญ๐ก๐ž ๐ซ๐ข๐ ๐ก๐ญ ๐๐š๐ญ๐š ๐ช๐ฎ๐ž๐ฌ๐ญ๐ข๐จ๐ง:
1๏ธโƒฃ What defines a paying user?
2๏ธโƒฃ How do I calculate downloads by type?
3๏ธโƒฃ How do I filter only when non-paying > paying?

๐Ÿ’ป ๐’๐ญ๐ž๐ฉ ๐Ÿ: ๐“๐ซ๐š๐ง๐ฌ๐ฅ๐š๐ญ๐ž ๐ข๐ง๐ญ๐จ ๐’๐๐‹ ๐ฅ๐จ๐ ๐ข๐œ

select
date as download_date,
sum(case when paying_customer=โ€™noโ€™ then downloads else 0 end) as non_paying,
sum(case when paying_customer=โ€™yesโ€™ then downloads else 0 end) as paying
from
download_status ds group by download_date
having
sum(case when paying_customer=โ€™noโ€™ then downloads else 0 end) >
sum(case when paying_customer=โ€™yesโ€™ then downloads else 0 end)
order by download_date asc;

Get Somyaguptaโ€™s stories inย yourย inbox

Join Medium for free to get updates fromย thisย writer.

๐Ÿงฉ ๐–๐ก๐š๐ญโ€™๐ฌ ๐ก๐š๐ฉ๐ฉ๐ž๐ง๐ข๐ง๐  ๐ก๐ž๐ซ๐ž?
๐Ÿ‘‰ ๐‚๐€๐’๐„ ๐–๐‡๐„๐ classifies each download by payment type.
๐Ÿ‘‰ ๐’๐”๐Œ aggregates total downloads for both.
๐Ÿ‘‰ ๐‡๐€๐•๐ˆ๐๐† filters only dates where non-paying > paying.
๐Ÿ‘‰ ๐Ž๐‘๐ƒ๐„๐‘ ๐๐˜ shows trends over time.

๐Ÿ’ก This query doesnโ€™t just retrieve data โ€” it tells a story about user behavior.

๐Ÿง  ๐“๐ซ๐ฒ ๐ˆ๐ญ ๐˜๐จ๐ฎ๐ซ๐ฌ๐ž๐ฅ๐Ÿ:
If you liked this logic, try applying CASE WHEN in these problems:

1๏ธโƒฃ ๐Œ๐ž๐ญ๐š/๐…๐š๐œ๐ž๐›๐จ๐จ๐ค ๐€๐œ๐œ๐จ๐ฎ๐ง๐ญ๐ฌ โ€” https://lnkd.in/e2XibwGW
Calculate the ratio of โ€˜closedโ€™ accounts among all accounts with status records on January 10, 2020.
๐‡๐ข๐ง๐ญ: Use CASE WHEN status = โ€˜closedโ€™ THEN 1 ELSE 0 END inside an aggregation to calculate ratios.

2๏ธโƒฃ ๐€๐ฉ๐ฉ๐ฅ๐ž ๐๐ซ๐จ๐๐ฎ๐œ๐ญ ๐‚๐จ๐ฎ๐ง๐ญ๐ฌ โ€” https://lnkd.in/e2cN2fDe
Measure the popularity of Apple devices (macbook pro, iphone 5s, ipad air) across languages.
๐‡๐ข๐ง๐ญ: Use CASE WHEN device IN (โ€ฆ) THEN user_id END to flag Apple users before counting distinct users per language.

๐Ÿงญ ๐“๐š๐ค๐ž๐š๐ฐ๐š๐ฒ:
๐‚๐€๐’๐„ ๐–๐‡๐„๐ is where SQL starts thinking like an analyst.
Itโ€™s not just filtering โ€” itโ€™s reasoning with data.

๐Ÿ’ฌ Whatโ€™s one tricky conditional query youโ€™ve solved recently? Share below โ€” letโ€™s discuss how to optimize it!

๐Ÿ”ฅ ๐‚๐จ๐ฆ๐ข๐ง๐  ๐ฎ๐ฉ ๐ง๐ž๐ฑ๐ญ: ๐„๐ฉ. ๐Ÿ” โ€” ๐‚๐Ž๐”๐๐“, ๐’๐”๐Œ, ๐€๐•๐†, ๐Œ๐ˆ๐, ๐Œ๐€๐— โ€” ๐Œ๐ž๐š๐ฌ๐ฎ๐ซ๐ข๐ง๐  ๐๐ฎ๐ฌ๐ข๐ง๐ž๐ฌ๐ฌ ๐Œ๐ž๐ญ๐ซ๐ข๐œ๐ฌ

--

--