TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4659 246
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“ Phase 3: SQL for Data Science

๐Ÿ“– Topic 6 โ€” GROUP BY

GROUP BY is used to group rows that have the same value in one or more columns.

It is mainly used when you want to perform calculations for each group.

For example:

โ€ข Total sales by region

โ€ข Average salary by department

โ€ข Number of customers by city

โ€ข Total orders by product

โ€ข Maximum transaction by category

๐Ÿ”น Basic Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;


Common aggregate functions:

โ€ข COUNT() โ†’ Count rows

โ€ข SUM() โ†’ Calculate total

โ€ข AVG() โ†’ Calculate average

โ€ข MIN() โ†’ Find minimum

โ€ข MAX() โ†’ Find maximum

๐Ÿ”น Simple Example

Suppose we have:

department   salary
IT 60000
IT 80000
HR 50000
HR 70000
Finance 90000


Query:

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


Result:

department   avg_salary
IT 70000
HR 60000
Finance 90000


Instead of calculating one overall average, SQL calculates an average for each department.

๐Ÿ”น GROUP BY with COUNT()

To count employees in each department:

SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;


Result:

department   employee_count
IT 2
HR 2
Finance 1


๐Ÿ”น GROUP BY with SUM()

Suppose you have sales data:

region   sales
North 10000
North 15000
South 20000
South 12000


Query:

SELECT region, SUM(sales) AS total_sales
FROM sales
GROUP BY region;


Result:

North   25000
South 32000


This is extremely common in business analytics.

๐Ÿ”น GROUP BY with MAX()

Find the highest salary in each department:

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


๐Ÿ”น GROUP BY with MIN()

Find the lowest salary in each department:

SELECT department, MIN(salary) AS lowest_salary
FROM employees
GROUP BY department;


๐Ÿ”น GROUP BY with Multiple Columns

You can group by multiple columns.

SELECT department, job_title, COUNT(*) AS employee_count
FROM employees
GROUP BY department, job_title;


Here SQL creates groups based on:

department + job_title

For example:

IT        Analyst       5
IT Developer 8
HR Analyst 3
Finance Manager 4


๐Ÿ”น GROUP BY with WHERE

WHERE filters rows before grouping.

SELECT department, AVG(salary) AS avg_salary
FROM employees
WHERE salary > 50000
GROUP BY department;


๐Ÿ”น GROUP BY with ORDER BY

You can sort grouped results.

For example, find departments with the highest total salary first:

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department
ORDER BY total_salary DESC;


This is very useful for ranking groups.

๐Ÿ”น GROUP BY vs DISTINCT

These two can sometimes look similar.

DISTINCT

SELECT DISTINCT department
FROM employees;


Used to find unique departments.

GROUP BY

SELECT department, COUNT(*)
FROM employees
GROUP BY department;
  • โค 3
More from @datasciencefun
  1. Oct 12, 2026Used when you want to perform calculations for each department. Simple rule: DISTINCT โ†’ Giโ€ฆ
  2. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  3. Oct 9, 2026Post #4657
  4. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  5. Oct 8, 2026๐Ÿ”ฌ Kandinsky 6.0 Video: Training Details and Architecture The new Kandinsky 6.0 Video lineโ€ฆ
  6. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
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 โ†’