4. Interview Questions- ORM Querying Guide
1. What method is used to start a query in SQLAlchemy ORM?
2. What is the recommended querying style in SQLAlchemy 2.0?
stmt = select(User).where(User.name == "Alice")
session.execute(stmt)3. How do you filter rows in SQLAlchemy 2.0?
select(User).where(User.id == 1)4. How do you retrieve a single result from a query?
5. What is the difference between scalar_one() and scalar_one_or_none()?
scalar_one() and scalar_one_or_none()?6. How do you retrieve all rows from a select statement?
7. What does .scalars() do on a result object?
.scalars() do on a result object?8. How do you order query results in SQLAlchemy ORM?
9. How do you limit the number of rows returned in SQLAlchemy?
10. What is the difference between select(User) and session.query(User)?
select(User) and session.query(User)?11. How do you join two tables in SQLAlchemy 2.0?
12. How do you filter based on a joined table's column?
13. What does distinct() do in a query?
distinct() do in a query?14. How can you perform an IN clause in a SQLAlchemy query?
15. How do you use SQL functions like count() in SQLAlchemy?
count() in SQLAlchemy?16. What method is used to paginate results (i.e., skip and limit)?
17. What does .unique() do on a result object?
.unique() do on a result object?18. Can you use ORM objects in select() without importing the table explicitly?
select() without importing the table explicitly?19. How do you count rows while using select() with ORM models?
select() with ORM models?20. What is the recommended way to iterate over results in a memory-efficient way?
21. How do you perform a left outer join in SQLAlchemy ORM using select()?
select()?22. What’s the purpose of using select_from() in SQLAlchemy queries?
select_from() in SQLAlchemy queries?23. How do you alias a table or model in SQLAlchemy 2.0 ORM queries?
24. How do you construct a subquery in SQLAlchemy ORM?
25. How do you write a correlated subquery in SQLAlchemy ORM?
26. How do you use exists() in SQLAlchemy ORM queries?
exists() in SQLAlchemy ORM queries?27. How do you check for NULL values in a column?
NULL values in a column?28. What’s the use of with_only_columns() in a select statement?
with_only_columns() in a select statement?29. What is the difference between .fetchall(), .scalars().all(), and .scalar_one()?
.fetchall(), .scalars().all(), and .scalar_one()?30. How do you perform a group by aggregation in SQLAlchemy ORM?
31. What does .first() return in SQLAlchemy ORM?
.first() return in SQLAlchemy ORM?32. Can you use ORM select() with .columns() like Core?
select() with .columns() like Core?33. How do you preload related data to avoid N+1 issues?
34. How do you use selectinload() in a query?
selectinload() in a query?35. How do you load a nested relationship eagerly?
36. What’s the purpose of contains_eager()?
contains_eager()?37. How can you sort by an aggregated value like count of child rows?
38. What does select(...).distinct() do?
select(...).distinct() do?39. What’s the benefit of using session.scalars() over session.execute().scalars()?
session.scalars() over session.execute().scalars()?40. Can you combine ORM models and Core tables in a single query?
41. How do you perform an UPDATE using SQLAlchemy ORM with select()?
select()?42. How do you perform a DELETE operation in SQLAlchemy ORM?
43. How do you update an ORM object directly instead of using update()?
update()?44. What’s the difference between get() and get_or_404()?
get() and get_or_404()?45. How do you execute a bulk INSERT with ORM-mapped objects?
46. What’s the advantage of using bulk_insert_mappings()?
bulk_insert_mappings()?47. How can you detect whether a query is returning too many rows (performance issue)?
48. How can you efficiently fetch only a few fields instead of whole ORM objects?
49. What happens if you call .one() on a query returning multiple rows?
.one() on a query returning multiple rows?50. How do you avoid loading ORM entities entirely but still perform ORM-aware queries?
Previous3. Interview Questions- Relationship ConfigurationNext5. Interview Questions- Using the Session
Last updated