GROUP BY With Joins

Can we use GROUP BY without aggregation?

Yes, you can use GROUP BY without an aggregate function. It returns each distinct value once, much like DISTINCT, and shows where MySQL raises an error.

On this page
  1. Yes, GROUP BY works without an aggregate function
  2. GROUP BY or DISTINCT?
  3. Selecting an extra column
  4. Using it with an INNER JOIN
  5. Sorting the output

Yes, GROUP BY works without an aggregate function

GROUP BY doesn’t require an aggregate function such as COUNT() or SUM(). On its own, it returns each distinct combination of the grouped columns once, which gives the same rows as SELECT DISTINCT on those columns.

Here is a small customers table to work with:

CREATE TABLE customers (
  customer_id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  city VARCHAR(100) NOT NULL
);

INSERT INTO customers (customer_id, name, city) VALUES
  (1, 'Ana', 'Lisbon'),
  (2, 'Ben', 'Denver'),
  (3, 'Chen', 'Lisbon');

This query returns each city once:

SELECT city
FROM customers
GROUP BY city
ORDER BY city;

The output has two rows, Denver and Lisbon. The same result comes from SELECT DISTINCT city FROM customers ORDER BY city;.

GROUP BY or DISTINCT?

When all you want is unique values, DISTINCT says so directly. GROUP BY is the better fit when you expect to add an aggregate later, such as a count per city. Both are valid, so choose the one that says what you mean.

Once you want a number for each group, you add an aggregate function. Readers who ask “Which SQL function is used to calculate the average marks of students?” are looking for AVG(). Placed in a GROUP BY, it returns one average per group, such as one per student.

Selecting an extra column

Each column in the SELECT list must be grouped or aggregated. With ONLY_FULL_GROUP_BY on, which is the default in MySQL, a query that breaks this rule fails with error 1055:

-- Error 1055: each city has several names
SELECT city, name
FROM customers
GROUP BY city;

Lisbon has two names, and MySQL can’t choose one for that group. Add name to GROUP BY to get one row per city and name pair, or wrap it in an aggregate such as MAX(name) to get one value per city.

Using it with an INNER JOIN

GROUP BY without aggregates also works as a way to get a unique list after a join. Suppose orders has a customer_id column that points to customers. This query lists each customer who has placed at least one order, once each:

SELECT c.name
FROM customers AS c
INNER JOIN orders AS o
        ON o.customer_id = c.customer_id
GROUP BY c.name;

Customers with several orders still appear once, and customers with no orders are left out. Grouping by name also merges two different customers who share a name, so GROUP BY c.customer_id, c.name as well when that matters.

Sorting the output

MySQL 8.0 and later don’t sort GROUP BY results on their own. Older versions often did, so a query could look sorted until an upgrade changed it. Add ORDER BY, as in the examples above, whenever the order matters.

The complete guide to group by with joins

Using GROUP BY With INNER JOIN in MySQL
Read the guide

Keep exploring

Guides from other topics

All topics →