LATERAL is Your Friend to Create Local Column Variables in SQL

The standard SQL WITH clause has been tremendously helpful in structuring SQL queries. Instead of nesting everything in unreadable derived tables like this: SELECT actor_id, name, COUNT(*) FROM ( SELECT actor_id, first_name || ' ' || last_name AS name FROM actor ) AS a JOIN film_actor AS fa USING (actor_id) GROUP BY actor_id, name ORDER … Continue reading LATERAL is Your Friend to Create Local Column Variables in SQL

How to Fetch Multiple Oracle Execution Plans in One Nice Query

When looking at execution plans in Oracle, we'll have to do several steps to be able to call the DBMS_XPLAN package functions. In fact, we have to find out the SQL_ID for a given statement first, and only then we can get its plan. I've blogged about this previously, here. However, thanks to lateral unnesting, … Continue reading How to Fetch Multiple Oracle Execution Plans in One Nice Query

Beautiful SQL: Lateral Unnesting of Array Columns

Sometimes, SQL can just be so beautiful. One of the less mainstream features in SQL is the array type (or nested collections). In fact, it's so not mainstream that only 2 major databases actually support it: Oracle and PostgreSQL (and HSQLDB and H2 in the Java ecosystem). In PostgreSQL, you can write: CREATE TABLE blogs … Continue reading Beautiful SQL: Lateral Unnesting of Array Columns