5 Essential SQL Queries for Data Analysts

Day 7/30 — 5 SQL queries I use every single day as a Data Analyst. Not textbook queries. The ones I actually run. On real data. Every week. Save this. You'll need it. 🔖 1️⃣ Finding duplicates instantly sql SELECT customer_id, COUNT(*) as count FROM orders GROUP BY customer_id HAVING COUNT(*) > 1 ORDER BY count DESC; Before building ANY dashboard — this is query #1. Duplicates in source data = wrong KPIs. Always check. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2️⃣ Running totals with window functions sql SELECT order_date, revenue, SUM(revenue) OVER (ORDER BY order_date) AS running_total FROM sales; No subquery. No JOIN. One clean line. Stakeholders love a running total visual. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3️⃣ Month-over-month comparison sql SELECT month, revenue, LAG(revenue) OVER (ORDER BY month) AS prev_month, ROUND((revenue - LAG(revenue) OVER (ORDER BY month)) / LAG(revenue) OVER (ORDER BY month) * 100, 2) AS MoM_growth FROM monthly_sales; This one query powers half my Power BI KPI cards. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4️⃣ Replacing NULLs without breaking aggregations sql SELECT COALESCE(region, 'Unknown') AS region, COALESCE(revenue, 0) AS revenue FROM sales_data; NULL values silently break SUM and AVG. COALESCE is your safety net. Use it everywhere. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5️⃣ Top N per category sql SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY region ORDER BY revenue DESC) AS rank FROM sales ) ranked WHERE rank <= 3; Top 3 products per region. Top 5 customers per segment. This pattern works for everything. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ These 5 queries cover 80% of real analyst work. Master these before memorising anything else. 💪 Day 7 of my #7DaysOfData series. One practical insight every day — follow along 🔔 💬 Which of these do you use most? Or is there a query you'd add to this list? Drop it below 👇 #30DaysOfData #SQL #DataAnalytics #DataAnalyst #SQLtips #DataEngineering #WindowFunctions #BIwithPankhuri

To view or add a comment, sign in

Explore content categories