Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to fetch all users from the database using Flask-SQLAlchemy.
Flask
users = User.query.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter() without conditions causes an error.
Using get() requires a primary key argument.
✗ Incorrect
The all() method fetches all records from the query.
2fill in blank
mediumComplete the code to filter users with age greater than 30.
Flask
users = User.query.filter(User.age [1] 30).all()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
== filters only users exactly 30 years old.Using
<= includes users 30 or younger.✗ Incorrect
The > operator filters users older than 30.
3fill in blank
hardFix the error in the query to get the first user with name 'Alice'.
Flask
user = User.query.filter_by(name=[1]).first() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the string causes a NameError.
Using variable name instead of string literal.
✗ Incorrect
String values must be quoted in Python queries. Use single or double quotes.
4fill in blank
hardFill both blanks to optimize the query by selecting only 'id' and 'name' columns.
Flask
users = db.session.query(User.[1], User.[2]).all()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting all columns when only a few are needed.
Choosing wrong columns that are not required.
✗ Incorrect
Selecting only needed columns reduces data load and speeds up queries.
5fill in blank
hardFill all three blanks to create a query that orders users by age descending and limits to 5 results.
Flask
users = User.query.order_by(User.[1].[2]()).[3](5).all()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'asc' instead of 'desc' for descending order.
Forgetting to limit the number of results.
✗ Incorrect
Ordering by age descending uses desc(), and limit(5) restricts results to 5.