Roshan Kumar Sharma’s Post

[𝗦𝗤𝗟 𝗖𝗛𝗔𝗟𝗟𝗘𝗡𝗚𝗘 #6]: 𝗧𝗵𝗲 "𝗗𝗮𝘁𝗮 𝗕𝘂𝗰𝗸𝗲𝘁𝗲𝗲𝗿" Raw numbers are great, but for a Finance or Product team, a list of 10,000 transactions is just noise. To find patterns, we need to see the 𝗱𝗶𝘀𝘁𝗿𝗶𝗯𝘂𝘁𝗶𝗼𝗻. Are most of our users spending small amounts, or are we driven by "whales"? Today’s challenge is about building a frequency histogram, a must-have skill for any analyst performing exploratory data analysis (EDA). 𝗧𝗵𝗲 𝗦𝗲𝘁𝘂𝗽 You have a transactions table. The CFO wants a high-level summary of transaction volume across specific price ranges (buckets). Your task is to categorize every transaction and count how many fall into each range. 𝗧𝗵𝗲 𝗦𝗰𝗵𝗲𝗺𝗮: CREATE TABLE transactions (   txn_id INT,    amount NUMERIC(10,2) ); 𝗧𝗵𝗲 𝗠𝗶𝘀𝘀𝗶𝗼𝗻 Write a query that groups transaction amounts into the following four buckets: 1️⃣ 0-100 2️⃣ 101-500 3️⃣ 501-1000 4️⃣ 1000+ 𝗘𝘅𝗽𝗲𝗰𝘁𝗲𝗱 𝗢𝘂𝘁𝗽𝘂𝘁: | 𝗯𝘂𝗰𝗸𝗲𝘁 | 𝗰𝗼𝘂𝗻𝘁 | | 0-100 | 45 | | 101-500 | 123 | | 501-1000 | 67 | | 1000+ | 12 | 𝗧𝗵𝗲 𝗦𝘁𝗿𝗮𝘁𝗲𝗴𝘆 𝗦𝗲𝘀𝘀𝗶𝗼𝗻: There are a few ways to slice this. The most common is using a CASE WHEN statement, but some dialects have specialized functions like WIDTH_BUCKET or floor math tricks. How would you ensure the buckets appear in the correct order (numerical rather than alphabetical)? And how do you handle the upper boundaries to make sure no transaction is counted twice? Drop your code in the comments! Tell us which SQL engine you’re using and your favorite trick for bucketing data. Let’s see those solutions! #SQL #DataAnalysis #DataScience #DataEngineering #PostgreSQL #MySQL #BigQuery #CodingChallenge #Statistics #LearnSQL

To view or add a comment, sign in

Explore content categories