0
0
Flaskframework~10 mins

Database query optimization in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Afirst
Bfilter
Cget
Dall
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter() without conditions causes an error.
Using get() requires a primary key argument.
2fill in blank
medium

Complete 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'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == filters only users exactly 30 years old.
Using <= includes users 30 or younger.
3fill in blank
hard

Fix 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'
A"Alice"
BAlice
C'Alice'
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the string causes a NameError.
Using variable name instead of string literal.
4fill in blank
hard

Fill 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'
Aid
Bage
Cname
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting all columns when only a few are needed.
Choosing wrong columns that are not required.
5fill in blank
hard

Fill 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'
Aage
Bdesc
Climit
Dasc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'asc' instead of 'desc' for descending order.
Forgetting to limit the number of results.