TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2725 6.72K
๐Ÿ”ฅ Letโ€™s move to the next topic in the SQL Roadmap:

โœ… GROUP BY & Aggregation Functions

๐Ÿง  1. What is GROUP BY?

GROUP BY is used to group rows with same values

๐Ÿ‘‰ It helps you summarize data

๐Ÿ’ก Example Table: employees
name department salary
Amit IT 60000
Neha HR 40000
Ravi IT 70000
Sara HR 50000

๐Ÿ‘‰ Without GROUP BY

SELECT AVG(salary) FROM employees;

โœ” Gives overall average

๐Ÿ‘‰ With GROUP BY

SELECT department, AVG(salary)
FROM employees
GROUP BY department;

โœ” Gives average salary per department

โšก 2. Aggregation Functions

These functions perform calculations on data

๐Ÿ”น COUNT() โ†’ number of rows

SELECT COUNT() FROM employees;

๐Ÿ”น SUM() โ†’ total

SELECT SUM(salary) FROM employees;

๐Ÿ”น AVG() โ†’ average

SELECT AVG(salary) FROM employees;

๐Ÿ”น MIN() โ†’ smallest value

SELECT MIN(salary) FROM employees;

๐Ÿ”น MAX() โ†’ largest value

SELECT MAX(salary) FROM employees;

๐ŸŽฏ 3. GROUP BY + Aggregation

๐Ÿ‘‰ Count employees in each department

SELECT department, COUNT()
FROM employees
GROUP BY department;

๐Ÿ‘‰ Total salary per department

SELECT department, SUM(salary)
FROM employees
GROUP BY department;

๐Ÿ‘‰ Highest salary per department

SELECT department, MAX(salary)
FROM employees
GROUP BY department;

๐Ÿšจ 4. Important Rule (Interview Favorite)

๐Ÿ‘‰ Every column in SELECT must be:
- Either inside GROUP BY
- Or used with aggregation function

โŒ Wrong:

SELECT name, AVG(salary) FROM employees;

โœ… Correct:

SELECT department, AVG(salary)
FROM employees
GROUP BY department;

๐ŸŽฏ 5. Practice Tasks
1. Count total employees
2. Find total salary of all employees
3. Find average salary per department
4. Find maximum salary in each department
5. Count employees in each department

โœ… Practice Task Solution

โœ… 1. Count total employees

SELECT COUNT() FROM employees;


โœ… 2. Find total salary of all employees

SELECT SUM(salary) FROM employees;


โœ… 3. Find average salary per department

SELECT department, AVG(salary)
FROM employees
GROUP BY department;


โœ… 4. Find maximum salary in each department

SELECT department, MAX(salary)
FROM employees
GROUP BY department;


โœ… 5. Count employees in each department

SELECT department, COUNT()
FROM employees
GROUP BY department;

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Find department with highest average salary

โšก Mini Challenge Solution ๐Ÿ”ฅ

SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC
LIMIT 1;

โšก Double Tap โค๏ธ For More
  • โค 25
More from @sqlspecialist
  1. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  2. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  4. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  5. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
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 โ†’