Technical Mid Level

When do you use DQL versus the QueryBuilder versus raw SQL in Doctrine? What are the trade-offs of each approach?

Quick Tip

Match the tool: "QueryBuilder when the query shape varies based on user input. DQL for fixed complex queries with entity hydration. Raw SQL for reporting queries or performance-critical reads where I only need arrays."

What good answers include

DQL (Doctrine Query Language): SQL-like syntax operating on entities instead of tables. Good for complex queries with joins and aggregations. QueryBuilder: programmatic, fluent API for building DQL dynamically — ideal when queries vary based on conditions (filters, search). Raw SQL (via Connection): when you need database-specific features, complex aggregations, or performance-critical queries that bypass ORM hydration. Trade-offs: DQL and QueryBuilder return hydrated entities (convenient but costly for read-only data); raw SQL returns arrays (fast but no entity features). Strong candidates discuss: using SELECT NEW for DTO hydration, partial objects, result caching, and when the ORM abstraction helps versus hinders.

What interviewers are looking for

Tests Doctrine depth. Candidates who only use findBy/findOneBy cannot handle complex data requirements. Those who reach for raw SQL by default are not leveraging the ORM. Look for judgement about when each approach fits.

← All Symfony questions