0
0
Laravelframework~20 mins

Ordering and grouping in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Ordering and Grouping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel Eloquent query?
Consider the following Eloquent query in Laravel:
$results = User::select('role', DB::raw('count(*) as total'))
    ->groupBy('role')
    ->orderBy('total', 'desc')
    ->get();

return $results->toArray();

What does this query return?
Laravel
use Illuminate\Support\Facades\DB;

$results = User::select('role', DB::raw('count(*) as total'))
    ->groupBy('role')
    ->orderBy('total', 'desc')
    ->get();

return $results->toArray();
AA syntax error because 'total' cannot be used in orderBy after groupBy.
BAn array of users ordered alphabetically by role name.
CAn array of all users with a total count of all users appended to each record.
DAn array of roles with their user counts, ordered from highest to lowest count.
Attempts:
2 left
💡 Hint
Think about what groupBy and orderBy do together in Eloquent queries.
📝 Syntax
intermediate
2:00remaining
Which option correctly groups and orders users by status and creation date?
You want to group users by their 'status' and order each group by the earliest 'created_at' date ascending. Which Eloquent query is correct?
A
User::select('status', DB::raw('min(created_at) as earliest'))
    ->groupBy('status')
    ->orderBy('earliest', 'asc')
    ->get();
BUser::orderBy('created_at', 'asc')->groupBy('status')->get();
CUser::groupBy('status')->orderBy('created_at', 'asc')->get();
DUser::groupBy('status')->orderByRaw('MIN(created_at) ASC')->get();
Attempts:
2 left
💡 Hint
Remember that ordering by an aggregate requires selecting it first.
🔧 Debug
advanced
2:00remaining
Why does this Laravel query throw an SQL error?
Given this Laravel query:
$users = User::select('status', DB::raw('count(*) as total'))
    ->orderBy('total', 'desc')
    ->groupBy('status')
    ->get();

It throws an SQL error about 'total' in ORDER BY. Why?
Laravel
use Illuminate\Support\Facades\DB;

$users = User::select('status', DB::raw('count(*) as total'))
    ->orderBy('total', 'desc')
    ->groupBy('status')
    ->get();
ABecause 'total' is not a column in the users table, orderBy cannot use it.
BBecause orderBy is called before groupBy, the alias 'total' is not recognized in ORDER BY.
CBecause groupBy must come before select in the query chain.
DBecause count(*) cannot be aliased in select when using groupBy.
Attempts:
2 left
💡 Hint
Think about the order SQL processes clauses and how Laravel builds the query.
🧠 Conceptual
advanced
2:00remaining
How does Laravel handle ordering by a computed column in a grouped query?
In Laravel Eloquent, when you group records and select a computed column like count(*), how can you order the results by that computed column?
AYou can orderBy the computed column name directly without selecting it first.
BLaravel automatically orders grouped queries by the first selected column.
CYou must select the computed column with an alias and then orderBy using that alias.
DYou cannot order grouped queries by computed columns in Laravel.
Attempts:
2 left
💡 Hint
Think about how SQL requires ordering by columns present in the SELECT clause.
state_output
expert
2:00remaining
What is the count of groups returned by this Laravel query?
Given the users table has 100 records with 4 distinct 'department' values, what is the count of items in the result of this query?
$groups = User::groupBy('department')->get();
return $groups->count();
Laravel
use App\Models\User;

$groups = User::groupBy('department')->get();
return $groups->count();
A100
B4
C1
D0
Attempts:
2 left
💡 Hint
Remember that groupBy alone does not reduce the number of rows returned without aggregation.