Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • GroupBy and OrderBy in MYSQL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 379
    Comment on it
    1. ORDERBY alters the order of the specified column into the table by giving there fields name and arrange them in desc as well as asc ordering but default ordering is asc while in GROUPBY aggregates the records by the specified column which allow to perform the aggregation on non-grouped columns.

    2. Examples of ORDERBY and GROUPBY clause:

      Consider a TABLE name emp having fields Id and Name:

    Table name emp:

    ID NAME
     1  sam
     2  John
     3  ken
     4  sam
    

    First Query:

     SELECT * FROM emp ORDER BY NAME
    

    So the result of the above query is:

    3 ken
     2 John
     1 sam
     4 sam
    

    Now the second Query using the GroupBy clause:

     SELECT Count(ID), NAME
     FROM TABLE
    GROUP BY NAME
    

    The result of the above query is:

     1 ken
     1 John 
     2 sam
    
    1. ORDERBY clause shows a field in asc as well as desc while in GROUPBY shows the same fieldnames, ids in only one output.
    1. GROUP BY clause often used with the aggregate functions like SUM, AVG, MAX, MIN and COUNT on the columns in a query while ORDERBY is used to sort the result set.
    1. ORDERBY basically controls the presentation of the column for the results While GROUPBY clause controls the presentation of the rows.
    1. In OREDRBY clause order of columns make difference.

    For example:

    ORDERBY A,B,C and ORDERBY C,A,B both returns the same number of records but the row order will be different.

    while in GROUPBY order of column make no difference.

    For example:

    GROUPBY A,B,C and GROUPBY C,A,B both returns the same result.

    1. ORDERBY clause is not allowed in the CREATE VIEW statement while the GROUPBY cluase can be used in the CREATE VIEW statement to sort the data.
    1. In SELECT statement the OREDRBY clause is always places after the GROUPBY clause while in case of GROUPBY is always placed before the ORDERBY clause in the SELECT statement.

 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: