How to Write Multiset Conditions With Oracle VARRAY Types
Oracle is one of the few databases that implements the SQL standard ORDBMS extensions, which essentially allow for nested collections. Other databases that have these features to some extent are...
View ArticleFind the Next Non-NULL Row in a Series With SQL
I’ve stumbled across this fun SQL question on reddit, recently. The question was looking at a time series of data points where some events happened. For each event, we have the start time and the end...
View ArticleBeware of Hidden PL/SQL to SQL Context Switches
I recently stumbled upon a curious query on a customer’s productive Oracle database: SELECT USER FROM SYS.DUAL Two things caught my attention: The query was executed many billions of times per month,...
View ArticleHow to Write a Multiplication Aggregate Function in SQL
Everyone knows the SQL SUM() aggregate function (and many people also know its window function variant). When querying the Sakila database, we can get the daily revenue (using PostgreSQL syntax): WITH...
View ArticleHow to Aggregate an Archive Log’s Deltas into a Snapshot with SQL
A customer of my popular SQL training (which you should book!) has recently challenged me to optimise a hierarchical query that merges an archive log’s deltas in order to obtain a snapshot of some...
View ArticleHow to Work Around ORA-38104: Columns referenced in the ON Clause cannot be...
Standard SQL is a beautiful language. Vendor specific implementations, however, have their warts. In Oracle, for example, it’s not possible to update any columns in a MERGE statement, which have been...
View ArticleCalculate Percentiles to Learn About Data Set Skew in SQL
B-Tree indexes are perfect when your data is uniformly distributed. They are not really useful, when you have skewed data. I’ll explain later why this is the case, but let’s first learn how to detect...
View ArticleHow to Emulate PERCENTILE_DISC in MySQL and Other RDBMS
In my previous article, I showed what the very useful percentile functions (also known as inverse distribution functions) can be used for. Unfortunately, these functions are not ubiquitously available...
View ArticleHow to Calculate a Cumulative Percentage in SQL
A fun report to write is to calculate a cumulative percentage. For example, when querying the Sakila database, we might want to calculate the percentage of our total revenue at any given date. The...
View ArticleCalculating Weighted Averages When Joining Tables in SQL
I stumbled upon a very interesting jOOQ question on Stack Overflow that required the calculation of a weighted average. Why is that. Problem description Assuming you have this database (using...
View ArticleThe Cost of Useless Surrogate Keys in Relationship Tables
What’s a good natural key? This is a very difficult question for most entities when you design your schema. In some rare cases, there seems to be an “obvious” candidate, such as a variety of ISO...
View ArticleThe Difference Between SQL’s JOIN .. ON Clause and the Where Clause
A question that is frequently occurring among my SQL training‘s participants is: What’s the difference between putting a predicate in the JOIN .. ON clause and the WHERE clause? I can definitely see...
View ArticleUsing IGNORE NULLS With SQL Window Functions to Fill Gaps
I found a very interesting SQL question on Twitter recently: Hi @sfonplsql we have some scenario, Let us 01Jan Mkt Value 100, 02Jan 120, next entry available 25th Jan 125, from 3rd Jan 24 Jan, our...
View ArticleHow to Fetch All Current Identity Values in Oracle
Oracle 12c has introduced the useful SQL standard IDENTITY feature, which is essentially just syntax sugar for binding a sequence to a column default. We can use it like this: create table t1 (col1...
View ArticleQuantified LIKE ANY predicates in jOOQ 3.12
Quantified comparison predicates One of SQL’s weirdes features are quantified comparison predicates. I’ve hardly ever seen these in the wild: SELECT * FROM t WHERE id = ANY (1, 2, 3) The above example...
View ArticleUsing DISTINCT ON in Non-PostgreSQL Databases
A nice little gem in PostgreSQL’s SQL syntax is the DISTINCT ON clause, which is as powerful as it is esoteric. In a previous post, we’ve blogged about some caveats to think of when DISTINCT and ORDER...
View ArticleOracle’s BINARY_DOUBLE Can Be Much Faster Than NUMBER
Using the right data type for some calculation sounds like some obvious advice. There are many blogs about using temporal data types for temporal data, instead of strings. An obvious reason is data...
View ArticleWhat’s Faster? COUNT(*) or COUNT(1)?
One of the biggest and undead myths in SQL is that COUNT(*) is faster than COUNT(1). Or was it that COUNT(1) is faster than COUNT(*)? Impossible to remember, because there’s really no reason at all why...
View ArticleHow to Map MySQL’s TINYINT(1) to Boolean in jOOQ
MySQL 8 does not yet support the BOOLEAN type as specified in the SQL standard. There is a DDL “type” called BOOL, which is just an alias for TINYINT: create table t(b bool); select table_name,...
View ArticleDogfooding in Product Development
Dogfooding, or eating your own dog food, is a practice that all product developers should implement all the time. According to wikipedia: Dogfooding, occurs when an organization uses its own product....
View Article