GROUP BY without aggregate function
Understanding GROUP BY Without Aggregate Functions in SQL

Explore the often misunderstood behavior of the SQL GROUP BY clause when no aggregate functions are present, and learn how it functions as a powerful distinct filter.
The GROUP BY
clause in SQL is primarily known for its role in summarizing data using aggregate functions like COUNT()
, SUM()
, AVG()
, MIN()
, and MAX()
. However, its behavior when used without any aggregate functions is often a source of confusion for many SQL users. This article will demystify this specific use case, explaining how GROUP BY
acts as an implicit DISTINCT
operator and providing practical examples.
The Core Function of GROUP BY
At its heart, the GROUP BY
clause organizes rows that have the same values in one or more specified columns into a summary row. When aggregate functions are present, they operate on these groups to produce a single result for each group. But what happens when there are no aggregates? In this scenario, GROUP BY
effectively filters out duplicate rows based on the columns specified in the GROUP BY
clause itself, returning only one unique row for each distinct combination of those columns.
flowchart TD A[Input Data Rows] --> B{GROUP BY Clause?} B -- Yes --> C[Group Rows by Specified Columns] C --> D{Aggregate Functions Present?} D -- No --> E[Return One Unique Row Per Group] D -- Yes --> F[Apply Aggregate Functions to Each Group] F --> G[Return Summary Row Per Group] E --> H[Result Set] G --> H[Result Set] B -- No --> I[Process Without Grouping]
Decision flow for SQL GROUP BY behavior
GROUP BY as a DISTINCT Filter
When you use GROUP BY
on a set of columns without any aggregate functions in the SELECT
list, the database engine processes the data to find all unique combinations of values across those columns. For each unique combination, it returns exactly one row. This behavior is functionally equivalent to using the DISTINCT
keyword on the same set of columns. While both achieve the same result, GROUP BY
can sometimes be more performant or necessary in complex queries involving HAVING
clauses or window functions, even if no aggregates are explicitly used in the SELECT
list.
SELECT column1, column2
FROM your_table
GROUP BY column1, column2;
Using GROUP BY to get distinct combinations of column1 and column2
SELECT DISTINCT column1, column2
FROM your_table;
Using DISTINCT to get distinct combinations of column1 and column2 (equivalent to the above)
GROUP BY
and DISTINCT
often yield the same results for simple distinct selections, GROUP BY
is more versatile. It's the foundation for aggregate operations and can be combined with HAVING
for group-level filtering, which DISTINCT
cannot do directly.Practical Implications and Use Cases
Understanding this behavior is crucial for writing efficient and correct SQL queries. Here are a few scenarios where GROUP BY
without aggregates can be particularly useful:
- Finding Unique Combinations: As demonstrated, it's a direct way to get unique rows based on specified columns.
- Preparing for Aggregations: Sometimes you might first want to identify unique entities before applying aggregates in a subquery or CTE.
- Performance Considerations: In some database systems and for certain data distributions,
GROUP BY
might offer better performance thanDISTINCT
or vice-versa. It's often worth testing both approaches. - Implicit Grouping in Complex Queries: In more complex queries involving joins or subqueries,
GROUP BY
can implicitly handle the uniqueness requirement for the selected columns, even if the primary goal isn't aggregation.
-- Example Table: Orders
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
Product VARCHAR(50)
);
INSERT INTO Orders (OrderID, CustomerID, OrderDate, Product) VALUES
(1, 101, '2023-01-15', 'Laptop'),
(2, 102, '2023-01-15', 'Mouse'),
(3, 101, '2023-01-16', 'Keyboard'),
(4, 103, '2023-01-17', 'Laptop'),
(5, 102, '2023-01-17', 'Mouse'),
(6, 101, '2023-01-18', 'Laptop');
-- Get all unique customer IDs who placed an order
SELECT CustomerID
FROM Orders
GROUP BY CustomerID;
-- Expected Output:
-- CustomerID
-- 101
-- 102
-- 103
-- Get all unique combinations of CustomerID and Product ordered
SELECT CustomerID, Product
FROM Orders
GROUP BY CustomerID, Product;
-- Expected Output:
-- CustomerID | Product
-- 101 | Keyboard
-- 101 | Laptop
-- 102 | Mouse
-- 103 | Laptop
SQL examples demonstrating GROUP BY without aggregate functions
GROUP BY
with non-grouped columns in the SELECT
list. Most SQL databases will throw an error if a column in the SELECT
list is not part of the GROUP BY
clause and is not an aggregate function. This is because the database wouldn't know which value to pick for that non-grouped column from within each group.