As a lead, when would you allow dynamic SQL in a codebase at all, and what standing rules would you set around it?
answer
- why does the shape vary?
- the ban has its own cost
- one sanctioned way to build
- fragments and names held in code
- visible, tested, revisited
basics
~20 sAllow dynamic SQL only where the statement's shape truly varies and fixed statements cannot cover it, after asking whether the data model causes the variation. Then require one shared builder, fixed fragments, allowlisted names and bound values.
solid answer
~50 sI would make static SQL the default and allow dynamic SQL where the shape truly varies beyond what a few fixed statements cover, typically search and admin reports. First I would ask why it varies: a table chosen per tenant is often a data-model decision, and a tenant column would turn it into a value. A flat ban has costs too, because teams then write huge catch-all predicates or dozens of near-identical statements. So the rules go on how it is built: one shared builder or library, fragments written as constants in code, names only from an allowlist owned by the report, values always bound. I would also make the generated statements visible in logs, test the shapes people really use, and have a lint rule or review check flag any other assembly of SQL strings. The trade-off is less flexibility per team for one place to fix and audit.
go deeper
Recall the default: write fixed statements with bind parameters, and raise it with the team before building statement text yourself, because many codebases have a rule or a shared helper.
Know the rules a team usually sets, fragments in code, names from a fixed list and values bound, and explain what each one prevents in a builder.
Bring evidence to the decision: which shapes are actually used, which are slow, and whether a filter or a table split could be dropped so the dynamic path never has to exist.
Own the trade-off: a ban moves the variation into catch-all predicates and copies, while a sanctioned path costs ownership. State the default, the exit condition and what would reopen it.
## There is no single right answer This is a standing decision, not a fact to recall. A team can reasonably land in several places. What an interviewer looks for is a default, the conditions for leaving it, and an understanding of what each choice costs. The right answer for a team with one reporting service differs from the right answer for a platform with fifty. Terms first: **dynamic SQL** is a statement whose text is assembled at run time, and **static SQL** has its text fixed when the code is written, with values passed as bind parameters. ## Ask first why the shape varies Before writing a policy, look at what is creating the demand. The cause often lies elsewhere: - **A table chosen at run time.** Per-tenant or per-month tables force the name into the text. A tenant column, or partitioning behind one table name, makes it a value instead. Per-tenant tables can be a deliberate isolation choice, but then the dynamic SQL is part of that choice's price and belongs in the decision. - **A column chosen at run time.** A report that picks among similar columns may be pointing at a modelling problem, such as one column per month. - **A product surface.** An admin report with a dozen filters may need only four. Removing filters people never use removes shapes. - **A real search feature.** Sometimes the variation is the product. That is the honest case for dynamic SQL. ## The options and what they cost | Standing position | What you gain | What it costs | |---|---|---| | Ban it outright | Every statement can be read and searched for | Catch-all predicates, near-duplicate statements, and quiet workarounds | | Allow it through one sanctioned path | One place to review, fix and test | Building and owning that path, and some friction for teams | | Adopt a query-builder library | Composition in code, less string work | A dependency, and generated SQL people must learn to read | | Leave it to each team | No central effort | As many builders as teams, with uneven handling of names | A ban often looks strongest and fails quietly. If the requirement stays, the variation moves into a giant `($1 IS NULL OR col = $1)` statement or thirty copies of one query, neither of which is simpler to run. Those can be fine for small cases, but they are not free consequences of a clean rule. ## Rules worth standing behind If a team allows dynamic SQL, these are the rules that tend to earn their keep: 1. **Static first.** A statement with a fixed shape stays fixed. Dynamic is justified by a requirement. 2. **One way to build.** A shared builder or chosen library, not string concatenation at each call site. 3. **Fragments are constants.** Every piece of text is written in code. Input selects pieces and never becomes text itself. 4. **Names come from an allowlist.** Each report owns a small map from the caller's choice to a fixed name, because an unquoted name taken from input is injectable. 5. **Values are always bound,** including the ones that seem harmless. 6. **The always-on predicate is not optional.** A tenant filter is added by the builder, not by each report. 7. **Mechanical enforcement.** A lint rule or review check flags SQL assembled anywhere else. Unenforced rules decay. ## Where it should run Building text in application code and building it inside a database routine are both options, and moving it into a routine does not remove any of these concerns. It relocates them to where review, testing and logging are often weaker. A team with most of its logic in the database may reasonably decide otherwise. It should still be a decision, with the same rules, not an accident. ## Keeping it honest over time A policy needs feedback, or it turns into folklore: - **Visibility.** Log the generated statements or their normalised form, so slow or surprising shapes can be found. - **Test the shapes that matter.** Single filters, none, and the common pairs; exhaustive combinations are out of reach. - **Count the exceptions.** If many services need an exception, the rule or the builder is wrong. - **Revisit triggers.** A new engine, a new tenant model or a move to a query library should reopen the decision. ## How to answer in an interview State a default, name the condition for leaving it, and describe the concrete rules. Then say what would change your mind. A team with five engineers and three reports may need nothing more than a short rule and code review. A platform shared by many teams may justify a builder and a lint rule. Saying which you are facing, and why, is the judgment being asked for.
- A team asks to pick among per-tenant tables at run time. What do you ask before approving a builder?Why the tables are separate. If the reason is habit, a tenant column or one partitioned table makes the tenant a bound value and removes the dynamic text. If the reason is strict isolation or a contractual boundary, the separate tables may stand, and the run-time name becomes a known cost of that decision.
- When would you accept a catch-all predicate instead of the shared builder?When there are only a few filters, an always-on predicate is already selective, and you have measured that the statement performs acceptably on real data. It keeps one static text and adds no machinery. I would reconsider once filters multiply or filter combinations show up as slow, because then the same statement is serving too many cases.
- Does moving dynamic SQL into a stored routine satisfy the policy?Not by itself. A routine that assembles text still needs fixed fragments, allowlisted names and bound values, and now has weaker tooling for review and tests in many teams. Moving it changes where the rules apply, not whether they do. It can still be right where database routines are already the team's main home for logic.
- A flat ban on dynamic SQL has no cost, because static SQL can always replace it.
- Per-tenant tables are fixed, so a builder is the only thing worth discussing.
- Moving text assembly into a stored routine removes the need for any rules.
- A code-review checklist alone will keep string-built SQL out indefinitely.
- Every team writing its own builder costs nothing, since their reports differ.