SlideShare a Scribd company logo
1 of 31
SQL Patterns
in Practice
(You’re Probably
Doing it Wrong)
Sean Scott, Oracle DBA
sean.scott@bodybuilding.com
The Four Rules of
Writing SQL
To improve readability and self-document code, follow these
four simple rules.
SELECT
order.order_id, order_line, product_name, unit_price, supplier_
name, SUM(total_units), sum(unit_price*total_units)
FROM order, product, order_items
WHERE ((order_items.product_id = product.product_id) and
((order.order_id = order_items.order_id) and (customer_id =
42)))
GROUP BY
order_id, product_name, unit_price, supplier_name, total_units
ORDER BY order_id, product_name;
Rule 1
Everything on its own line
This improves readability and makes troubleshooting easier.
SELECT order.order_id,
order_line,
product_name,
unit_price,
supplier_name,
SUM(total_units), sum(unit_price*total_units)
FROM order,
product,
order_items
WHERE ((order_items.product_id = product.product_id) and
((order.order_id = order_items.order_id) and
(customer_id = 42)))
GROUP BY…
SELECT order.order_id,
--order_line,
product_name,
--unit_price,
supplier_name--,
--SUM(total_units),
--sum(unit_price*total_units)
FROM order,
product--,
--order_items
WHERE ((order_items.product_id = product.product_id) and
(--(order.order_id = order_items.order_id) and
and (customer_id = 42)))
Rule 2
Put commas, ANDs at the
beginning of lines, not the end
This minimizes commenting necessary to remove something
from your query.
SELECT order.order_id
, order_line
, product_name
, unit_price
, supplier_name
, SUM(total_units)
, sum(unit_price*total_units)
FROM order
, product
, order_items
WHERE ((order_items.product_id = product.product_id)
and ((order.order_id = order_items.order_id)
and (customer_id = 42)))
SELECT order.order_id
--, order_line
, product_name
--, unit_price
, supplier_name
--, SUM(total_units)
--, sum(unit_price*total_units)
FROM order
, product
--, order_items
WHERE ((order_items.product_id = product.product_id)
--and ((order.order_id = order_items.order_id)
( and (customer_id = 42)))
Rule 3
When joining tables, use short,
meaningful aliases for tables
and always prefix columns
It eliminates confusion about what table a column belongs
to, and improves readability.
SELECT o.order_id
, ol.order_line
, p.product_name
, ol.unit_price
, p.supplier_name
, SUM(ol.total_units)
, sum(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE ((oi.product_id = p.product_id)
and ((o.order_id = oi.order_id)
and (o.customer_id = 42)))
Rule 4
Avoid parentheses in WHERE
clauses unless required to nest
“OR” expressions
Simplify SQL by eliminating that which is unnecessary
SELECT o.order_id
, ol.order_line
, p.product_name
, ol.unit_price
, p.supplier_name
, SUM(ol.total_units)
, sum(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
and o.order_id = oi.order_id
and o.customer_id = 42
Four Rules
• Everything on its own line
• Put commas, “AND” at the beginning of lines, not
the end
• When joining tables, use short, meaningful aliases
for tables and always prefix columns
• Avoid parentheses in WHERE clauses unless required
to nest “OR” expressions.
The Six Habits of
Legible SQL
Seven habits to adopt when writing SQL that
will improve legibility
SELECT o.order_id
, ol.order_line
, p.product_name
, ol.unit_price
, p.supplier_name
, SUM(ol.total_units)
, sum(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
and o.order_id = oi.order_id
and o.customer_id = 42
Pick a case and stick to it
Upper or lower, it doesn’t matter, provided you’re consistent.
Case provides visual cues about the purpose or meaning of
the various parts of your statement.
SELECT o.order_id
, ol.order_line
, p.product_name
, ol.unit_price
, p.supplier_name
, SUM(ol.total_units)
, SUM(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
and o.order_id = oi.order_id
and o.customer_id = 42
Use white space to align
statements for meaning
White space makes the parts and purpose of a statement
more visually apparent and easier to read.
SELECT o.order_id
, ol.order_line
, p.product_name
, ol.unit_price
, p.supplier_name
, SUM(ol.total_units)
, SUM(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
AND o.order_id = oi.order_id
AND o.customer_id = 42
GROUP BY o.order_id
, ol.order_line
...
SELECT o.order_id
, ol.order_line
, p.product_name
, ol.unit_price
, p.supplier_name
, SUM(ol.total_units)
, SUM(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
AND o.order_id = oi.order_id
AND o.customer_id = 42
GROUP BY o.order_id
, ol.order_line
...
Use white space to align
operands and aliases
When all operands fall into alignment, it’s much easier to see the
left and right sides in the WHERE statement. Likewise, when table
aliases are aligned it’s much easier to reference them.
SELECT o.order_id
, ol.order_line
, p.product_name
, ol.unit_price
, p.supplier_name
, SUM(ol.total_units)
, SUM(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
AND o.order_id = oi.order_id
AND o.customer_id = 42
GROUP BY o.order_id
, ol.order_line
...
Group columns by table
If you need to troubleshoot by commenting out a table, it’s
more efficient when everything is together.
SELECT o.order_id
, p.product_name
, p.supplier_name
, ol.order_line
, ol.unit_price
, SUM(ol.total_units)
, SUM(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
AND o.order_id = oi.order_id
AND o.customer_id = 42
GROUP BY o.order_id
, ol.order_line
...
In a WHERE clause,
equalities first, then IN lists,
and subqueries last.
This orders statements from the least to most likely to cause a
problem and need to be edited.
SELECT o.order_id
, p.product_name
, p.supplier_name
, ol.order_line
, ol.unit_price
, SUM(ol.total_units)
, SUM(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
AND o.order_id = oi.order_id
AND o.customer_id = 42
GROUP BY o.order_id
, ol.order_line
...
SELECT o.order_id
, p.product_name
, p.supplier_name
, ol.order_line
, ol.unit_price
, SUM(ol.total_units)
, SUM(p.unit_price*ol.total_units)
FROM order o
INNER JOIN order_items oi
ON o.order_id = oi.order_id
INNER JOIN product p
ON oi.product_id = p.product_id
WHERE o.customer_id = 42
GROUP BY o.order_id
, ol.order_line
...
Place aggregate functions
last in the SELECT
It makes the GROUP BY easier to write—just copy/paste the
SELECT clause up to the aggregates as the GROUP BY.
SELECT o.order_id
, p.product_name
, p.supplier_name
, ol.order_line
, ol.unit_price
, SUM(ol.total_units)
, SUM(p.unit_price*ol.total_units)
FROM order o
, product p
, order_items oi
WHERE oi.product_id = p.product_id
AND o.order_id = oi.order_id
AND o.customer_id = 42
GROUP BY o.order_id
, ol.order_line
...
Six Habits of Legible SQL
• Pick a case and stick to it
• Use white space to align statements for meaning
• Use white space to align operands and aliases
• Group columns by table
• In a WHERE clause, equalities first, then IN lists, and
subqueries last
• Place aggregate functions last in the SELECT

More Related Content

What's hot

Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Achmad Solichin
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraintsuman kumar
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL WebinarRam Kedem
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview QuestionsBest SEO Tampa
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting dataSyed Zaid Irshad
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statementSyed Zaid Irshad
 
New Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialNew Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialIlgar Zarbaliyev
 
Fill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialFill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialIlgar Zarbaliyev
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functionsVikas Gupta
 
Sql basics
Sql basicsSql basics
Sql basicsKumar
 

What's hot (20)

Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
Module03
Module03Module03
Module03
 
Subquery & view
Subquery & viewSubquery & view
Subquery & view
 
Les03
Les03Les03
Les03
 
MY SQL
MY SQLMY SQL
MY SQL
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL Webinar
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Sql Tags
Sql TagsSql Tags
Sql Tags
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
 
Sql
SqlSql
Sql
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statement
 
New Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialNew Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel Tutorial
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Fill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialFill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel Tutorial
 
Les20
Les20Les20
Les20
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Sql basics
Sql basicsSql basics
Sql basics
 

Viewers also liked

SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2Mohd Tousif
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 
SQL: Query optimization in practice
SQL: Query optimization in practiceSQL: Query optimization in practice
SQL: Query optimization in practiceJano Suchal
 
Database Project Airport management System
Database Project Airport management SystemDatabase Project Airport management System
Database Project Airport management SystemFahad Chishti
 
Top 100 SQL Interview Questions and Answers
Top 100 SQL Interview Questions and AnswersTop 100 SQL Interview Questions and Answers
Top 100 SQL Interview Questions and Answersiimjobs and hirist
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answersvijaybusu
 

Viewers also liked (11)

SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
Oracle 10g Form
Oracle 10g Form Oracle 10g Form
Oracle 10g Form
 
10g forms
10g forms10g forms
10g forms
 
SQL: Query optimization in practice
SQL: Query optimization in practiceSQL: Query optimization in practice
SQL: Query optimization in practice
 
Sql joins
Sql joinsSql joins
Sql joins
 
Database Project Airport management System
Database Project Airport management SystemDatabase Project Airport management System
Database Project Airport management System
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
Top 100 SQL Interview Questions and Answers
Top 100 SQL Interview Questions and AnswersTop 100 SQL Interview Questions and Answers
Top 100 SQL Interview Questions and Answers
 
Airlines Database Design
Airlines Database DesignAirlines Database Design
Airlines Database Design
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 

Similar to SQL Patterns in Practice - You're (Probably) Doing it Wrong - Methods for Improving SQL

Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptxServer Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptxauzee32
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSFOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSHITIKAJAIN4
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic ConceptsTony Wong
 
Intro To TSQL - Unit 4
Intro To TSQL - Unit 4Intro To TSQL - Unit 4
Intro To TSQL - Unit 4iccma
 
Intro to tsql unit 4
Intro to tsql   unit 4Intro to tsql   unit 4
Intro to tsql unit 4Syed Asrarali
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfMadhusha15
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2Dan D'Urso
 
Database Management System - SQL Advanced Training
Database Management System - SQL Advanced TrainingDatabase Management System - SQL Advanced Training
Database Management System - SQL Advanced TrainingMoutasm Tamimi
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptxANSHVAJPAI
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Vidyasagar Mundroy
 
SQA server performance tuning
SQA server performance tuningSQA server performance tuning
SQA server performance tuningDuy Tan Geek
 

Similar to SQL Patterns in Practice - You're (Probably) Doing it Wrong - Methods for Improving SQL (20)

Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptxServer Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSFOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
Intro To TSQL - Unit 4
Intro To TSQL - Unit 4Intro To TSQL - Unit 4
Intro To TSQL - Unit 4
 
Intro to tsql unit 4
Intro to tsql   unit 4Intro to tsql   unit 4
Intro to tsql unit 4
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2
 
Sql functions
Sql functionsSql functions
Sql functions
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql General
Sql General Sql General
Sql General
 
Database Management System - SQL Advanced Training
Database Management System - SQL Advanced TrainingDatabase Management System - SQL Advanced Training
Database Management System - SQL Advanced Training
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Introtosqltuning
IntrotosqltuningIntrotosqltuning
Introtosqltuning
 
Sql
SqlSql
Sql
 
1 z0 047
1 z0 0471 z0 047
1 z0 047
 
SQA server performance tuning
SQA server performance tuningSQA server performance tuning
SQA server performance tuning
 

Recently uploaded

Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 

Recently uploaded (20)

Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 

SQL Patterns in Practice - You're (Probably) Doing it Wrong - Methods for Improving SQL

  • 1. SQL Patterns in Practice (You’re Probably Doing it Wrong) Sean Scott, Oracle DBA sean.scott@bodybuilding.com
  • 2. The Four Rules of Writing SQL To improve readability and self-document code, follow these four simple rules.
  • 3. SELECT order.order_id, order_line, product_name, unit_price, supplier_ name, SUM(total_units), sum(unit_price*total_units) FROM order, product, order_items WHERE ((order_items.product_id = product.product_id) and ((order.order_id = order_items.order_id) and (customer_id = 42))) GROUP BY order_id, product_name, unit_price, supplier_name, total_units ORDER BY order_id, product_name;
  • 4. Rule 1 Everything on its own line This improves readability and makes troubleshooting easier.
  • 5. SELECT order.order_id, order_line, product_name, unit_price, supplier_name, SUM(total_units), sum(unit_price*total_units) FROM order, product, order_items WHERE ((order_items.product_id = product.product_id) and ((order.order_id = order_items.order_id) and (customer_id = 42))) GROUP BY…
  • 6. SELECT order.order_id, --order_line, product_name, --unit_price, supplier_name--, --SUM(total_units), --sum(unit_price*total_units) FROM order, product--, --order_items WHERE ((order_items.product_id = product.product_id) and (--(order.order_id = order_items.order_id) and and (customer_id = 42)))
  • 7. Rule 2 Put commas, ANDs at the beginning of lines, not the end This minimizes commenting necessary to remove something from your query.
  • 8. SELECT order.order_id , order_line , product_name , unit_price , supplier_name , SUM(total_units) , sum(unit_price*total_units) FROM order , product , order_items WHERE ((order_items.product_id = product.product_id) and ((order.order_id = order_items.order_id) and (customer_id = 42)))
  • 9. SELECT order.order_id --, order_line , product_name --, unit_price , supplier_name --, SUM(total_units) --, sum(unit_price*total_units) FROM order , product --, order_items WHERE ((order_items.product_id = product.product_id) --and ((order.order_id = order_items.order_id) ( and (customer_id = 42)))
  • 10. Rule 3 When joining tables, use short, meaningful aliases for tables and always prefix columns It eliminates confusion about what table a column belongs to, and improves readability.
  • 11. SELECT o.order_id , ol.order_line , p.product_name , ol.unit_price , p.supplier_name , SUM(ol.total_units) , sum(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE ((oi.product_id = p.product_id) and ((o.order_id = oi.order_id) and (o.customer_id = 42)))
  • 12. Rule 4 Avoid parentheses in WHERE clauses unless required to nest “OR” expressions Simplify SQL by eliminating that which is unnecessary
  • 13. SELECT o.order_id , ol.order_line , p.product_name , ol.unit_price , p.supplier_name , SUM(ol.total_units) , sum(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id and o.order_id = oi.order_id and o.customer_id = 42
  • 14. Four Rules • Everything on its own line • Put commas, “AND” at the beginning of lines, not the end • When joining tables, use short, meaningful aliases for tables and always prefix columns • Avoid parentheses in WHERE clauses unless required to nest “OR” expressions.
  • 15. The Six Habits of Legible SQL Seven habits to adopt when writing SQL that will improve legibility
  • 16. SELECT o.order_id , ol.order_line , p.product_name , ol.unit_price , p.supplier_name , SUM(ol.total_units) , sum(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id and o.order_id = oi.order_id and o.customer_id = 42
  • 17. Pick a case and stick to it Upper or lower, it doesn’t matter, provided you’re consistent. Case provides visual cues about the purpose or meaning of the various parts of your statement.
  • 18. SELECT o.order_id , ol.order_line , p.product_name , ol.unit_price , p.supplier_name , SUM(ol.total_units) , SUM(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id and o.order_id = oi.order_id and o.customer_id = 42
  • 19. Use white space to align statements for meaning White space makes the parts and purpose of a statement more visually apparent and easier to read.
  • 20. SELECT o.order_id , ol.order_line , p.product_name , ol.unit_price , p.supplier_name , SUM(ol.total_units) , SUM(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42 GROUP BY o.order_id , ol.order_line ...
  • 21. SELECT o.order_id , ol.order_line , p.product_name , ol.unit_price , p.supplier_name , SUM(ol.total_units) , SUM(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42 GROUP BY o.order_id , ol.order_line ...
  • 22. Use white space to align operands and aliases When all operands fall into alignment, it’s much easier to see the left and right sides in the WHERE statement. Likewise, when table aliases are aligned it’s much easier to reference them.
  • 23. SELECT o.order_id , ol.order_line , p.product_name , ol.unit_price , p.supplier_name , SUM(ol.total_units) , SUM(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42 GROUP BY o.order_id , ol.order_line ...
  • 24. Group columns by table If you need to troubleshoot by commenting out a table, it’s more efficient when everything is together.
  • 25. SELECT o.order_id , p.product_name , p.supplier_name , ol.order_line , ol.unit_price , SUM(ol.total_units) , SUM(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42 GROUP BY o.order_id , ol.order_line ...
  • 26. In a WHERE clause, equalities first, then IN lists, and subqueries last. This orders statements from the least to most likely to cause a problem and need to be edited.
  • 27. SELECT o.order_id , p.product_name , p.supplier_name , ol.order_line , ol.unit_price , SUM(ol.total_units) , SUM(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42 GROUP BY o.order_id , ol.order_line ...
  • 28. SELECT o.order_id , p.product_name , p.supplier_name , ol.order_line , ol.unit_price , SUM(ol.total_units) , SUM(p.unit_price*ol.total_units) FROM order o INNER JOIN order_items oi ON o.order_id = oi.order_id INNER JOIN product p ON oi.product_id = p.product_id WHERE o.customer_id = 42 GROUP BY o.order_id , ol.order_line ...
  • 29. Place aggregate functions last in the SELECT It makes the GROUP BY easier to write—just copy/paste the SELECT clause up to the aggregates as the GROUP BY.
  • 30. SELECT o.order_id , p.product_name , p.supplier_name , ol.order_line , ol.unit_price , SUM(ol.total_units) , SUM(p.unit_price*ol.total_units) FROM order o , product p , order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42 GROUP BY o.order_id , ol.order_line ...
  • 31. Six Habits of Legible SQL • Pick a case and stick to it • Use white space to align statements for meaning • Use white space to align operands and aliases • Group columns by table • In a WHERE clause, equalities first, then IN lists, and subqueries last • Place aggregate functions last in the SELECT

Editor's Notes

  1. SQL has very few rules.Capitalization doesn’t matter in many cases, MySQL being a notable exception.White space is meaningless.I located several ETL queries in my work library that exceeded 500 lines. I can’t imagine being able to understand or work on anything at that scale without some rules for writing and formatting the SQL, but outside of the order you write the statements, SQL is almost rule free. Everyone seems to do it differently, and you can see that if you go just to the web where you’ll find a plethora of styles.My method of writing SQL has evolved over a 25 year career. It’s based on having written a lot of SQL, but more importantly on having been a consultant for many years, specializing in part in troubleshooting performance issues. Usually, that means looking at a piece of code that I’ve never seen before and trying to figure out what it does so I can understand why it’s not performing well. Hundreds of lines of ugly, poorly formatted SQL for some ETL or data warehouse query are almost always going to be tough to read, so I needed to come up with a way of making it more palatable.This method is centered on making SQL legible and easy to troubleshoot. It’s version agnostic, so you can use it in Oracle, MySQL, SQL Server, Postgress, etc. It’s quite simple and makes a ton of sense to me.I write almost exclusively in a simple text editor, directly in vi, or at the command line. I’m not personally a fan of GUI or object editors, but these same methods will work in tools like Enterprise Manager, SQL Developer, TOAD, or whatever else you may like to use.
  2. This is a fairly simple piece of SQL that I pulled off of a web site. The formatting seen here isn’t unusual.QUESTIONS:What is this doing?Isit easy to read?To understand?If you had to work on this, either to add to it or fix something that was broken, what’s the first thing you’d do?
  3. My first rule is that everything gets it’s own line. Yes, it will make the statement longer but it has important benefits.
  4. A typical troubleshooting scenario for a complex query involves breaking it down, often by commenting out individual tables to see where a problem is introduced. When everything is run together it’s much more difficult to remove individual columns and tables and parts of the WHERE clause.At this point, I’ve removed the GROUP BY and ORDER BY clauses simply because it will be more readable from here on out.If we were having some issue with the order_itemstable and wanted to comment it out, it is somewhat easier.
  5. Notice that I had to comment out some columns in the select, from and where clauses. I also had to comment out the comma on the end of the supplier name, the comma after product in the from clause, add an “and” before the customer ID because of the lost and at the end of the order_id equijoin, and I had to add that comment in between two parenthesis to keep them balanced.That’s kind of a pain in the neck, and it leads to the next rule.
  6. Don’t end a line with a comma or an and. Instead, start the next line with the comma or and.That would make the original statement look like this:
  7. …and now if we wanted to comment out those same lines to get rid of the order_items table, it’s a lot less work:
  8. I did still have to add a parenthesis at the beginning of the last line to keep them balanced. We’ll get to that later.Between putting everything on one line and moving “and”s and commas to the beginning of lines, you eliminate effort spent commenting out lines when you’re in troubleshooting mode. It’s just a lot less work and it’ll make your life much easier, especially when you’re dealing with a query more complex than this.Remember that we also had GROUP BY and ORDER BY clauses, and the same thing goes for them as well.One of my pet peeves is when someone assumes that whoever comes after them is going to have intimate understanding of their schema, and they leave out column aliases. Looking at this, it’s not obvious what table any of this belongs to.
  9. Rule 3: When joining tables, use short, meaningful aliases for tables and always prefix columns.Let’s do that now and get rid of the comments...
  10. At this point, things are a lot more obvious. This appears a lot less cluttered, and if this were even slightly longer it’s going to save a ton of typing all of those table names in favor of short aliases.We’re also being good coding citizens by including a basic level of documentation within the code itself. All columns are aliased, meaning there’s no need to refer to a data dictionary to see where unaliased columns came from.One last rule. In this query, the parentheses have absolutely no function other than to make the query confusing.
  11. Rule 4: Avoid parentheses in your where clause unless you absolutely need them to nest “OR” expressions. Call it minimalist SQL.The only time you should ever have parentheses in your WHERE clause (outside of enclosing a functions, INLISTs or subqueries, obviously) is when they’re necessary to enforce the order in which a statement is evaluated. In a where clause, that’s when you have an “OR”.No OR? No parentheses. Let’s fix this statement…
  12. Summary of the four rules.
  13. In addition to the four rules, there are six good habits for writing SQL that will improve legibility and make your life easier. These are not hard-and-fast rules and there is flexibility in how you apply them, but I do consider them central to this method.
  14. Pick a case and stick to it. If you want to use upper case for keywords like SELECT and FROM, go for it. This is purely personal preference. The same goes for objects.I personally put everything in lower case. It seems less like shouting and it means that literals “where a.b = ‘SOME STRING’” stand out a little more, since they are often upper case values. However, whichever case you personally choose, others will take visual cues from it provided you’re consistent.
  15. Designers use white space to improve legibility and assign visual meaning to layouts. SQL benefits from the same technique.SQL is easier to read if everything lines up.
  16. Here, I’ve aligned the queryalong a gutter by right aligning the statements and left aligning objects. This creates a nice visual flow for your eye. It sees the statements (SELECT, FROM, WHERE) as being important, and the objects all have similar visual meaning and importance to the right.I personally like to indent everything on the GROUP BY/ORDER BY which means all of my objects start in column 10. I personally find that to be easier to read.Other people like to left align everything, such as:
  17. Whatever technique you prefer. Again, consistency is the most important thing.
  18. Just as white space gives meaning and importance to the statements and objects in your query, it has a similar effect on the aliases and operands in your FROM and WHERE clauses.
  19. This provides a nice, visual cue that separates aliases from tables and makes them easy to find, as well as separating the two sides of equality tests.With the aliases aligned, it’s easy for your eye to fall directly on them as a reference. Likewise, when reading the WHERE clause, it’s much easier to see and differentiate the left and right sides of the individual conditions.
  20. I recommend grouping columns by table if possible, so that if I do need to troubleshoot and comment out a particular table, everything is all together.SQL is often being fed to some other language that will reorder and format the columns so the actual order they appear in your statement isn’t always important.
  21. This allows you to comment out the columns associated with a particular table as a single block, as opposed to hunting/pecking through the code.
  22. If you’re old fashioned, your where clause should start with equalities needed to satisfy the table joins, followed by test (equality, inequality) conditions, followed by in lists, followed by subqueries.This order is going from typically less to more problematic. I recommend putting the equalities and inequalities first, because they’re the things that are likely to be hard and fast tests that won’t change and are less likely to be removed.Betweens and IN lists are ranges and pseudo-equalities, and are a little more likely to be edited or removed for troubleshooting. These are also visually heavier and sometimes less critical to how the query works.Subqueries are usually the most problematic to the performance or accuracy of a query. Putting them at the end just makes it less work to cut and paste them out of the middle of a query. It’s also easier to copy/paste the top part of a query into into a terminal window, or highlight the SQL up to the subquery and run just that part.
  23. For those that want to see this as ANSI joins.
  24. I suggest putting aggregate functions last in the select.Aggregates are more likely to be the source of a problem than non-aggregates, so this makes them easier to comment out if necessary. It also means that building the GROUP BY clause is as simple as copying up to the aggregates and then pasting that into the GROUP BY.