TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun · 77.8K subscribers
Post #4660 326
Used when you want to perform calculations for each department.

Simple rule:

DISTINCT → Give me unique values

GROUP BY → Give me groups so I can calculate something

🔹 Real-World Data Science Example

Imagine an e-commerce dataset.

You want to know the total revenue generated by each product category.

SELECT category,
SUM(quantity * price) AS total_revenue
FROM sales
GROUP BY category
ORDER BY total_revenue DESC;


This gives you a category-level summary that can be used for:

• Revenue analysis

• Product performance

• Business reporting

• Feature creation

• Exploratory data analysis

🔹 Common Mistake

Consider:

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


This can cause an error in many SQL databases because employee_name is neither:

• included in GROUP BY, nor

• used inside an aggregate function.

A valid query would be:

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


🔹 Important Rule

When using GROUP BY, selected columns generally need to be either:

1. Included in the GROUP BY, or

2. Wrapped inside an aggregate function.

Example:

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


department → grouped column

COUNT(*) → aggregate

🔹 Interview Questions

1. What is GROUP BY?

GROUP BY groups rows with the same values so aggregate calculations can be performed for each group.

2. Which aggregate functions are commonly used with GROUP BY?

COUNT(), SUM(), AVG(), MIN(), and MAX().

3. What is the difference between WHERE and GROUP BY?

WHERE filters individual rows before grouping.

GROUP BY creates groups from the filtered rows.

4. Can GROUP BY use multiple columns?

Yes.

GROUP BY department, job_title;

5. Can GROUP BY be used without aggregate functions?

Yes, although DISTINCT is often clearer when the goal is simply to return unique combinations.

🎯 Practice Questions

1. Find the number of employees in each department.

2. Find the average salary for each department.

3. Find the total sales for each region.

4. Find the highest salary in each department.

5. Find the total revenue for each product category and sort categories from highest to lowest revenue.

🎯 Key Takeaways

• GROUP BY creates groups of similar rows.

• It is commonly used with aggregate functions.

• COUNT() counts.

• SUM() totals.

• AVG() calculates averages.

• MIN() finds the minimum.

• MAX() finds the maximum.

• WHERE filters rows before grouping.

• ORDER BY can sort grouped results.

• GROUP BY is one of the most important SQL concepts for Data Science and Analytics.

📍Double Tap ❤️ For More
  • ❤ 2
More from @datasciencefun
  1. Oct 12, 2026🚀 Data Science Roadmap 2026 📍 Phase 3: SQL for Data Science 📖 Topic 6 — GROUP BY GROUP…
  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 →