Laravel : Syntax error or access violation: 1055 Error

Laravel : Syntax error or access violation: 1055 Error

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel : Syntax error or access violation: 1055 Error

When combining whereIn and groupBy in a single query, developers often run into SQLSTATE[42000]: Syntax error or access violation: 1055. This specific MySQL error indicates that the database engine cannot determine how to group non-aggregated columns that are not included in the GROUP BY clause.

Consider a scenario where you need to filter records by a list of IDs and then aggregate them by a specific field, such as vehicle number. In your application logic, you might parse a delimited string like #@* to extract an array of loading IDs:

$loadIds = explode('#@*', $receiptDet->loading_id);

If you attempt to execute the following query directly using the legacy helper or the older Fluent interface without modification:

$loadingData = DB::table('loading')
    ->groupBy('vehicle_no')
    ->whereIn('id', $loadIds)
    ->get();

MySQL throws the error 1055 'sbrtpt.loading.id' isn't in GROUP BY. The underlying SQL generated looks roughly like this:

SELECT * FROM loading WHERE id IN (14, 15, 16) GROUP BY vehicle_no

The issue stems from a fundamental rule in the SQL standard that MySQL enforces strictly in its default mode. When you use GROUP BY, MySQL expects every column in the SELECT list to either be part of the aggregation (like COUNT(), SUM()) or explicitly listed in the GROUP BY clause. Since SELECT * implicitly selects all columns—including id—MySQL complains because id is neither aggregated nor grouped by.

While this might seem like a minor syntax issue, it highlights a common pitfall when mixing filtering operations with aggregation logic. The error occurs because the database cannot guarantee which id belongs to which vehicle_no if multiple rows share the same vehicle number but have different IDs.

To resolve this in Laravel 10 and later, the recommended approach is to use Eloquent models or ensure strict compliance with SQL standards by selecting only the necessary columns. If you are using the query builder on a model, you can chain the methods differently:

$loadingData = Loading::whereIn('id', $loadIds)
    ->groupBy('vehicle_no')
    ->get();

However, even with Eloquent, if * is returned implicitly or explicitly, the error persists. The most robust solution is to select only the columns you actually need, excluding the non-aggregated filtered column from the result set:

$loadingData = Loading::select('vehicle_no')
    ->whereIn('id', $loadIds)
    ->groupBy('vehicle_no')
    ->get();

By selecting only vehicle_no, the query becomes valid because there are no other non-aggregated columns left for MySQL to complain about. The result will be a collection where each item represents a unique vehicle number found within the specified ID list.

If you require specific data from both the filtered IDs and the grouped column, you must explicitly select both and handle the grouping logic carefully. However, in many cases involving GROUP BY, the original intent is often to find unique values rather than retain all original row attributes. If retaining the full row is critical, consider performing the filtering first and then aggregating, or using a subquery if the database allows it without strict mode enforcement.

Another alternative involves checking your MySQL configuration. The error message 1055 is strictly enforced when SQL modes like ONLY_FULL_GROUP_BY are active. If you cannot change the query structure immediately due to legacy constraints, enabling STRICT_TRANS_TABLES or adjusting the SQL mode might suppress the error in older versions, but this is generally discouraged as it masks logical flaws in your query design.

Ultimately, when combining filters and aggregations, clarity in column selection prevents runtime errors and ensures your application behaves consistently across different MySQL configurations. If you encounter similar issues with complex joins or subqueries, reviewing the generated SQL via ddQuery or logging can provide immediate insight into which columns are triggering the violation. For deeper dives into query optimization and best practices for Laravel development, resources like https://laravelcompany.com offer valuable insights into maintaining high-performance applications.

Our network