Home >Database >Mysql Tutorial >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>
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:
a3
values within each group.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!