SlideShare a Scribd company logo
Advance SQL
Bring Sanity Back to SQL
Eyal Trabelsi
Agenda
● Execution Order
● Null
● SQL Sanity
● Spaghetti Query
● Additional Resources
Execution order
Execution order
More detailed execution order
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
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 ?
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
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:
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

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

What's hot (20)

The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
Stacks fundamentals
Stacks fundamentalsStacks fundamentals
Stacks fundamentals
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Function vs not function
Function vs not functionFunction vs not function
Function vs not function
 
Stacks queues
Stacks queuesStacks queues
Stacks queues
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Topic11 sortingandsearching
Topic11 sortingandsearchingTopic11 sortingandsearching
Topic11 sortingandsearching
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
sort search in C
 sort search in C  sort search in C
sort search in C
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 

Similar to Bring sanity back to sql (advance sql)

Sql
SqlSql
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Integrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdfIntegrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdf
Saikrishna492522
 
Parsing
ParsingParsing
Parsing
khush_boo31
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
MySQL 8.0 NF : Common Table Expressions (CTE)
MySQL 8.0 NF : Common Table Expressions (CTE)MySQL 8.0 NF : Common Table Expressions (CTE)
MySQL 8.0 NF : Common Table Expressions (CTE)
I Goo Lee
 
GE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_NotesGE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_Notes
Asst.prof M.Gokilavani
 
Functional Programming with Clojure
Functional Programming with ClojureFunctional Programming with Clojure
Functional Programming with Clojure
Carlo Sciolla
 
Photon Technical Deep Dive: How to Think Vectorized
Photon Technical Deep Dive: How to Think VectorizedPhoton Technical Deep Dive: How to Think Vectorized
Photon Technical Deep Dive: How to Think Vectorized
Databricks
 
Sql interview questions
Sql interview questionsSql interview questions
Sql interview questions
nagesh Rao
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
ayyankhanna6480086
 
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB plc
 
OracleSQLraining.pptx
OracleSQLraining.pptxOracleSQLraining.pptx
OracleSQLraining.pptx
Rajendra Jain
 
Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...
Dimas Ruliandi
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
"Excelling in Excel" workshop
"Excelling in Excel" workshop"Excelling in Excel" workshop
"Excelling in Excel" workshop
Angelina Teneva
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3
MariaDB plc
 
Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 

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

Sql
SqlSql
Sql
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Integrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdfIntegrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdf
 
Parsing
ParsingParsing
Parsing
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
MySQL 8.0 NF : Common Table Expressions (CTE)
MySQL 8.0 NF : Common Table Expressions (CTE)MySQL 8.0 NF : Common Table Expressions (CTE)
MySQL 8.0 NF : Common Table Expressions (CTE)
 
GE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_NotesGE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_Notes
 
Functional Programming with Clojure
Functional Programming with ClojureFunctional Programming with Clojure
Functional Programming with Clojure
 
Photon Technical Deep Dive: How to Think Vectorized
Photon Technical Deep Dive: How to Think VectorizedPhoton Technical Deep Dive: How to Think Vectorized
Photon Technical Deep Dive: How to Think Vectorized
 
Sql interview questions
Sql interview questionsSql interview questions
Sql interview questions
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
 
OracleSQLraining.pptx
OracleSQLraining.pptxOracleSQLraining.pptx
OracleSQLraining.pptx
 
Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
"Excelling in Excel" workshop
"Excelling in Excel" workshop"Excelling in Excel" workshop
"Excelling in Excel" workshop
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3
 
Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
 

More from Eyal Trabelsi

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

More from Eyal Trabelsi (6)

Structuring and packaging your python project
Structuring and packaging your python projectStructuring and packaging your python project
Structuring and packaging your python project
 
Getting to know any dataset
Getting to know any datasetGetting to know any dataset
Getting to know any dataset
 
Make Terminal Fun Again
Make Terminal Fun AgainMake Terminal Fun Again
Make Terminal Fun Again
 
Advance sql session - strings
Advance sql  session - stringsAdvance sql  session - strings
Advance sql session - strings
 
Advance sql - window functions patterns and tricks
Advance sql - window functions patterns and tricksAdvance sql - window functions patterns and tricks
Advance sql - window functions patterns and tricks
 
Seminar - Similarity Joins in SQL (performance and semantic joins)
Seminar - Similarity Joins in SQL (performance and semantic joins)Seminar - Similarity Joins in SQL (performance and semantic joins)
Seminar - Similarity Joins in SQL (performance and semantic joins)
 

Recently uploaded

Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
GDSC PJATK
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 

Recently uploaded (20)

Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 

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
  • 3.
  • 6.
  • 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
  • 18.
  • 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 ?
  • 27.
  • 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
  • 37.
  • 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:
  • 45.
  • 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

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