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

Selecting all Columns Except One in PostgreSQL

Google's BigQuery has a very interesting SQL language feature, which I've missed many times in other databases: select: SELECT [{ ALL | DISTINCT }] { [ expression. ]* [ EXCEPT ( column_name [, ...] ) ] [ REPLACE ( expression [ AS ] column_name [, ...] ) ] | expression [ [ AS ] alias … Continue reading Selecting all Columns Except One in PostgreSQL

How to Write Efficient TOP N Queries in SQL

A very common type of SQL query is the TOP-N query, where we need the "TOP N" records ordered by some value, possibly per category. In this blog post, we're going to look into a variety of different aspects to this problem, as well as how to solve them with standard and non-standard SQL. These … Continue reading How to Write Efficient TOP N Queries in SQL

How to Find the Closest Subset Sum with SQL

I've stumbled upon this very interesting question on Stack Overflow, recently. Its title is: [How to] compare a number with sum of subset of numbers In this article, we'll compare the user's imperative approach to the extremely elegant (Oracle) SQL approach. We'll be making use of any combination of these awesome SQL features: Window functions … Continue reading How to Find the Closest Subset Sum with SQL

jOOQ Newsletter: December 30, 2013. Happy New Year!

subscribe to the newsletter here Tweet of the Day We would like to contribute this new section of the newsletter to our followers, users, and customers. Here's Andy Van Den Heuvel, who appreciates jOOQ's and MyBatis' (both being "post-JPA" frameworks) return to SQL. https://twitter.com/andyvdh7/status/416685921288093696 2013 from jOOQ's perspective 2013 was a very exciting year for jOOQ and jOOQ customers. … Continue reading jOOQ Newsletter: December 30, 2013. Happy New Year!

Add LATERAL Joins or CROSS APPLY to Your SQL Tool Chain

The T-SQL dialect has known the powerful CROSS APPLY and OUTER APPLY JOIN syntaxes for ages. The SQL:1999 standard had introduced almost equivalent "lateral derived tables", which are finally supported with PostgreSQL 9.3, or Oracle 12c, which has adopted both the SQL standard LATERAL syntax and the T-SQL vendor-specific CROSS APPLY and OUTER APPLY syntaxes. … Continue reading Add LATERAL Joins or CROSS APPLY to Your SQL Tool Chain

Oracle 12c Goodies: { CROSS | OUTER } APPLY

I can't believe my eyes. Has this been openly communicated by Oracle? I haven't seen too many blog posts on that matter. Apart from introducing the awesome SQL Standard OFFSET .. FETCH clause (just like SQL Server 2012), there seems to be now also { CROSS | OUTER } APPLY in Oracle 12c! Check out the … Continue reading Oracle 12c Goodies: { CROSS | OUTER } APPLY