more than a query language: sql in the 21 century€¦ · is like saying that an intercontinental...

145
More Than a Query Language: SQL in the 21 st Century @MarkusWinand • @ModernSQL http://www.almaden.ibm.com/cs/people/chamberlin/sequel-1974.pdf

Upload: others

Post on 30-Apr-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

More Than a Query Language: SQL in the 21st Century

@MarkusWinand • @ModernSQL

http://www.almaden.ibm.com/cs/people/chamberlin/sequel-1974.pdf

Page 2: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

More Than a Query Language: SQL in the 21st Century

@MarkusWinand • @ModernSQL

http://www.almaden.ibm.com/cs/people/chamberlin/sequel-1974.pdf

Page 3: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99
Page 4: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1974 1992

Page 5: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Page 6: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

Page 7: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

Atom image: https://commons.wikimedia.org/wiki/File:Stylised_atom_with_three_Bohr_model_orbits_and_stylised_nucleus.png

Page 8: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

A B C

Atom image: https://commons.wikimedia.org/wiki/File:Stylised_atom_with_three_Bohr_model_orbits_and_stylised_nucleus.png

Page 9: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

A B C

Page 10: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

‣Schema independent ofprocessing purposes ‣ “Normalization”

A B C

Page 11: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

‣Schema independent ofprocessing purposes ‣ “Normalization”

A B C C D B E

Page 12: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

‣Schema independent ofprocessing purposes ‣ “Normalization”

Relational Operations ‣Transform data for

each particularprocessing purposes ‣JOIN, UNION, nesting, …

A B C C D B E A B C D E

Page 13: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

‣Schema independent ofprocessing purposes ‣ “Normalization”

Relational Operations ‣Transform data for

each particularprocessing purposes ‣JOIN, UNION, nesting, …

A B C C D B EA B C D E

A B E

Page 14: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

‣Schema independent ofprocessing purposes ‣ “Normalization”

Relational Operations ‣Transform data for

each particularprocessing purposes ‣JOIN, UNION, nesting, …

A B C C D B EA B C D E

A B E

C D E

Page 15: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL-92 — Tied to the Relational Idea

Relational Data Model‣ “Atomic” types (domain)

‣Schema independent ofprocessing purposes ‣ “Normalization”

Relational Operations ‣Transform data for

each particularprocessing purposes ‣JOIN, UNION, nesting, …

A B C C D B EA B C D E

A B E

C D E

Page 16: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1992 1999

Page 17: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

https://www.wiscorp.com/DBMS_-_GreatNews-TheRelationalModelIsDead_-_paper_-_sam.pdf

Page 18: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Escaping the Relational Cage

To say that these SQL:1999 extensions are mere “extended interpretations” of the relational data modelis like saying that an intercontinental ballistic missile is

merely an “extended interpretation” of a spear.

https://www.wiscorp.com/DBMS_-_GreatNews-TheRelationalModelIsDead_-_paper_-_sam.pdf

Page 19: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Escaping the Relational Cage

To say that these SQL:1999 extensions are mere “extended interpretations” of the relational data modelis like saying that an intercontinental ballistic missile is

merely an “extended interpretation” of a spear.

With SQL/99 you can get the best of both worlds and

of course, you can get the worst of both worlds.It’s up to the database practitioners to do the right thing.

https://www.wiscorp.com/DBMS_-_GreatNews-TheRelationalModelIsDead_-_paper_-_sam.pdf

Page 20: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Escaping the Relational Cage

Page 21: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?

SQL:1999 — Escaping the Relational Cage

Page 22: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

I was as confused as anyone else

Page 23: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

?I was as confused as anyone else

Page 24: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

?I was as confused as anyone else By the early 1990s, however,

I’d seen the light

Page 25: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

?I was as confused as anyone else By the early 1990s, however,

I’d seen the lightDomains Can Contain Anything!

Page 26: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?‣ Introduced rich types

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

?I was as confused as anyone else By the early 1990s, however,

I’d seen the lightDomains Can Contain Anything!

Page 27: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?‣ Introduced rich types

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational CageA

?I was as confused as anyone else By the early 1990s, however,

I’d seen the lightDomains Can Contain Anything!

Page 28: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?‣ Introduced rich types‣arrays

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational CageA B

[ , ][ ][]

?I was as confused as anyone else By the early 1990s, however,

I’d seen the lightDomains Can Contain Anything!

Page 29: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?‣ Introduced rich types‣arrays‣Nested tables (multiset)

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational CageA B

[ , ][ ][]

CC D

C D

C D

?I was as confused as anyone else By the early 1990s, however,

I’d seen the lightDomains Can Contain Anything!

Page 30: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?‣ Introduced rich types‣arrays‣Nested tables (multiset)

‣composite types (objects)

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational CageA B C D

[ , ] {x: , y: }

[ ] {x: , y: }

[] {x: , y: }

C D

C D

C D

?I was as confused as anyone else By the early 1990s, however,

I’d seen the lightDomains Can Contain Anything!

Page 31: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Relational Model?‣ Introduced rich types‣arrays‣Nested tables (multiset)

‣composite types (objects)

Non-Relational Operations ‣ Introduced recursive

queries that processtheir own output ‣Transitive closure

Chris DateDate on Database: Writings 2000-2006

SQL:1999 — Escaping the Relational Cage

?I was as confused as anyone else By the early 1990s, however,

I’d seen the lightDomains Can Contain Anything!

Page 32: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion CREATETABLEt(idINTEGER,parentINTEGER,)

Page 33: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion CREATETABLEt(idINTEGER,parentINTEGER,)

Page 34: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion CREATETABLEt(idINTEGER,parentINTEGER,)

Page 35: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

Page 36: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

Page 37: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

SELECTt.id,t.parentFROMtWHEREt.id=?

Page 38: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

SELECTt.id,t.parentFROMtWHEREt.id=?UNIONALLSELECTt.id,t.parentFROMtWHEREt.parent=?

Page 39: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

SELECTt.id,t.parentFROMtWHEREt.id=?UNIONALLSELECTt.id,t.parentFROMtWHEREt.parent=?

Page 40: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

SELECTt.id,t.parentFROMtWHEREt.id=?UNIONALLSELECTt.id,t.parentFROMtWHEREt.parent=?

Page 41: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

WITHRECURSIVEprev(id,parent)AS(

)

SELECTt.id,t.parentFROMtWHEREt.id=?UNIONALLSELECTt.id,t.parentFROMtJOINprevONt.parent=prev.id

SELECT*FROMprev

Page 42: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

WITHRECURSIVEprev(id,parent)AS(

)

SELECTt.id,t.parentFROMtWHEREt.id=?UNIONALLSELECTt.id,t.parentFROMtJOINprevONt.parent=prev.id

SELECT*FROMprev

Page 43: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

WITHRECURSIVEprev(id,parent)AS(

)

SELECTt.id,t.parentFROMtWHEREt.id=?UNIONALLSELECTt.id,t.parentFROMtJOINprevONt.parent=prev.id

SELECT*FROMprev

Page 44: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — Recursion

1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 10.2 MariaDB8.0 MySQL

8.4 PostgreSQL1.0 3.8.3 SQLite

7.0 DB2 LUW11gR2 Oracle

2005 SQL Server

Page 45: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99
Page 46: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

LATERAL

Page 47: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

Select-list sub-queries must be scalar[0]:

SELECT…,(SELECTcolumn_1FROMt1WHEREt1.x=t2.y)AScFROMt2…

(return no more than one domain value)

Page 48: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

Select-list sub-queries must be scalar[0]:

SELECT…,(SELECTcolumn_1FROMt1WHEREt1.x=t2.y)AScFROMt2…

(return no more than one domain value)

✗,column_2

More thanone column? ⇒Syntax error

Page 49: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

Select-list sub-queries must be scalar[0]:

SELECT…,(SELECTcolumn_1FROMt1WHEREt1.x=t2.y)AScFROMt2…

(return no more than one domain value)

✗,column_2

More thanone column? ⇒Syntax error

}More thanone row?

⇒Runtime error!

Page 50: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

Lateral derived tables lift both limitations and can be correlated:

SELECT…,ldt.*FROMt2CROSSJOINLATERAL(SELECTcolumn_1,column_2FROMt1WHEREt1.x=t2.y)ASldt…

Page 51: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

Lateral derived tables lift both limitations and can be correlated:

SELECT…,ldt.*FROMt2CROSSJOINLATERAL(SELECTcolumn_1,column_2FROMt1WHEREt1.x=t2.y)ASldt…

“Derived table” meansit’s in the

FROM/JOIN clause

Page 52: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

Lateral derived tables lift both limitations and can be correlated:

SELECT…,ldt.*FROMt2CROSSJOINLATERAL(SELECTcolumn_1,column_2FROMt1WHEREt1.x=t2.y)ASldt…

“Derived table” meansit’s in the

FROM/JOIN clause

Still “correlated”

Page 53: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

Lateral derived tables lift both limitations and can be correlated:

SELECT…,ldt.*FROMt2CROSSJOINLATERAL(SELECTcolumn_1,column_2FROMt1WHEREt1.x=t2.y)ASldt…

“Derived table” meansit’s in the

FROM/JOIN clause

Still “correlated”

Regular join semantics

Page 54: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

Page 55: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

‣ Top-N per group

inside a lateral derived tableFETCHFIRST (or LIMIT, TOP) applies per row from left tables.

Page 56: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

‣ Top-N per group

inside a lateral derived tableFETCHFIRST (or LIMIT, TOP) applies per row from left tables.

FROMtJOINLATERAL(SELECT…FROM…WHEREt.c=…ORDERBY…LIMIT10)derived_table

Page 57: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

‣ Top-N per group

inside a lateral derived tableFETCHFIRST (or LIMIT, TOP) applies per row from left tables.

Add proper indexfor Top-N query

https://use-the-index-luke.com/sql/partial-results/top-n-queries

FROMtJOINLATERAL(SELECT…FROM…WHEREt.c=…ORDERBY…LIMIT10)derived_table

Page 58: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

‣ Top-N per group

inside a lateral derived tableFETCHFIRST (or LIMIT, TOP) applies per row from left tables.

‣ Also useful to find most recent news from several subscribed topics (“multi-source top-N”).

Add proper indexfor Top-N query

https://use-the-index-luke.com/sql/partial-results/top-n-queries

FROMtJOINLATERAL(SELECT…FROM…WHEREt.c=…ORDERBY…LIMIT10)derived_table

Page 59: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL

‣ Top-N per group

inside a lateral derived tableFETCHFIRST (or LIMIT, TOP) applies per row from left tables.

‣ Also useful to find most recent news from several subscribed topics (“multi-source top-N”).

‣ Table function arguments

(TABLE often implies LATERAL)

Add proper indexfor Top-N query

https://use-the-index-luke.com/sql/partial-results/top-n-queries

FROMtJOINTABLE(your_func(t.c))

FROMtJOINLATERAL(SELECT…FROM…WHEREt.c=…ORDERBY…LIMIT10)derived_table

Page 60: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 MariaDB8.0.14 MySQL

9.3 PostgreSQL1.0 SQLite

9.1 DB2 LUW11gR1[0] 12cR1 Oracle

2005[1] SQL Server[0]Undocumented. Requires setting trace event 22829.[1]LATERAL is not supported as of SQL Server 2016 but [CROSS|OUTER]APPLY can be used for the same effect.

Page 61: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:1999 — LATERAL1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 MariaDB8.0.14 MySQL

9.3 PostgreSQL1.0 SQLite

9.1 DB2 LUW11gR1[0] 12cR1 Oracle

2005[1] SQL Server[0]Undocumented. Requires setting trace event 22829.[1]LATERAL is not supported as of SQL Server 2016 but [CROSS|OUTER]APPLY can be used for the same effect.

MDEV-6373: Fix Version: none

Page 62: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1999 2003

Page 63: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

http://www.acm.org:80/sigmod/record/issues/0206/standard.pdf (via Wayback machine)

Page 64: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2003 — Schemaless & Analytical

Schemaless ‣ Introduced XML ‣Non-uniform

documents ina single column

Page 65: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2003 — Schemaless & Analytical

Schemaless ‣ Introduced XML ‣Non-uniform

documents ina single column

Later: ‣ JSON added with SQL:2016 ‣ Proprietary JSON support: ‣ 2012: PostgreSQL ‣ 2014: Oracle ‣ 2015: MySQL ‣ 2016: SQL Server

Page 66: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2003 — Schemaless & Analytical

Analytical ‣ Introduced

window functions ‣Accessing other rows

of the current result

Schemaless ‣ Introduced XML ‣Non-uniform

documents ina single column

Later: ‣ JSON added with SQL:2016 ‣ Proprietary JSON support: ‣ 2012: PostgreSQL ‣ 2014: Oracle ‣ 2015: MySQL ‣ 2016: SQL Server

Page 67: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2003 — Schemaless & Analytical

Analytical ‣ Introduced

window functions ‣Accessing other rows

of the current result

Schemaless ‣ Introduced XML ‣Non-uniform

documents ina single column

Later: ‣ Extended in SQL:2011 ‣ Popular among “New SQLs” ‣ 2013: BigQuery, Hive ‣ 2014: Impala ‣ 2015: Spark SQL ‣ 2016: NuoDB, MemSQL,

Cockroach DB, VoltDB

Later: ‣ JSON added with SQL:2016 ‣ Proprietary JSON support: ‣ 2012: PostgreSQL ‣ 2014: Oracle ‣ 2015: MySQL ‣ 2016: SQL Server

Page 68: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

SQL:2003 — Analytical

Page 69: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

Page 70: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

+10

Page 71: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

+10

+30

+20

+70

+40

+20

Page 72: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 73: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

ORDERBYid

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 74: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

ORDERBYid

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 75: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDING

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 76: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDING

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 77: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 78: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 79: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id

1

2

3

4

5

6

value

+10

+20

-10

+50

-30

-20

bal

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 80: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id value bal

1 +10

2 +20

3 -10

4 +50

5 -30

6 -20

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 81: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id value bal

1 +10

2 +20

3 -10

4 +50

5 -30

6 -20

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 82: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id value bal

1 +10

2 +20

3 -10

4 +50

5 -30

6 -20

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 83: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id value bal

1 +10

2 +20

3 -10

4 +50

5 -30

6 -20

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 84: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTid,value

FROMt

id value bal

1 +10

2 +20

3 -10

4 +50

5 -30

6 -20

SQL:2003 — Analytical

ORDERBYidROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW

,SUM(value)OVER(

)bal

+10

+30

+20

+70

+40

+20

Page 85: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2003 — Analytical1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 10.2 MariaDB8.0 MySQL

8.4 PostgreSQL1.0 3.25.0 SQLite

7.0 DB2 LUW8i Oracle

2005[0] 2012 SQL Server[0]Without framing

Page 86: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

Inverse Distribution Functions (percentiles)

Page 87: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2003 — Analytical (Median)

Page 88: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTd1.valFROMdatad1JOINdatad2ON(d1.val<d2.valOR(d1.val=d2.valANDd1.id<d2.id))GROUPBYd1.valHAVINGcount(*)=(SELECTFLOOR(COUNT(*)/2)FROMdatad3)

SQL:2003 — Analytical (Median)

Page 89: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTd1.valFROMdatad1JOINdatad2ON(d1.val<d2.valOR(d1.val=d2.valANDd1.id<d2.id))GROUPBYd1.valHAVINGcount(*)=(SELECTFLOOR(COUNT(*)/2)FROMdatad3)

Number rows

SQL:2003 — Analytical (Median)

Page 90: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTd1.valFROMdatad1JOINdatad2ON(d1.val<d2.valOR(d1.val=d2.valANDd1.id<d2.id))GROUPBYd1.valHAVINGcount(*)=(SELECTFLOOR(COUNT(*)/2)FROMdatad3)

Number rows

Pick middle one

SQL:2003 — Analytical (Median)

Page 91: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTd1.valFROMdatad1JOINdatad2ON(d1.val<d2.valOR(d1.val=d2.valANDd1.id<d2.id))GROUPBYd1.valHAVINGcount(*)=(SELECTFLOOR(COUNT(*)/2)FROMdatad3)

Number rows

Pick middle one

SQL:2003 — Analytical (Median)

Page 92: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTd1.valFROMdatad1JOINdatad2ON(d1.val<d2.valOR(d1.val=d2.valANDd1.id<d2.id))GROUPBYd1.valHAVINGcount(*)=(SELECTFLOOR(COUNT(*)/2)FROMdatad3)

Number rows

Pick middle one

SQL:2003 — Analytical (Median)

Page 93: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTd1.valFROMdatad1JOINdatad2ON(d1.val<d2.valOR(d1.val=d2.valANDd1.id<d2.id))GROUPBYd1.valHAVINGcount(*)=(SELECTFLOOR(COUNT(*)/2)FROMdatad3)

Number rows

Pick middle one

SQL:2003 — Analytical (Median)Notice

All employees mustwash hands

after usingself-joins

Page 94: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTd1.valFROMdatad1JOINdatad2ON(d1.val<d2.valOR(d1.val=d2.valANDd1.id<d2.id))GROUPBYd1.valHAVINGcount(*)=(SELECTFLOOR(COUNT(*)/2)FROMdatad3)

Number rows

Pick middle one

SQL:2003 — Analytical (Median)Notice

All employees mustwash hands

after usingself-joins

Grab stickersand coasters!

Page 95: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2003 — Analytical (Median)

Page 96: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

SQL:2003 — Analytical (Median)

Page 97: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

MedianSQL:2003 — Analytical (Median)

Page 98: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

Median

Which value?

SQL:2003 — Analytical (Median)

Page 99: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

SQL:2003 — Analytical (Median)

Page 100: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

Two variants:

‣for discrete values(categories)

‣for continuous values(linear interpolation)

SQL:2003 — Analytical (Median)

Page 101: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1

2

3

4

0 0.25 0.5 0.75 11

2

3

4

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

Two variants:

‣for discrete values(categories)

‣for continuous values(linear interpolation)

SQL:2003 — Analytical (Median)

Page 102: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1

2

3

4

0 0.25 0.5 0.75 11

2

3

4

0 0.25 0.5 0.75 11

2

3

4

PERCENTILE_DISC

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

Two variants:

‣for discrete values(categories)

‣for continuous values(linear interpolation)

SQL:2003 — Analytical (Median)

Page 103: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1

2

3

4

0 0.25 0.5 0.75 11

2

3

4

0 0.25 0.5 0.75 11

2

3

4

PERCENTILE_DISC0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_DISC

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

Two variants:

‣for discrete values(categories)

‣for continuous values(linear interpolation)

SQL:2003 — Analytical (Median)

Page 104: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1

2

3

4

0 0.25 0.5 0.75 11

2

3

4

0 0.25 0.5 0.75 11

2

3

4

PERCENTILE_DISC0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_DISC0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_DISC(0.5)

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

Two variants:

‣for discrete values(categories)

‣for continuous values(linear interpolation)

SQL:2003 — Analytical (Median)

Page 105: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1

2

3

4

0 0.25 0.5 0.75 11

2

3

4

0 0.25 0.5 0.75 11

2

3

4

PERCENTILE_DISC0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_DISC0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_DISC(0.5)0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_CONT

PERCENTILE_DISC(0.5)

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

Two variants:

‣for discrete values(categories)

‣for continuous values(linear interpolation)

SQL:2003 — Analytical (Median)

Page 106: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

1

2

3

4

0 0.25 0.5 0.75 11

2

3

4

0 0.25 0.5 0.75 11

2

3

4

PERCENTILE_DISC0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_DISC0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_DISC(0.5)0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_CONT

PERCENTILE_DISC(0.5)0 0.25 0.5 0.75 1

1

2

3

4

PERCENTILE_CONT(0.5)

PERCENTILE_DISC(0.5)

SELECTPERCENTILE_DISC(0.5)WITHINGROUP(ORDERBYval)FROMdata

Two variants:

‣for discrete values(categories)

‣for continuous values(linear interpolation)

SQL:2003 — Analytical (Median)

Page 107: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2003 — PERCENTILE_DISC1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 10.3.7[0] MariaDBMySQL

9.4 PostgreSQL1.0 SQLite

DB2 LUW9iR1 Oracle

2012[0] SQL Server[0]Only as window function (OVER required).

Page 108: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

2003 2016

Page 109: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2016 — JSON

http://standards.iso.org/ittf/PubliclyAvailableStandards/c067367_ISO_IEC_TR_19075-6_2017.zip

Page 110: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2016 — JSON

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

http://standards.iso.org/ittf/PubliclyAvailableStandards/c067367_ISO_IEC_TR_19075-6_2017.zip

Page 111: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2016 — JSON

id a1

42 foo

43 bar

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

http://standards.iso.org/ittf/PubliclyAvailableStandards/c067367_ISO_IEC_TR_19075-6_2017.zip

Page 112: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECT*FROMJSON_TABLE(?,'$[*]'COLUMNS(idINTPATH'$.id',a1VARCHAR(…)PATH'$.a1'))r

SQL:2016 — JSON_TABLE

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

id a142 foo43 bar

Page 113: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECT*FROMJSON_TABLE(?,'$[*]'COLUMNS(idINTPATH'$.id',a1VARCHAR(…)PATH'$.a1'))r

SQL:2016 — JSON_TABLE

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

id a142 foo43 bar

Bind Parameter

Page 114: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECT*FROMJSON_TABLE(?,'$[*]'COLUMNS(idINTPATH'$.id',a1VARCHAR(…)PATH'$.a1'))r

SQL:2016 — JSON_TABLE

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

id a142 foo43 bar

SQL/JSON Path ‣ Query language to

select elements from a JSON document ‣Defined in the

SQL standardBind

Parameter

Page 115: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECT*FROMJSON_TABLE(?,'$[*]'COLUMNS(idINTPATH'$.id',a1VARCHAR(…)PATH'$.a1'))r

SQL:2016 — JSON_TABLE

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

id a142 foo43 bar

SQL/JSON Path ‣ Query language to

select elements from a JSON document ‣Defined in the

SQL standardBind

Parameter

Page 116: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SELECT*FROMJSON_TABLE(?,'$[*]'COLUMNS(idINTPATH'$.id',a1VARCHAR(…)PATH'$.a1'))r

SQL:2016 — JSON_TABLE

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

id a142 foo43 bar

SQL/JSON Path ‣ Query language to

select elements from a JSON document ‣Defined in the

SQL standardBind

Parameter

Page 117: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2016 — JSON_TABLE — Use Case

SELECT*FROMJSON_TABLE(?,'$[*]'COLUMNS(idINTPATH'$.id',a1VARCHAR(…)PATH'$.a1'))r

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

id a142 foo43 bar

Page 118: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2016 — JSON_TABLE — Use Case

SELECT*FROMJSON_TABLE(?,'$[*]'COLUMNS(idINTPATH'$.id',a1VARCHAR(…)PATH'$.a1'))r

INSERTINTOtarget_table

[{"id":42,"a1":"foo"},{"id":43,"a1":"bar"}]

id a142 foo43 bar

Page 119: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2016 — JSON_TABLE1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 MariaDB8.0 MySQL

PostgreSQL1.0 SQLite

11.1.4.4 DB2 LUW12cR1 Oracle

2016[0] SQL Server[0]OPENJSON provides similar functionality

Page 120: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2016 — JSON_TABLE1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 MariaDB8.0 MySQL

PostgreSQL1.0 SQLite

11.1.4.4 DB2 LUW12cR1 Oracle

2016[0] SQL Server[0]OPENJSON provides similar functionality

MDEV-17399: Fix Version: 10.5

Page 121: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

2011 2016

Page 122: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Time Travelling

http://cs.ulb.ac.be/public/_media/teaching/infoh415/tempfeaturessql2011.pdf

Page 123: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Time Travelling

Application Versioning ‣Dedicated syntax added ‣When did something

happen in thereal world?

http://cs.ulb.ac.be/public/_media/teaching/infoh415/tempfeaturessql2011.pdf

Page 124: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Time Travelling

Application Versioning ‣Dedicated syntax added ‣When did something

happen in thereal world?

New syntax (excerpt) ‣ FORPORTIONOF inUPDATE and DELETE ‣ WITHOUTOVERLAPS in UNIQUE

constraints & PRIMARYKEYS‣ [IMMEDIATELY]PRECEDES,OVERLAPS in WHERE,HAVING,…

Page 125: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Time Travelling

System Versioning ‣Fully automatic and

(almost) transparent ‣When did we learn

about something

Application Versioning ‣Dedicated syntax added ‣When did something

happen in thereal world?

New syntax (excerpt) ‣ FORPORTIONOF inUPDATE and DELETE ‣ WITHOUTOVERLAPS in UNIQUE

constraints & PRIMARYKEYS‣ [IMMEDIATELY]PRECEDES,OVERLAPS in WHERE,HAVING,…

Page 126: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Time Travelling

System Versioning ‣Fully automatic and

(almost) transparent ‣When did we learn

about something

Application Versioning ‣Dedicated syntax added ‣When did something

happen in thereal world?

New syntax (excerpt) ‣ FORPORTIONOF inUPDATE and DELETE ‣ WITHOUTOVERLAPS in UNIQUE

constraints & PRIMARYKEYS‣ [IMMEDIATELY]PRECEDES,OVERLAPS in WHERE,HAVING,…

Transparent changes, new syntax for queries ‣ INSERT, UPDATE & DELETE

use the system time automatically ‣ SELECT can use FORSYSTEM_TIMEASOF

Page 127: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System VersioningCREATETABLEt(...,fromTIMESTAMP(9)GENERATEDALWAYSASROWSTART,tillTIMESTAMP(9)GENERATEDALWAYSASROWEND

,PERIODFORSYSTEM_TIME(from,till))WITHSYSTEMVERSIONING

Page 128: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System Versioning

Page 129: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System VersioningINSERTINTOt(id,data)VALUES(1,'X')

id data from till1 X 10:00

Page 130: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System VersioningINSERTINTOt(id,data)VALUES(1,'X')

id data from till1 X 10:00

UPDATEtSETdata='Y'WHEREid=1

id data from till1 X 10:00 11:001 Y 11:00

Page 131: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System VersioningINSERTINTOt(id,data)VALUES(1,'X')

id data from till1 X 10:00

UPDATEtSETdata='Y'WHEREid=1

id data from till1 X 10:00 11:001 Y 11:00

DELETEFROMtWHEREid=1

id data from till1 X 10:00 11:001 Y 11:00 12:00

Page 132: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System Versioningid data from till1 X 10:00 11:001 Y 11:00 12:00

Page 133: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System Versioningid data from till1 X 10:00 11:001 Y 11:00 12:00

SELECT*FROMt id data from till

Page 134: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System Versioningid data from till1 X 10:00 11:001 Y 11:00 12:00

SELECT*FROMt id data from till

SELECT*FROMtFORSYSTEM_TIMEASOFTIMESTAMP'…10:30:00'

id data from till1 X 10:00 11:00

Page 135: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — System Versioning1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 10.3 MariaDBMySQLPostgreSQL

1.0 SQLite

10.1 DB2 LUW10gR1[0] 11gR1[1] Oracle

2016 SQL Server[0]Short term using Flashback.[1]Flashback Archive. Proprietery syntax.

Page 136: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Application Time Periods

Page 137: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Application Time Periods

CREATETABLEt(...,fromTIMESTAMP(9),tillTIMESTAMP(9),PERIODFORa(from,till))

Page 138: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Application Time Periods

ID Data From Till1 X 10:00:00 12:00:00

INSERTt(id,data,from,till) VALUES(1,'X','10:00:00','12:00:00')

Page 139: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Application Time Periods

UPDATEtFORPORTIONOFaFROM'10:30:00'TO'11:30:00'SETDATA='Y'

ID Data From Till1 X 10:00:00 10:30:001 Y 10:30:00 11:30:001 X 11:30:00 12:00:00

ID Data From Till1 X 10:00:00 12:00:00

INSERTt(id,data,from,till) VALUES(1,'X','10:00:00','12:00:00')

Page 140: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL:2011 — Application Time Periods1999

2001

2003

2005

2007

2009

2011

2013

2015

2017

5.1 10.4 MariaDBMySQLPostgreSQL

1.0 SQLite

10.5 DB2 LUWOracleSQL Server

Page 141: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

https://webstore.iec.ch/publication/59685

Page 142: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

A lot has happened

since SQL-92

Page 143: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL has evolved

beyond the relational idea

A lot has happened

since SQL-92

Page 144: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL has evolved

beyond the relational idea

If you use SQL for

CRUD operations only, you are doing it wrong

A lot has happened

since SQL-92

Page 145: More Than a Query Language: SQL in the 21 Century€¦ · is like saying that an intercontinental ballistic missile is merely an “extended interpretation” of a spear. With SQL/99

SQL has evolved

beyond the relational idea

If you use SQL for

CRUD operations only, you are doing it wrong

A lot has happened

since SQL-92

https://modern-sql.com@ModernSQL by @MarkusWinand

Training:https://winand.at/