SlideShare a Scribd company logo
Advance SQL
Bring Sanity Back to SQL
Eyal Trabelsi
Agenda
● Execution Order
● Null
● SQL Sanity
● Spaghetti Query
● Additional Resources
Bring sanity back to sql (advance sql)
Execution order
Execution order
More detailed execution order
Bring sanity back to sql (advance sql)
Null Definition
Null Definition
● Null signifies an unknown value or that a
value does not exist.
Null Definition
● Null signifies an unknown value or that a
value does not exist.
● The predicate is null can be used to check
for Null values.
Null Definition
● Null signifies an unknown value or that a
value does not exist.
● The predicate is null can be used to check
for Null values.
SELECT <column_list>
FROM [tablenames]
WHERE <column_a> IS NULL
Null Weird Behavior
Null Weird Behavior
● 5 + Null returns ?
● 5 < Null returns ?
● Null = Null ?
● Null in (Null) returns ?
● Null and true?
● Null and false?
● Null or true ?
● Null or false?
Null Weird Behavior
● 5 + Null returns ? Null
● 5 < Null returns ? Null
● Null = Null ? Null != Null
● Null in (Null) returns ? Null because in use = operator
● Null and true? Null
● Null and false? False
● Null or true ? True
● Null or false? Null
Null Weird Behavior
● The result of any arithmetic expression involving null is null.
Null Weird Behavior
● The result of any arithmetic expression involving null is null.
● Any comparison with null returns unknown.
Null Weird Behavior
● The result of any arithmetic expression involving null is null.
● Any comparison with null returns unknown.
● All aggregate operations except count(*) ignore tuples with null
values on the aggregated attributes.
Null Weird Behavior
● The result of any arithmetic expression involving null is null.
● Any comparison with null returns unknown.
● All aggregate operations except count(*) ignore tuples with null
values on the aggregated attributes.
● logic inference
Bring sanity back to sql (advance sql)
Null Weird Behavior- Problem Example
Null Weird Behavior- Problem Example
a c
c a
d b
a b
NULL a
c b
T2T1
Null Weird Behavior- Problem Example
a c
c a
d b
a b
NULL a
c b
T2T1
Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is
the fk)
Null Weird Behavior- Solution 1
a c
c a
d b
a b
NULL a
c b
T2T1
Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is
the fk)SELECT *
FROM T1
WHERE a NOT IN (SELECT a FROM T2)
Null Weird Behavior- Solution 1
a c
c a
d b
a b
NULL a
c b
T2T1
Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is
the fk)SELECT *
FROM T1
WHERE a NOT IN (SELECT a FROM T2)
Is it correct ?
Null Weird Behavior- Solution 1
a c
c a
d b
a b
NULL a
c b
T2T1
Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is
the fk)SELECT *
FROM T1
WHERE a NOT IN (SELECT a FROM T2)
No it will retrieve no rows!!
Null Weird Behavior- Solution 2
a c
c a
d b
a b
NULL a
c b
T2T1
Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is
the fk)SELECT T1.*
FROM T1
LEFT JOIN T2 ON T1.a = T2.a
WHERE T2.a IS NULL
Null Weird Behavior- Solution 2
a c
c a
d b
a b
NULL a
c b
T2T1
Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is
the fk)SELECT T1.*
FROM T1
LEFT JOIN T2 ON T1.a = T2.a
WHERE T2.a IS NULL
Is it correct ?
Bring sanity back to sql (advance sql)
SQL Sanity- Duplication Check
SQL Sanity- Duplication Check
When you want to check for duplications in T1
SQL Sanity- Duplication Check
When you want to check for duplications in T1
SELECT pk
FROM T1
GROUP BY pk
HAVING COUNT(1)>1
SQL Sanity- Joinability
SQL Sanity- Joinability
When you want to check joinability ratios of T1 and T2
SQL Sanity- Joinability
When you want to check joinability ratios of T1 and T2
SELECT SUM(CAST(T1.pk IS NOT NULL AS INT) AS t1_size,
SUM(CAST(T2.pk IS NOT NULL AS INT) AS t2_size,
SUM(CAST(T2.pk IS NOT NULL AND AS INT) AS t2_size,
FROM T1
FULL OUTER JOIN T2
ON T2.pk = T2.pk
SQL Sanity- Joinability
When you want to check joinability ratios of T1 and T2
WITH T3 AS (SELECT T1.pk IS NOT NULL AS t1_row,
CAST(T2.pk IS NOT NULL AS INT) AS t2_row
FROM T1
FULL OUTER JOIN T2 ON T2.pk = T2.fk)
SELECT SUM(CAST(t1_row AS INT)) AS t1_size,
SUM(CAST(t2_row AS INT)) AS t2_size,
SUM(CAST(t1_row AND NOT t2_row AS INT)) AS only_t1_size,
SUM(CAST(NOT t1_row AND t2_row AS INT)) AS only_t2_size,
SUM(CAST(t1_row AND t2_row AS INT)) AS t1_t2_size,
FROM T3
SQL Sanity- Outliers
Understanding outlier detection in sql
SQL Sanity- Outliers
Understanding outliers with skew and kurtosis
Bring sanity back to sql (advance sql)
Spaghetti SQL
Spaghetti SQL Example
Avoiding Spaghetti SQL
Avoiding Spaghetti SQL
● Construct Rules and Conventions[example 1] [example 2]
Avoiding Spaghetti SQL
● Construct Rules and Conventions[example 1] [example 2]
● Automatically validate conventions in CI[tool 1]
Avoiding Spaghetti SQL
● Construct Rules and Conventions[example 1] [example 2]
● Automatically validate conventions in CI
● Break queries into multiple queries
[tool 1]
Avoiding Spaghetti SQL
● Construct Rules and Conventions[example 1] [example 2]
● Automatically validate conventions in CI
● Break queries into multiple queries
[tool 1]
WITH T2 AS
(subquery_statement_on_T1),
T3
AS(subquery_statement_on_T2)
SELECT *
FROM T3
CREATE TEMP TABLE T2 AS
(subquery_statement_on_T1);
CREATE TEMP TABLE T3 AS
(subquery_statement_on_T2);
SELECT *
FROM T3
With Statement: Create temp table Statement:
Bring sanity back to sql (advance sql)
Good Resources
● MUST SEE VIDEO
● Basic SQL reference
● Good SQL blog
● Another Good SQL blog
● http://www.silota.com/docs/recipes/
Bring sanity back to sql (advance sql)

More Related Content

What's hot (20)

PPTX
The Stack And Recursion
Ashim Lamichhane
 
PPT
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
PPT
Sorting Techniques
Rafay Farooq
 
PPT
Stacks, Queues, Deques
A-Tech and Software Development
 
PDF
Chapter 14 Searching and Sorting
MuhammadBakri13
 
PPTX
Lecture 02: Preliminaries of Data structure
Nurjahan Nipa
 
PDF
LEC3-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
PPSX
Stacks fundamentals
greatqadirgee4u
 
PPTX
Stack and queue
Shakila Mahjabin
 
PPT
Function vs not function
Mr. Hohman
 
PPT
Stacks queues
Rajendran
 
PPTX
Stack_Data_Structure.pptx
sandeep54552
 
PPTX
Stacks in c++
Vineeta Garg
 
PPT
Topic11 sortingandsearching
Gopi Saiteja
 
PPTX
stacks and queues
EktaVaswani2
 
PDF
sort search in C
faizankhan260690
 
PPSX
Stacks Implementation and Examples
greatqadirgee4u
 
PPT
Priority queues
Priyanka Rana
 
PPTX
Java presentation on insertion sort
_fahad_shaikh
 
PPTX
Stacks in Data Structure
Lovely Professional University
 
The Stack And Recursion
Ashim Lamichhane
 
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
Sorting Techniques
Rafay Farooq
 
Stacks, Queues, Deques
A-Tech and Software Development
 
Chapter 14 Searching and Sorting
MuhammadBakri13
 
Lecture 02: Preliminaries of Data structure
Nurjahan Nipa
 
LEC3-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
Stacks fundamentals
greatqadirgee4u
 
Stack and queue
Shakila Mahjabin
 
Function vs not function
Mr. Hohman
 
Stacks queues
Rajendran
 
Stack_Data_Structure.pptx
sandeep54552
 
Stacks in c++
Vineeta Garg
 
Topic11 sortingandsearching
Gopi Saiteja
 
stacks and queues
EktaVaswani2
 
sort search in C
faizankhan260690
 
Stacks Implementation and Examples
greatqadirgee4u
 
Priority queues
Priyanka Rana
 
Java presentation on insertion sort
_fahad_shaikh
 
Stacks in Data Structure
Lovely Professional University
 

Similar to Bring sanity back to sql (advance sql) (20)

PDF
45 Essential SQL Interview Questions
Best SEO Tampa
 
PPTX
Intro to t sql – 3rd session
Medhat Dawoud
 
PPTX
More Complex SQL and Concurrency ControlModule 4.pptx
bgscseise
 
PPT
Intro to tsql unit 3
Syed Asrarali
 
PPT
Intro To TSQL - Unit 3
iccma
 
PPT
Advanced Sql Training
bixxman
 
PPTX
Data Retrival
Er. Nawaraj Bhandari
 
PPTX
Basic Concepts of SQL for Class-XI & 12 CBSE Board
devsuchaye
 
PPTX
1. dml select statement reterive data
Amrit Kaur
 
PPT
Module 3 Part I - Bk1 Chapter 07.ppt
KusumaS36
 
PDF
04-Intro-SQL database management systems
ArmanHarun2221777642
 
PDF
Handling SQL Server Null Values
Duncan Greaves PhD
 
PPTX
Database Management System - SQL Advanced Training
Moutasm Tamimi
 
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Introduction to mysql part 2
baabtra.com - No. 1 supplier of quality freshers
 
PDF
L4_SQL.pdf
PriyanshTan
 
PDF
Tech Jam 01 - Database Querying
Rodger Oates
 
PDF
Standard SQL features where PostgreSQL beats its competitors
Markus Winand
 
PDF
SQL Outer Joins for Fun and Profit
Karwin Software Solutions LLC
 
PDF
Lecture_4_SQL_Adv_II_hi_HIHdkdjdjdjdjjjj
uuganbayrexe
 
45 Essential SQL Interview Questions
Best SEO Tampa
 
Intro to t sql – 3rd session
Medhat Dawoud
 
More Complex SQL and Concurrency ControlModule 4.pptx
bgscseise
 
Intro to tsql unit 3
Syed Asrarali
 
Intro To TSQL - Unit 3
iccma
 
Advanced Sql Training
bixxman
 
Data Retrival
Er. Nawaraj Bhandari
 
Basic Concepts of SQL for Class-XI & 12 CBSE Board
devsuchaye
 
1. dml select statement reterive data
Amrit Kaur
 
Module 3 Part I - Bk1 Chapter 07.ppt
KusumaS36
 
04-Intro-SQL database management systems
ArmanHarun2221777642
 
Handling SQL Server Null Values
Duncan Greaves PhD
 
Database Management System - SQL Advanced Training
Moutasm Tamimi
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
L4_SQL.pdf
PriyanshTan
 
Tech Jam 01 - Database Querying
Rodger Oates
 
Standard SQL features where PostgreSQL beats its competitors
Markus Winand
 
SQL Outer Joins for Fun and Profit
Karwin Software Solutions LLC
 
Lecture_4_SQL_Adv_II_hi_HIHdkdjdjdjdjjjj
uuganbayrexe
 
Ad

More from Eyal Trabelsi (6)

PPTX
Structuring and packaging your python project
Eyal Trabelsi
 
PPTX
Getting to know any dataset
Eyal Trabelsi
 
PPTX
Make Terminal Fun Again
Eyal Trabelsi
 
PPTX
Advance sql session - strings
Eyal Trabelsi
 
PPTX
Advance sql - window functions patterns and tricks
Eyal Trabelsi
 
PDF
Seminar - Similarity Joins in SQL (performance and semantic joins)
Eyal Trabelsi
 
Structuring and packaging your python project
Eyal Trabelsi
 
Getting to know any dataset
Eyal Trabelsi
 
Make Terminal Fun Again
Eyal Trabelsi
 
Advance sql session - strings
Eyal Trabelsi
 
Advance sql - window functions patterns and tricks
Eyal Trabelsi
 
Seminar - Similarity Joins in SQL (performance and semantic joins)
Eyal Trabelsi
 
Ad

Recently uploaded (20)

PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 

Bring sanity back to sql (advance sql)

  • 1. Advance SQL Bring Sanity Back to SQL Eyal Trabelsi
  • 2. Agenda ● Execution Order ● Null ● SQL Sanity ● Spaghetti Query ● Additional Resources
  • 8. Null Definition ● Null signifies an unknown value or that a value does not exist.
  • 9. Null Definition ● Null signifies an unknown value or that a value does not exist. ● The predicate is null can be used to check for Null values.
  • 10. Null Definition ● Null signifies an unknown value or that a value does not exist. ● The predicate is null can be used to check for Null values. SELECT <column_list> FROM [tablenames] WHERE <column_a> IS NULL
  • 12. Null Weird Behavior ● 5 + Null returns ? ● 5 < Null returns ? ● Null = Null ? ● Null in (Null) returns ? ● Null and true? ● Null and false? ● Null or true ? ● Null or false?
  • 13. Null Weird Behavior ● 5 + Null returns ? Null ● 5 < Null returns ? Null ● Null = Null ? Null != Null ● Null in (Null) returns ? Null because in use = operator ● Null and true? Null ● Null and false? False ● Null or true ? True ● Null or false? Null
  • 14. Null Weird Behavior ● The result of any arithmetic expression involving null is null.
  • 15. Null Weird Behavior ● The result of any arithmetic expression involving null is null. ● Any comparison with null returns unknown.
  • 16. Null Weird Behavior ● The result of any arithmetic expression involving null is null. ● Any comparison with null returns unknown. ● All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes.
  • 17. Null Weird Behavior ● The result of any arithmetic expression involving null is null. ● Any comparison with null returns unknown. ● All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes. ● logic inference
  • 19. Null Weird Behavior- Problem Example
  • 20. Null Weird Behavior- Problem Example a c c a d b a b NULL a c b T2T1
  • 21. Null Weird Behavior- Problem Example a c c a d b a b NULL a c b T2T1 Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is the fk)
  • 22. Null Weird Behavior- Solution 1 a c c a d b a b NULL a c b T2T1 Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is the fk)SELECT * FROM T1 WHERE a NOT IN (SELECT a FROM T2)
  • 23. Null Weird Behavior- Solution 1 a c c a d b a b NULL a c b T2T1 Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is the fk)SELECT * FROM T1 WHERE a NOT IN (SELECT a FROM T2) Is it correct ?
  • 24. Null Weird Behavior- Solution 1 a c c a d b a b NULL a c b T2T1 Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is the fk)SELECT * FROM T1 WHERE a NOT IN (SELECT a FROM T2) No it will retrieve no rows!!
  • 25. Null Weird Behavior- Solution 2 a c c a d b a b NULL a c b T2T1 Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is the fk)SELECT T1.* FROM T1 LEFT JOIN T2 ON T1.a = T2.a WHERE T2.a IS NULL
  • 26. Null Weird Behavior- Solution 2 a c c a d b a b NULL a c b T2T1 Retrieve rows of table table T1 which doesn’t exists in T2 ? (a is the fk)SELECT T1.* FROM T1 LEFT JOIN T2 ON T1.a = T2.a WHERE T2.a IS NULL Is it correct ?
  • 29. SQL Sanity- Duplication Check When you want to check for duplications in T1
  • 30. SQL Sanity- Duplication Check When you want to check for duplications in T1 SELECT pk FROM T1 GROUP BY pk HAVING COUNT(1)>1
  • 32. SQL Sanity- Joinability When you want to check joinability ratios of T1 and T2
  • 33. SQL Sanity- Joinability When you want to check joinability ratios of T1 and T2 SELECT SUM(CAST(T1.pk IS NOT NULL AS INT) AS t1_size, SUM(CAST(T2.pk IS NOT NULL AS INT) AS t2_size, SUM(CAST(T2.pk IS NOT NULL AND AS INT) AS t2_size, FROM T1 FULL OUTER JOIN T2 ON T2.pk = T2.pk
  • 34. SQL Sanity- Joinability When you want to check joinability ratios of T1 and T2 WITH T3 AS (SELECT T1.pk IS NOT NULL AS t1_row, CAST(T2.pk IS NOT NULL AS INT) AS t2_row FROM T1 FULL OUTER JOIN T2 ON T2.pk = T2.fk) SELECT SUM(CAST(t1_row AS INT)) AS t1_size, SUM(CAST(t2_row AS INT)) AS t2_size, SUM(CAST(t1_row AND NOT t2_row AS INT)) AS only_t1_size, SUM(CAST(NOT t1_row AND t2_row AS INT)) AS only_t2_size, SUM(CAST(t1_row AND t2_row AS INT)) AS t1_t2_size, FROM T3
  • 35. SQL Sanity- Outliers Understanding outlier detection in sql
  • 36. SQL Sanity- Outliers Understanding outliers with skew and kurtosis
  • 41. Avoiding Spaghetti SQL ● Construct Rules and Conventions[example 1] [example 2]
  • 42. Avoiding Spaghetti SQL ● Construct Rules and Conventions[example 1] [example 2] ● Automatically validate conventions in CI[tool 1]
  • 43. Avoiding Spaghetti SQL ● Construct Rules and Conventions[example 1] [example 2] ● Automatically validate conventions in CI ● Break queries into multiple queries [tool 1]
  • 44. Avoiding Spaghetti SQL ● Construct Rules and Conventions[example 1] [example 2] ● Automatically validate conventions in CI ● Break queries into multiple queries [tool 1] WITH T2 AS (subquery_statement_on_T1), T3 AS(subquery_statement_on_T2) SELECT * FROM T3 CREATE TEMP TABLE T2 AS (subquery_statement_on_T1); CREATE TEMP TABLE T3 AS (subquery_statement_on_T2); SELECT * FROM T3 With Statement: Create temp table Statement:
  • 46. Good Resources ● MUST SEE VIDEO ● Basic SQL reference ● Good SQL blog ● Another Good SQL blog ● http://www.silota.com/docs/recipes/

Editor's Notes

  • #45: https://stackoverflow.caom/questions/46421743/what-would-be-the-difference-between-with-clause-temporary-table