Modern SQL
Regis Hideki Hattori
Introduction
Introduction
● DB2, SQL Server, Postgres and Oracle implement almost all of them
Introduction
● DB2, SQL Server, Postgres and Oracle implement almost all of them
● SQLite implements some of them
Introduction
● DB2, SQL Server, Postgres and Oracle implement almost all of them
● SQLite implements some of them
● MySQL implements none of them
Tables
performance_reviewsemployees
Filter
How many performance reviews per year?
Filter
How many performance reviews per year?
Filter
How many performance reviews per year?
“What are the last
performance review of
each employee?”
Last performance review of each employee
Last Performance Review by Employee
Last Performance Review by Employee
Last Performance Review by Employee
Last Performance Review by Employee
CTE (Common Table Expressions)
CTE
CTE
is like a private method
“What are the 2 last
performance reviews of
each employee?”
Last 2 performance reviews of each employee
What we need to change?
Lateral Join
Lateral Join
Lateral Join
Lateral Join
Lateral Join
It is like a foreach...
“We need a table with a
cumulative salary.”
Over (Window functions)
Over (Window functions)
Over (Window functions)
Over (Window functions)
“What is the percentage of
salary of each employee
by department?”
Over + Partition
Over + Partition
Over + Partition
Over + Partition
Over + Partition
Over + Partition
“I want a vision of our
hierarchy”
Hierarchy
Hierarchy
CTE recursive
CTE recursive
CTE recursive
CTE recursive
CTE recursive
CTE recursive
References
● https://modern-sql.com/
● http://stackoverflow.com/questions/3699395/find-parent-
recursively-using-query
● http://www.postgresql.org/docs/8.4/static/functions-
window.html
● https://www.youtube.com/watch?v=ogVka4KzE6s
Thanks

Modern sql