TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2521 2.44K
โœ… 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!
  • โค 4
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
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 โ†’