SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. GROUP BY clause

Text copied!

« Previous
Next »

GROUP BY clause

May 5, 2023, 6:12 a.m. under SQL

  • Hi! You should check WHERE clause post first.

    SQL clause is the specific part of a SQL statement which is used to perform various operations. It can be combined to create more complex queries to retrieve and manipulate data.

    GROUP BY clause :

    GROUP BY clause is used to group and summarize the data based on column(s) and generally combined with aggregate functions such as SUM, AVG, COUNT, MIN and MAX etcetera to calculate the summaries of the grouped data.

    It is often used with other clauses such as 'SELECT', 'FROM', 'HAVING' and 'ORDER BY' etc. to retrieve, specify table, filter, sort and manipulate data.

    The syntax of the GROUP BY clause generally looks like this :
    SELECT column_name(s)
    FROM table_name
    GROUP BY column_name(s);
            

    • Specify the column(s) name after the 'SELECT' keyword.

    • Specify Asterisk (*) symbol to selects all columns from the table after the 'SELECT' keyword.

    • Specify the table name after the 'FROM' keyword.

    • Specify the column name(s) that you want to group and summarize after the 'GROUP BY' keyword.

    Here's an example of how you might use the GROUP BY clause :

    Let's assume we have a table named "[Employees]".

    GROUP BY clause
    Case - 1 :

    Let's assume you want to determine the number of employees by gender to know how many employees are male or female in the [Employees] table.

    Run below SQL statement :

    SELECT Gender, COUNT(Gender) AS [Gender_Count]
    FROM [Employees]
    GROUP BY Gender;
            

    In above statement, we used 'COUNT' function that returns the number of rows. Here, we specified the 'Gender' column in COUNT function.

    'Gender column' is specified in both of the SELECT and GROUP BY clauses that group and summarize the data by gender and 'COUNT' function returns the count of 'Male' and 'Female' employees separately.

    GROUP BY clause

    That's it! You have successfully retrieved the number of employees by gender from [Employees] table.

    Case - 2 :

    Let's assume you want to determine the total salary of employees by gender to know how much male or female employees earns in the [Employees] table.

    Run below SQL statement :

    SELECT Gender, SUM(Salary) AS [Salary_By_Gender]
    FROM [Employees]
    GROUP BY Gender;
            

    In above statement, we used 'SUM' function that calculates the total sum of numeric values. Here, we specified the 'Salary' column in SUM function.

    'Gender column' is specified in both of the SELECT and GROUP BY clauses that group and summarize the data by gender and 'SUM' function calculates the total sum of 'Male' and 'Female' employees salary separately.

    GROUP BY clause

    That's it! You have successfully calculated the sum of employees salary by gender from [Employees] table.

    Case - 3 :

    Let's assume you want to determine the highest salary of employees by gender to know what is the highest salary a male or female employee earns in the [Employees] table.

    Run below SQL statement :

    SELECT Gender, MAX(Salary) AS [Max_Salary_By_Gender]
    FROM Employees
    GROUP BY Gender;
            

    In above statement, we used 'MAX' function that returns the maximum value of a column. Here, we specified the 'Salary' column in MAX function.

    'Gender column' is specified in both of the SELECT and GROUP BY clauses that group and summarize the data by gender and 'MAX' function returns the maximum value of 'Male' and 'Female' employees salary separately.

    GROUP BY clause

    That's it! You have successfully retrieved the maximum value of employees salary by gender from [Employees] table.

    Frequently Asked Questions :

    What is GROUP BY clause with example?

    GROUP BY clause is used in SQL to group rows that have the same values into summary rows, with an example like "SELECT department, AVG(salary) FROM employees GROUP BY department;"

    What is the use of GROUP BY HAVING clause?

    GROUP BY HAVING clause filters grouped rows based on specified conditions, helpful for filtering aggregated data, such as "SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000;"

    What does GROUP BY 1 do?

    GROUP BY 1 refers to grouping by the first column specified in the SELECT statement, such as "SELECT department, AVG(salary) FROM employees GROUP BY 1;"

    What is the difference between clause and GROUP BY clause?

    "Clause" is a general term in SQL referring to syntactical elements like SELECT or WHERE, whereas GROUP BY clause specifically organizes data into groups for aggregation.
    Thank You! You should check Having clause post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy