Challenge - 5 Problems
Filter Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Flask-SQLAlchemy query return?
Consider a model
User with fields id and name. What will this query return?users = User.query.filter(User.name == 'Alice').all()Attempts:
2 left
💡 Hint
Remember that filter returns a query object that can return multiple results with .all().
✗ Incorrect
The filter method takes a condition and returns a query for all matching rows. Using .all() returns a list of all User objects with name 'Alice'.
📝 Syntax
intermediate2:00remaining
Which option correctly uses filter_by to find users named 'Bob'?
Given the User model with a
name field, which query correctly finds all users named 'Bob'?Attempts:
2 left
💡 Hint
filter_by uses keyword arguments, not expressions or strings.
✗ Incorrect
filter_by accepts keyword arguments like name='Bob'. It does not accept expressions or strings as conditions.
🔧 Debug
advanced2:00remaining
Why does this filter query raise an error?
What error does this code raise and why?
users = User.query.filter(User.age > 20).filter_by(name='Charlie').all()Attempts:
2 left
💡 Hint
filter and filter_by can be chained; filter_by adds keyword filters.
✗ Incorrect
Chaining filter and filter_by is valid. The query returns users with age > 20 and name 'Charlie'. No error occurs if fields exist.
❓ state_output
advanced2:00remaining
What is the length of the result list?
Assuming the database has users with names: ['Anna', 'Bob', 'Anna', 'Charlie'], what is the length of the list returned by:
result = User.query.filter_by(name='Anna').all()Attempts:
2 left
💡 Hint
filter_by returns all matching rows, not just one.
✗ Incorrect
There are two users named 'Anna', so the list length is 2.
🧠 Conceptual
expert2:00remaining
Which statement about filter and filter_by is true?
Choose the correct statement about the difference between
filter and filter_by in Flask-SQLAlchemy.Attempts:
2 left
💡 Hint
Think about how you write conditions in each method.
✗ Incorrect
filter_by uses keyword arguments like name='Alice'. filter uses SQL expressions like User.name == 'Alice'.