PostgreSQL’s lateral joins have a lot of uses. One of my favorites is the ability to reuse calculations in a query. Before I discovered lateral joins, I would either copy calculations throughout the query or use subqueries. Neither of these approaches is ideal because they make the query much more difficult to read—and nearly impossible […]
I was recently tasked with writing a bit of code to find delivery route stops where nothing has to be unloaded. This code is intended to prevent a delivery driver from stopping at a store when there’s nothing in the truck for that customer that day.
Our team recently found itself in a situation where we needed to write a database query to: Union several tables together Filter out some rows Sort the unioned set different ways This was going to be a very expensive and slow query.