Home > Database > Mysql Tutorial > How Does SQL's GROUP BY Clause Transform Data with Non-Unique Attributes?

How Does SQL's GROUP BY Clause Transform Data with Non-Unique Attributes?

Susan Sarandon
Release: 2025-01-10 13:06:41
Original
473 people have browsed it

How Does SQL's GROUP BY Clause Transform Data with Non-Unique Attributes?

Understanding SQL's GROUP BY Clause with Non-Unique Data

The GROUP BY clause is essential for transforming data in SQL tables, particularly when dealing with non-unique attributes. Let's illustrate this using a sample table, Tab1, containing non-unique attribute values.

The following SQL query uses GROUP BY for data aggregation:

<code class="language-sql">SELECT a1, a2, SUM(a3) FROM Tab1 GROUP BY a1, a2;</code>
Copy after login

Query Functionality Explained

This query groups the data based on unique combinations of a1 and a2. For each distinct pair of a1 and a2 values:

  • It creates a group containing all rows with matching attribute values.
  • It sums the a3 values within each group.
  • The result is a single row per group, showing the a1, a2 values, and the calculated sum of a3.

Therefore, the output won't be a single row, but rather multiple rows, each representing a unique combination of a1 and a2 found in the table.

The above is the detailed content of How Does SQL's GROUP BY Clause Transform Data with Non-Unique Attributes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template