Bird
Raised Fist0
Laravelframework~20 mins

Select queries in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Laravel Select Queries Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a basic select query
What will be the output of this Laravel Eloquent query if the users table has 3 records with names 'Alice', 'Bob', and 'Charlie'?
Laravel
$users = User::select('name')->get();
return $users->pluck('name')->toArray();
A["Alice", "Bob", "Charlie"]
B[{"name": "Alice"}, {"name": "Bob"}, {"name": "Charlie"}]
C["Alice Bob Charlie"]
D[]
Attempts:
2 left
💡 Hint
Remember that pluck extracts the values of a single column into an array.
❓ component_behavior
intermediate
2:00remaining
Behavior of select with multiple columns
Given this Laravel query, what will be the structure of each item in the returned collection?
Laravel
$users = User::select('id', 'email')->get();
return $users->first();
AAn array with 'id' and 'email' keys
BAn object with all user properties including 'id' and 'email'
CAn object with only 'id' and 'email' properties
DA string containing 'id' and 'email'
Attempts:
2 left
💡 Hint
Selecting specific columns limits the attributes loaded on the model.
📝 Syntax
advanced
2:00remaining
Correct syntax for selecting with alias
Which option correctly selects the 'name' column as 'username' using Laravel's query builder?
ADB::table('users')->select(['name as username'])->get();
BDB::table('users')->select('name')->as('username')->get();
CDB::table('users')->select(DB::raw('name as username'))->get();
DDB::table('users')->select('name as username')->get();
Attempts:
2 left
💡 Hint
Column aliases can be specified directly like 'name as username' in select().
🔧 Debug
advanced
2:00remaining
Identify the error in this select query
What error will this Laravel query produce?
Laravel
$users = User::select('id', 'email')->where('name', '=', 'Alice')->get();
return $users->email;
AError: Property 'email' does not exist on Collection
BError: Undefined variable 'users'
CNo error, returns email of first user
DError: Missing semicolon
Attempts:
2 left
💡 Hint
Remember what type is returned by get() method.
🧠 Conceptual
expert
3:00remaining
Effect of select on eager loading relationships
If you run this query, what will be the effect on the eager loaded 'posts' relationship?
Laravel
$users = User::select('id', 'name')->with('posts')->get();
AThe 'posts' relationship will not load because select limits the whole query
BThe 'posts' relationship will load all columns for posts as usual
CThe 'posts' relationship will only load 'id' and 'name' columns from posts
DThe query will throw an error because select conflicts with with()
Attempts:
2 left
💡 Hint
Eager loading runs separate queries for relationships.

Practice

(1/5)
1. What does the select() method do in a Laravel query builder?
easy
A. It specifies which columns to retrieve from the database.
B. It inserts new data into the database.
C. It deletes rows from the database table.
D. It updates existing records in the database.

Solution

  1. Step 1: Understand the purpose of select()

    The select() method tells Laravel which columns you want to get from the database table.
  2. Step 2: Compare with other query methods

    Methods like insert(), delete(), and update() perform different actions, not selecting columns.
  3. Final Answer:

    It specifies which columns to retrieve from the database. -> Option A
  4. Quick Check:

    select() chooses columns [OK]
Hint: Remember: select() picks columns, not rows or actions [OK]
Common Mistakes:
  • Confusing select() with insert() or update()
  • Thinking select() deletes data
  • Assuming select() fetches all columns by default
2. Which of the following is the correct syntax to select the columns 'name' and 'email' from the 'users' table using Laravel's query builder?
easy
A. DB::table('users')->select(['name', 'email'])->get();
B. DB::table('users')->select('name' & 'email')->get();
C. DB::table('users')->select('name, email')->get();
D. DB::table('users')->select('name' + 'email')->get();

Solution

  1. Step 1: Check the correct way to pass multiple columns

    Laravel's select() accepts an array of column names to select multiple columns.
  2. Step 2: Analyze each option's syntax

    DB::table('users')->select('name, email')->get(); uses select('name, email'), which selects a single column named 'name, email'. Options B and D use invalid operators & and +. DB::table('users')->select(['name', 'email'])->get(); correctly uses an array ['name', 'email'].
  3. Final Answer:

    DB::table('users')->select(['name', 'email'])->get(); -> Option A
  4. Quick Check:

    Use array for multiple columns in select() [OK]
Hint: Use array syntax for multiple columns in select() [OK]
Common Mistakes:
  • Passing multiple strings without array
  • Using operators like + or & inside select()
  • Forgetting to call get() to execute query
3. What will be the output of the following Laravel query?
DB::table('products')->select('name')->where('price', '>', 100)->get();
medium
A. All products with price greater than 100, showing all columns.
B. All products with price less than or equal to 100, showing only their names.
C. All products with price greater than 100, showing only their names.
D. Syntax error due to incorrect where clause.

Solution

  1. Step 1: Understand the where condition

    The query filters products where the price is greater than 100.
  2. Step 2: Understand the select clause

    Only the 'name' column is selected, so only product names will be returned.
  3. Final Answer:

    All products with price greater than 100, showing only their names. -> Option C
  4. Quick Check:

    where('price', '>', 100) filters price > 100 [OK]
Hint: where() filters rows; select() chooses columns [OK]
Common Mistakes:
  • Confusing > with < in where clause
  • Expecting all columns instead of selected ones
  • Thinking where() causes syntax error here
4. Identify the error in this Laravel query:
DB::table('orders')->select('id', 'total')->where('status' = 'pending')->get();
medium
A. Using '=' instead of '==' in where clause.
B. Using '=' instead of ',' to separate arguments in where().
C. Missing semicolon at the end of the query.
D. select() cannot take multiple columns.

Solution

  1. Step 1: Check the where clause syntax

    The where method expects two arguments separated by a comma, not an assignment with '='.
  2. Step 2: Identify the correct syntax

    The correct syntax is where('status', 'pending'), not where('status' = 'pending').
  3. Final Answer:

    Using '=' instead of ',' to separate arguments in where(). -> Option B
  4. Quick Check:

    where() needs comma between arguments [OK]
Hint: Use comma, not =, between where() arguments [OK]
Common Mistakes:
  • Using = instead of comma in where()
  • Confusing assignment with comparison
  • Thinking select() can't take multiple columns
5. You want to get the 'id' and 'email' of users who are active and have verified their email. Which Laravel query correctly does this?
hard
A. DB::table('users')->select(['id', 'email'])->where('active' = 1)->where('email_verified' = 1)->get();
B. DB::table('users')->select('id', 'email')->where('active' == 1 && 'email_verified' == 1)->get();
C. DB::table('users')->select(['id', 'email'])->where('active', '=', 1, 'email_verified', '=', 1)->get();
D. DB::table('users')->select(['id', 'email'])->where(['active' => 1, 'email_verified' => 1])->get();

Solution

  1. Step 1: Understand filtering multiple conditions

    Laravel allows passing an associative array to where() to filter multiple columns with AND logic.
  2. Step 2: Analyze each option

    DB::table('users')->select(['id', 'email'])->where(['active' => 1, 'email_verified' => 1])->get(); correctly uses where(['active' => 1, 'email_verified' => 1]). DB::table('users')->select('id', 'email')->where('active' == 1 && 'email_verified' == 1)->get(); uses invalid syntax with == and && inside where(). DB::table('users')->select(['id', 'email'])->where('active', '=', 1, 'email_verified', '=', 1)->get(); passes too many arguments to a single where(). DB::table('users')->select(['id', 'email'])->where('active' = 1)->where('email_verified' = 1)->get(); uses = instead of , to separate where() arguments, causing syntax error.
  3. Final Answer:

    DB::table('users')->select(['id', 'email'])->where(['active' => 1, 'email_verified' => 1])->get(); -> Option D
  4. Quick Check:

    Use array in where() for multiple AND conditions [OK]
Hint: Use array in where() for multiple AND filters [OK]
Common Mistakes:
  • Using invalid operators inside where()
  • Passing too many arguments to where()
  • Not using array for multiple conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard