Menu

How to select first row in each ‘Group By’ group?

How to select first row in each 'Group By' group? Let's understand with example problem and solutions.

Written by Selva Prabhakaran | 3 min read

Problem

You have a table with multiple rows of data for each group, and you want to select the first row from each group.

From the orders table below, select the first row in each group.

Input

order_idcustomer_idorder_dateproduct_namequantity
112023-01-01Laptop1
212023-01-02Mouse2
322023-01-03Keyboard1
422023-01-04Monitor1
532023-01-05Mouse Pad5
632023-01-06USB Cable3

Try Hands-On: Fiddle

Create Input Table: Gist

Desired Output

We want only the first record for each customer_id.

order_idcustomer_idorder_dateproduct_namequantity
112023-01-01Laptop1
322023-01-03Keyboard1
532023-01-05Mouse Pad5

There are multiple ways to do this. Let’s look at some of them.

Solution 1:

Using GROUP BY and JOIN

To achieve this, we can use a combination of JOIN and subquery. The idea is to find the earliest order date for each customer and then join that result with the main table.

sql
SELECT o1.*
FROM orders o1
JOIN (
    SELECT customer_id, MIN(order_date) as first_order_date
    FROM orders
    GROUP BY customer_id
) o2 ON o1.customer_id = o2.customer_id AND o1.order_date = o2.first_order_date;

Explanation:

This query first finds the earliest order_date for each customer_id in the subquery. Then, it joins this result with the main orders table to retrieve the details of the first order for each customer.

Solution 2:

Using ROW_NUMBER() window functions

MySQL does not have ROW_NUMBER() function. So, switch to a different DB like SQLite before running the below code.

The idea is to assign a row number to each row within a partition (in this case, for each customer_id) based on order_date, and then filter out the rows with row number 1.

sql
    WITH NumberedOrders AS (
        SELECT *,
               ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY order_date) AS rn
        FROM orders
    )

    SELECT *
    FROM NumberedOrders
    WHERE rn = 1;

Explanation:

The Common Table Expression (CTE) NumberedOrders assigns row numbers to each row in the orders table, partitioned by customer_id and ordered by order_date.

The main query then filters the results to only select rows where the row number rn is 1, which would be the first order for each customer.

This approach takes advantage of the capabilities of MySQL 8.0+, where window functions like ROW_NUMBER() were introduced.

Solution 3:

Using Correlated Subquery.

sql
SELECT o.*
FROM orders o
WHERE order_date = (
    SELECT MIN(order_date)
    FROM orders o_inner
    WHERE o_inner.customer_id = o.customer_id
);

Explanation:

For each row in the orders table (represented by the alias o), we perform a subquery.

This subquery (using the alias o_inner) retrieves the smallest (or “first” in terms of time) order_date for that specific customer_id.

If the order_date of the current row matches the result of this subquery, the row is included in the output.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Free Callback - Limited Slots
Not Sure Which Course to Start With?
Talk to our AI Counsellors and Practitioners. We'll help you clear all your questions for your background and goals, bridging the gap between your current skills and a career in AI.
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science