SQLite in 2018 - A State of The Art SQL Dialect
SQLite in 2018 - A State of The Art SQL Dialect
html
Window functions, filter clause, upsert, and more. SQLite got some cool new features in 2018! Read online
The SQL dialect of SQLite is also very strong. For example, it introduced the
with clause four years before MySQL did. Lately, it has even added window
functions—just five months after MySQL did the same.
This article covers the SQL enhancements that were added to SQLite in 2018,
i.e. the new SQL features introduced in versions 3.22.0 through 3.26.0.
Starting with release 3.23.0, SQLite understands the keywords true and
false as synonyms for the values 1 and 0 and supports the is [not]
true|false test. The keyword unknown is generally not supported. You can
use null instead because the values unknown and null are the same for
Boolean values.
The literals true and false can greatly improve the readability of values
and set clauses in insert and update statements.
1 of 8 3/12/2021, 5:49 PM
SQLite in 2018: A state of the art SQL dialect file:///C:/Users/stadimet/AppData/Local/Temp/bat/Message.html
vs.
If c is the null value, the result of the condition c <> false is unknown. As
the where clause only accepts true values, but rejects false as well as
unknown values, it will remove those rows from the result.
In contrast, the result of c is not false is also true if c is the null value.
The second where clause will thus also accept rows where c has the null
value.
Another way to select the same rows is to accept the null case separately.
On my Own Behalf
I make my living from training and other SQL related services.
The support of Boolean literals and Boolean tests in SQLite is now close to
that of other open source databases. The only gap is that SQLite does not
support the is [not] unknown test (use is [not] null instead).
Interestingly, these features are generally not available in the commercial
products shown below.
2 of 8 3/12/2021, 5:49 PM
SQLite in 2018: A state of the art SQL dialect file:///C:/Users/stadimet/AppData/Local/Temp/bat/Message.html
Window Functions
SQLite 3.25.0 introduces window functions. If you know window functions, you
also know that this is a big deal. If you don’t know window functions, learn how
to use them! This article is not the right place to explain window functions, but
trust me: it is the most important “modern” SQL feature at all.
SQLite’s support of the over clause is pretty close to that of other databases.
The only notable limitation is that range frames don’t support numeric or
interval distances (only current row and unbounded
preceding|following). This is the same limitation SQL Server has, and
PostgreSQL had at the time when SQLite 3.25.0 was released. In the
meantime, PostgreSQL 11 has lifted this limitation.
The set of window function supported by SQLite is pretty much at a state of the
art level. The main omissions (distinct in aggregates, width_bucket,
respect|ignore nulls, and from first|last) are also missing in
3 of 8 3/12/2021, 5:49 PM
SQLite in 2018: A state of the art SQL dialect file:///C:/Users/stadimet/AppData/Local/Temp/bat/Message.html
Filter Clause
Even though the filter clause is just syntax sugar—you can easily use
case expressions for the same result—I think it’s essential syntax sugar
because it makes learning and understanding a lot easier.
Just look at the following select clauses. Which one is easier to understand?
4 of 8 3/12/2021, 5:49 PM
SQLite in 2018: A state of the art SQL dialect file:///C:/Users/stadimet/AppData/Local/Temp/bat/Message.html
vs.
This example pretty much summarizes what the filter clause does: it is a
suffix for aggregate functions that conditionally removes rows before the
aggregation. The pivot technique is the most common use case of the filter
clause. That includes transforming attributes from the entity-attribute-value
(EAV) model into columns. Learn more about it in “filter — Selective
Aggregates”.
SQLite 3.25.0 introduced the filter clause for aggregate functions that use
the over clause—not for aggregates that use group by. Unfortunately, that
means that you still cannot use filter for the above mentioned use cases in
SQLite. You have to revert to case expression as before. I truly hope that
changes soon.
This is a proprietary SQL extensions, i.e. it is not part of the SQL standard and
therefore gray in the matrix below. However, SQLite uses the same syntax as
PostgreSQL for this feature. 0 The standard offers the merge statement for this
and other use cases.
According to the documentation, the problem is that “the parser does not know
if the token "ON" is part of a join constraint on the SELECT, or the beginning of
the upsert-clause.” This can be resolved by adding another clause to the
query, e.g. where true.
Rename Column
Another proprietary feature that SQLite introduced is the ability to rename
columns in base tables. 1 The SQL standard does not offer such functionality. 2
6 of 8 3/12/2021, 5:49 PM
SQLite in 2018: A state of the art SQL dialect file:///C:/Users/stadimet/AppData/Local/Temp/bat/Message.html
Other News
Besides the SQL changes, there were also some API changes in SQLite in
2018. Please refer to the news section on sqlite.com for all details.
If you’d like to learn more about modern SQL, have a look at my training in
Vienna. Besides window functions (mentioned above) the training covers
recursion, indexing, and greatly improves your understanding of the basic SQL
concepts. The training is based on the current draft of my next book. Take a
look now!
Footnotes
0 SQLite often follows the PostgreSQL syntax. Richard Hipp has referred to this as What Would PostgreSQL Do (WWPD).
1 Base tables are those tables created using create table. Columns in a derived table (e.g. result of select) can be renamed in the
select clause, the from clause, or via with.
7 of 8 3/12/2021, 5:49 PM
SQLite in 2018: A state of the art SQL dialect file:///C:/Users/stadimet/AppData/Local/Temp/bat/Message.html
You received this message because you subscribed to my “ Modern SQL Blog ”. Would you like to manage your subscription?
Copyright © 2019 • Markus Winand • Maderspergerstr. 1-3/9/11 • 1160 Vienna • AUSTRIA • About
8 of 8 3/12/2021, 5:49 PM