A Cool SQL Problem: Avoiding For-Loops


This post is a neat interview problem, but I like it because it does a great job of teaching a concept that I find myself teaching junior data people very frequently. Here's the meat of the article:

If you’re using procedural approaches to solve a query-based problem within a relational database, you’re probably doing something wrong!
A procedural approach is a solution that operates line-by-line, object-by-object. For-loops are a quintessential example of a procedural approach: instead of solving in one big operation, the procedural solution treats the problem as a series of small operations to be iterated out.
There are a few common cases where it makes sense to use for-loops in code that interacts with a relational database, such as performing a set of operations on each column from a list of column names, or re-running a model on various sets of training data. But once you venture outside things like this and you’re still using for-loops, be extremely careful.


Want to receive more content like this in your inbox?