Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

Module 6: DB2 SQL Functions, Joins and Subqueries


GROUP BY

What is GROUP BY

  • GROUP BY groups rows that have the same column value into one group.
  • Each group then produces exactly one row in the result table.
  • It is always used together with aggregate functions like SUM, AVG, COUNT, MIN and MAX.
  • Grouping happens after the WHERE clause has filtered the rows.

Rules of GROUP BY

  • Every column in the SELECT list that is not inside an aggregate function must appear in the GROUP BY clause.
  • NULL values form their own single group.
  • The result order is not guaranteed. Add ORDER BY if sorted output is needed.
  • Example:-
    -- Total salary per domain SELECT Domain, SUM(Sal) FROM Employee GROUP BY Domain; -- Number of employees in each grade, sorted by grade SELECT Grade, COUNT(*) FROM Employee GROUP BY Grade ORDER BY Grade;





© copyright mainframebug.com
Privacy Policy
MainframeBug Assistant