Home  >  Article  >  Database  >  The difference between group by and sort by in SQL

The difference between group by and sort by in SQL

PHPz
PHPzforward
2023-09-20 08:01:04998browse

The difference between group by and sort by in SQL

In this article, we will understand the difference between group by and order by in SQL.

GROUP BY

  • It is used to group rows with the same value.

  • It can be used in the CREATE VIEW statement.

  • The statement in select must be used before the "ORDER BY" keyword.

  • The attribute cannot appear in the "GROUP BY" statement when it is under an aggregate function. p>

  • Tuples are grouped according to the similarity of the tuple attribute values.

  • It controls the rendering/rows of tuples.

ORDER BY

  • It does not use the "CREATE VIEW" statement.

  • The result set is sorted in ascending or descending order.

  • It is used after the "group by" keyword.

  • This attribute can be located under an aggregate function.

  • It controls the rendering of columns.

    li>

The following is the syntax for Group By and Order By in a single query -

SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2

The above is the detailed content of The difference between group by and sort by in SQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete