How to select duplicate rows and group by two columns

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Data Duplication: Selecting and Grouping Rows in Laravel

As developers working with relational databases, a common challenge arises when dealing with large datasets: identifying unique combinations or handling rows that represent duplicates based on specific criteria. Today, we will walk through how to approach this problem using both raw SQL and the elegant Eloquent Query Builder within the Laravel ecosystem.

Understanding how to efficiently select distinct data based on multiple columns is crucial for optimizing database queries and ensuring data integrity in your applications.

The Scenario: Identifying Duplicates by Criteria

Let's look at the sample data you provided. We want to focus specifically on group_id = 3 and find the unique combinations of subject and book_id present within that group.

Your Data:

ID|user_id|group_id|subject |book_id
1| 2     |3       |history |1
2| 4     |3       |history |1  <-- Duplicate key (history, 1)
3| 5     |3       |art     |2
4| 2     |3       |art     |2  <-- Duplicate key (art, 2)
5| 1     |4       |sport   |5

Desired Result: We need to extract the unique pairings for group_id = 3:

subject |book_id
--------|-------
history |1
art     |2

This task fundamentally requires using aggregation functions or distinct selection mechanisms in SQL.

Solution 1: The Power of Raw SQL

For complex data manipulation, raw SQL often provides the most direct and performant solution. We will use the SELECT DISTINCT clause combined with a WHERE filter to achieve the desired grouping.

SQL Implementation

To get the distinct combinations for group_id = 3, the query is straightforward:

SELECT DISTINCT subject, book_id
FROM your_table_name
WHERE group_id = 3;

Explanation:

  1. SELECT DISTINCT subject, book_id: This instructs the database to return only unique pairs of values found in the specified columns. If multiple rows share the same combination of subject and book_id, only one instance will be returned.
  2. FROM your_table_name: Specifies the table we are querying.
  3. WHERE group_id = 3: Filters the result set to only include records belonging to group 3, as required by the problem.

This approach is highly efficient because the database engine is optimized for these types of set operations, making it ideal for high-volume data processing. If you are building complex data interactions in Laravel, remember that mastering SQL fundamentals is key to writing performant Eloquent queries. For deeper dives into database interaction patterns within a framework like Laravel, exploring official documentation can be very helpful, much like understanding the robust structure provided by laravelcompany.com.

Solution 2: Implementing with Eloquent

While raw SQL is powerful, developers often prefer to stay within the ORM paradigm when working in a Laravel application. We can achieve the same result using Eloquent's Query Builder methods.

Eloquent Implementation

Assuming you have an Eloquent Model set up for your table (e.g., BookItem), here is how you would execute this query:

use App\Models\BookItem;

$results = BookItem::where('group_id', 3)
                    ->select('subject', 'book_id')
                    ->distinct()
                    ->get();

// $results will now contain the desired unique combinations.

Explanation:

  1. BookItem::where('group_id', 3): This starts by filtering the entire dataset down to only the records associated with group_id = 3.
  2. select('subject', 'book_id'): We specify exactly which columns we want in the final result set.
  3. distinct(): This is the Eloquent method that translates directly into the SQL DISTINCT clause, ensuring that only unique combinations of the selected columns are returned.
  4. get(): Executes the query and retrieves the results as a collection of objects.

This Eloquent approach keeps your code clean, readable, and highly maintainable. It abstracts away the direct SQL syntax while still leveraging the database's underlying performance for grouping operations. When structuring complex data relationships in Laravel, using these streamlined methods ensures that your application remains robust and scalable.

Conclusion

Whether you opt for raw SQL or Eloquent, the core principle remains the same: use the appropriate mechanism to instruct the database to filter records by a group and return only the unique combinations of specified columns. For simple filtering like this, both methods are effective. When performance is paramount, direct SQL is excellent; when code readability and adherence to the framework structure are priorities, Eloquent provides the perfect abstraction layer. As you continue developing with Laravel, mastering these data manipulation techniques will significantly enhance your ability to handle complex data requirements efficiently.