SlideShare a Scribd company logo
1 of 7
Download to read offline
Database Management
Essentials
Extended Query Formulation with SQL
Summarized by
github.com/dgkim5360
dgkim5360.tistory.com
Query Formulation
Guidelines
Query Formulation Process

• Problem Statement

• Database Representation

• Database Language
Statement
Efficiency Considerations

• 사실 별로 신경쓸 게 없다.

(요즘엔 알아서 잘 해줌)

• No Extra Tables

• No Unnecessary Grouping

• No Missing JOIN Conditions
Critical Questions
• What tables?
• Columns in result

• Conditions to test (including join conditions)

• How to combine the tables?
• Usually join of PK to FK

• Individual rows or groups of rows?
• Aggregate functions in result

• Conditions with aggregate functions
Examples
Multiple Tables

SELECT

OfferNo,

Offering.CourseNo,

CrsUnits,

OffDays,

OffLocations,

OffTime

FROM Faculty, Course, Offering

WHERE

Faculty.FacNo = Offering.FacNo

AND Offering.CourseNo =
Course.CourseNo

AND OffYear = 2016

AND OffTerm = 'FALL'

AND FacFirstName = 'LEONARD'

AND FacLastName = 'VINCE'

;

Joining & Grouping

SELECT

CourseNo,

Enrollment.OfferNo,

COUNT(*) AS NumStudents

FROM Offering, Enrollment

WHERE

Offering.OfferNo =
Enrollment.OfferNo

AND OffYear = 2017

AND OffTerm = 'SPRING'

GROUP BY

Enrollment.OfferNo,

CourseNo

;
SET Operators
• Union Compatibility: Table끼리 합집합하려면

• 같은 수의 column

• 매칭되는 column은 서로 같아야 (compatible)

• 매칭되는 column은 서로 같은 위치에 있어야

• 너무 빡빡하다!

• 현실적으로는...

• Union하고자 하는 column끼리만 같으면 인정

• 길이 제한은 안 맞아도 좀 괜찮다

• INTERSECT, MINUS도 있는데

• Oracle only

• 역시 union compatibility 충족해야

• MINUS 대신 SQL standard에서는 EXCEPT
SELECT <list-of-columns>
FROM <table1>
UNIOM
SELECT <list-of-columns>
FROM <table2>
SQL Modification
Statements
• INSERT, UPDATE, DELETE

• DB마다 다르다: 상수의 형식, null 값 표시

• PK는 웬만하면 바꾸지 말자

• Reference row의 update rule 때문에 불가능할 수도

• Related child row가 있다면 일이 복잡해진다.
Query Formulation Errors
• Syntax Error

• 실행되기 전에 compiler가 잡아줌

• 오탈자, 테이블 빼먹음, unqualified column name

• Redundancy Error

• 실제로 맞는 작업을 하는데, 불필요한 자원을 소모

• Extra table, 불필요한 GROUP BY

• Semantic Error

• 틀린 작업을 함 + 불필요한 자원도 소모할 수도

• 잘못된 JOIN/WHERE condition, 괄호가 없어서 잘못하시면 일자리를

잃을 수도 있습니다

More Related Content

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Database Management Essentials: Module 5 Extended Query Formulation with SQL

  • 1. Database Management Essentials Extended Query Formulation with SQL Summarized by github.com/dgkim5360 dgkim5360.tistory.com
  • 2. Query Formulation Guidelines Query Formulation Process • Problem Statement • Database Representation • Database Language Statement Efficiency Considerations • 사실 별로 신경쓸 게 없다.
 (요즘엔 알아서 잘 해줌) • No Extra Tables • No Unnecessary Grouping • No Missing JOIN Conditions
  • 3. Critical Questions • What tables? • Columns in result • Conditions to test (including join conditions) • How to combine the tables? • Usually join of PK to FK • Individual rows or groups of rows? • Aggregate functions in result • Conditions with aggregate functions
  • 4. Examples Multiple Tables SELECT
 OfferNo,
 Offering.CourseNo,
 CrsUnits,
 OffDays,
 OffLocations,
 OffTime
 FROM Faculty, Course, Offering
 WHERE
 Faculty.FacNo = Offering.FacNo
 AND Offering.CourseNo = Course.CourseNo
 AND OffYear = 2016
 AND OffTerm = 'FALL'
 AND FacFirstName = 'LEONARD'
 AND FacLastName = 'VINCE'
 ;
 Joining & Grouping SELECT
 CourseNo,
 Enrollment.OfferNo,
 COUNT(*) AS NumStudents
 FROM Offering, Enrollment
 WHERE
 Offering.OfferNo = Enrollment.OfferNo
 AND OffYear = 2017
 AND OffTerm = 'SPRING'
 GROUP BY
 Enrollment.OfferNo,
 CourseNo
 ;
  • 5. SET Operators • Union Compatibility: Table끼리 합집합하려면 • 같은 수의 column • 매칭되는 column은 서로 같아야 (compatible) • 매칭되는 column은 서로 같은 위치에 있어야 • 너무 빡빡하다! • 현실적으로는... • Union하고자 하는 column끼리만 같으면 인정 • 길이 제한은 안 맞아도 좀 괜찮다 • INTERSECT, MINUS도 있는데 • Oracle only • 역시 union compatibility 충족해야 • MINUS 대신 SQL standard에서는 EXCEPT SELECT <list-of-columns> FROM <table1> UNIOM SELECT <list-of-columns> FROM <table2>
  • 6. SQL Modification Statements • INSERT, UPDATE, DELETE • DB마다 다르다: 상수의 형식, null 값 표시 • PK는 웬만하면 바꾸지 말자 • Reference row의 update rule 때문에 불가능할 수도 • Related child row가 있다면 일이 복잡해진다.
  • 7. Query Formulation Errors • Syntax Error • 실행되기 전에 compiler가 잡아줌 • 오탈자, 테이블 빼먹음, unqualified column name • Redundancy Error • 실제로 맞는 작업을 하는데, 불필요한 자원을 소모 • Extra table, 불필요한 GROUP BY • Semantic Error • 틀린 작업을 함 + 불필요한 자원도 소모할 수도 • 잘못된 JOIN/WHERE condition, 괄호가 없어서 잘못하시면 일자리를
 잃을 수도 있습니다