SQL LESSON 11 OF 32

GROUP BY: one summary per group

GROUP BY combines rows with equal grouping values. Each group gets its own aggregate. Include grouped columns or aggregate expressions in your SELECT for portable, predictable SQL.

हिन्दी में समझें

GROUP BY समान मानों की पंक्तियाँ मिलाता है। हर समूह का सार अलग निकलता है। SELECT में समूह के कॉलम या एग्रीगेट ही रखें।

अभ्यास: department और AVG(salary) को avg_salary: विभाग अनुसार समूह और क्रम।

Hinglish mein samjhein

GROUP BY same values ki rows group karta hai. Har group ka aggregate alag milta hai. SELECT mein grouped columns ya aggregates rakho.

Practice: department aur AVG(salary) AS avg_salary, department se group aur sort karo.

A small SQL example

SELECT category, COUNT(*) AS total FROM products GROUP BY category ORDER BY category;

Examples use the fictional shop database bundled with the SQL playground: customers, products, orders, order_items and employees. The engine is SQLite; other SQL dialects may differ.

Try this example in the SQL playground →

Practise this idea

Show department and AVG(salary) as avg_salary, grouped and ordered by department.

Check answer & explanation
SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department ORDER BY department;

GROUP BY department creates one group per department. AVG(salary) calculates an independent average for each group.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

GROUP BY creates…

  1. One summary per group
  2. One new database
  3. Only one result always
Check the quiz answer

One summary per group

GROUP BY combines rows with equal grouping values. Each group gets its own aggregate. Include grouped columns or aggregate expressions in your SELECT for portable, predictable SQL.

References & corrections

Use the official SQLite documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.