Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is the use of GROUP by statement in MySQL?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 486
    Comment on it

    The GROUP by statement is used with the aggregate functions to group the result by one or more columns.

    GROUP by Syntax

    SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP by column_name;

    We have a table "employee" as below:

        employee
        id  first_name            last_name       salary
        .......................................................
        1   John                     Simp         10000
        2   Chris                    Hely         25000
        3   John                     Roy          20000
        4   Jenny                    Mill         35000
        5   Jenny                    Simpson      35000
    
    

    GROUP by Example

    Now we want to find occurrence of first name value in column "first_name" then you just need to write the below command:

    SELECT first_name, COUNT(fistr_name) as count
    FROM employee 
    GROUP by first_name;
    

    Result:

        first_name            count
        .............................
        John                  2
        Chris                 1
        Jenny                 2
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: