Post content
✅SQL Aggregations with Interview Q&A📊🧮 Aggregation functions help summarize large datasets. Combine them with GROUP BY to analyze grouped data. 1️⃣ COUNT() Returns the number of records. SELECT COUNT(*) FROM employees; 2️⃣ SUM() Adds up values in a column. SELECT dept_id, SUM(salary) FROM employees GROUP BY dept_id; 3️⃣ AVG() Returns the average of values. SELECT AVG(salary) FROM employees; 4️⃣ MAX() / MIN() Returns the highest/lowest value. SELECT MAX(salary), MIN(salary) FROM employees; 5️⃣ GROUP BY Groups rows that have the same values in specified columns. SELECT dept_id, COUNT(*) FROM employees GROUP BY dept_id; 6️⃣ HAVING Filters groups after aggregation (unlike WHERE which filters rows). SELECT dept_id, AVG(salary) FROM employees GROUP BY dept_id HAVING AVG(salary) > 50000; ———————— Real-World Interview Questions + Answers Q1: What’s the difference between WHERE and HAVING? A: WHERE filters rows before grouping. HAVING filters after aggregation. Q2: Can you use aggregate functions without GROUP BY? A: Yes. Without GROUP BY, the function applies to the entire table. Q3: How do you find departments with more than 5 employees? SELECT dept_id, COUNT(*) FROM employees GROUP BY dept_id HAVING COUNT(*) > 5; Q4: Can you group by multiple columns? A: Yes. GROUP BY dept_id, job_title Q5: How do you calculate total and average salary per department? SELECT dept_id, SUM(salary), AVG(salary) FROM employees GROUP BY dept_id; 💬Tap ❤️ for more!