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

The difference between group by and sort by in SQL

PHPz
Release: 2023-09-20 08:01:04
forward
1046 people have browsed it

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template