TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #3091 1.74K
โœ… 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!
  • โค 6
More from @crackingthecodinginterview
  1. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  2. Sep 29, 2026โœ… Daily Coding Habits That Make You a Better Developer ๐Ÿง ๐Ÿ’ปโœจ 1๏ธโƒฃ Code Every Day (Even 30 Mโ€ฆ
  3. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  4. Sep 28, 2026Hereโ€™s a DSA problem-solving cheat sheet that will help you solve 90โ€“95% of questions thatโ€ฆ
  5. Sep 28, 2026๐ŸŽ“ ๐—›๐—”๐—ฅ๐—ฉ๐—”๐—ฅ๐—— ๐—จ๐—ก๐—œ๐—ฉ๐—˜๐—ฅ๐—ฆ๐—œ๐—ง๐—ฌ ๐—™๐—ฅ๐—˜๐—˜ ๐—ข๐—ก๐—Ÿ๐—œ๐—ก๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿ˜ Dreaming ofโ€ฆ
  6. Sep 27, 2026Top 21 skills to learn this year ๐Ÿ‘‡ 1. Artificial Intelligence and Machine Learning: Underโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’