๐ฏ ๐๐๐ ๐๐๐ซ๐ข๐๐ฌ | ๐๐ฉ. ๐ โ ๐ ๐ข๐ฅ๐ญ๐๐ซ๐ข๐ง๐ & ๐๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐๐ฅ ๐๐จ๐ ๐ข๐ ๐ฐ๐ข๐ญ๐ก ๐๐๐๐ ๐๐๐๐
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!
๐ฅ ๐๐จ๐ฆ๐ข๐ง๐ ๐ฎ๐ฉ ๐ง๐๐ฑ๐ญ: ๐๐ฉ. ๐ โ ๐๐๐๐๐, ๐๐๐, ๐๐๐, ๐๐๐, ๐๐๐ โ ๐๐๐๐ฌ๐ฎ๐ซ๐ข๐ง๐ ๐๐ฎ๐ฌ๐ข๐ง๐๐ฌ๐ฌ ๐๐๐ญ๐ซ๐ข๐๐ฌ






