I'm wondering about best practices for organizing SQL queries in backend applications. Specifically, should SQL be embedded directly in the application code or stored in separate `.sql` files? What are the advantages and disadvantages of both approaches?
2 Answers
I personally like using an ORM over raw SQL. It keeps things clean, and with inline queries, you're less likely to deal with stray changes. I've had experiences where separating SQL into files just led to confusion during reviews, especially with complex queries.
Inline SQL tends to make more sense because it keeps the queries closely tied to the code they're used with. Plus, modern IDEs offer great tools that enhance safety and ease of use when SQL is embedded.
But isn't that just like sharing any function? You could say the same about code organization in general!

Totally get that! But if your queries get really long, wouldn't separate files still be more manageable?