SlideShare a Scribd company logo
1 of 11
77
Multiple-Column Subqueries
7-2
Objectives
After completing this lesson, you shouldAfter completing this lesson, you should
be able to do the following:be able to do the following:
• Write a multiple-column subquery
• Describe and explain the behavior of
subqueries when null values are
retrieved
• Write a subquery in a FROM clause
7-3
Multiple-Column Subqueries
Main query
MANAGER 10
Subquery
SALESMAN 30
MANAGER 10
CLERK 20
Main queryMain query
comparescompares
MANAGER 10MANAGER 10
Values from a multiple-row andValues from a multiple-row and
multiple-column subquerymultiple-column subquery
SALESMANSALESMAN 3030
MANAGERMANAGER 1010
CLERKCLERK 2020
toto
7-4
Using Multiple-Column
Subqueries
Display the order number, product number, andDisplay the order number, product number, and
quantity of any item in which the productquantity of any item in which the product
number and quantity matchnumber and quantity match bothboth the productthe product
number and quantity of an item in order 605.number and quantity of an item in order 605.
SQL> SELECT ordid, prodid, qty
2 FROM item
3 WHERE (prodid, qty) IN
4 (SELECT prodid, qty
5 FROM item
6 WHERE ordid = 605)
7 AND ordid <> 605;
7-6
Column Comparisons
Pairwise
PRODID QTY
101863 100
100861 100
102130 10
100890 5
100870 500
101860 50
Nonpairwise
PRODID QTY
101863 100
100861 100
102130 10
100890 5
100870 500
101860 50
7-7
Nonpairwise Comparison
Subquery
SQL> SELECT ordid, prodid, qty
2 FROM item
3 WHERE prodid IN (SELECT prodid
4 FROM item
5 WHERE ordid = 605)
6 AND qty IN (SELECT qty
7 FROM item
8 WHERE ordid = 605)
9 AND ordid <> 605;
Display the order number, product number, andDisplay the order number, product number, and
quantity of any item in which the product numberquantity of any item in which the product number
and quantity match any product number and anyand quantity match any product number and any
quantity of an item in order 605.quantity of an item in order 605.
7-8
Nonpairwise Subquery
ORDID PRODID QTY
--------- --------- ---------
609 100870 5
616 100861 10
616 102130 10
621 100861 10
618 100870 10
618 100861 50
616 100870 50
617 100861 100
619 102130 100
615 100870 100
617 101860 100
621 100870 100
617 102130 100
. . .
16 rows selected.
7-9
Null Values in a Subquery
SQL> SELECT employee.ename
2 FROM emp employee
3 WHERE employee.empno NOT IN
4 (SELECT manager.mgr
5 FROM emp manager);
no rows selected.no rows selected.
7-10
Using a Subquery
in the FROM Clause
ENAME SAL DEPTNO SALAVG
---------- --------- --------- ----------
KING 5000 10 2916.6667
JONES 2975 20 2175
SCOTT 3000 20 2175
...
6 rows selected.
ENAME SAL DEPTNO SALAVG
---------- --------- --------- ----------
KING 5000 10 2916.6667
JONES 2975 20 2175
SCOTT 3000 20 2175
...
6 rows selected.
SQL> SELECT a.ename, a.sal, a.deptno, b.salavg
2 FROM emp a, (SELECT deptno, avg(sal) salavg
3 FROM emp
4 GROUP BY deptno) b
5 WHERE a.deptno = b.deptno
6 AND a.sal > b.salavg;
7-11
Summary
• A multiple-column subquery returns
more than one column.
• Column comparisons in multiple-
column comparisons can be pairwise or
nonpairwise.
• A multiple-column subquery can also be
used in the FROM clause of a SELECT
statement.
7-12
Practice Overview
Creating multiple-column subqueries

More Related Content

What's hot

Window aggregate functions
Window aggregate functionsWindow aggregate functions
Window aggregate functionsSilvio Sangineto
 
Practice create procedure
Practice   create procedurePractice   create procedure
Practice create procedurecit gubbi
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functionssinhacp
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functionsMudasir Syed
 
Procedure rol against retail raw data
Procedure rol against retail raw dataProcedure rol against retail raw data
Procedure rol against retail raw dataPalash Halder
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queriesSyed Zaid Irshad
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answersNawaz Sk
 

What's hot (9)

Window aggregate functions
Window aggregate functionsWindow aggregate functions
Window aggregate functions
 
Practice create procedure
Practice   create procedurePractice   create procedure
Practice create procedure
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Lab 07
Lab 07Lab 07
Lab 07
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 
Procedure rol against retail raw data
Procedure rol against retail raw dataProcedure rol against retail raw data
Procedure rol against retail raw data
 
Les11
Les11Les11
Les11
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
 

Viewers also liked

SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2Umair Amjad
 
SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9Umair Amjad
 
SQL WORKSHOP::Lecture 10
SQL WORKSHOP::Lecture 10SQL WORKSHOP::Lecture 10
SQL WORKSHOP::Lecture 10Umair Amjad
 
SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6Umair Amjad
 
SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12Umair Amjad
 
SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4Umair Amjad
 
SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11Umair Amjad
 
SQL WORKSHOP::Lecture 13
SQL WORKSHOP::Lecture 13SQL WORKSHOP::Lecture 13
SQL WORKSHOP::Lecture 13Umair Amjad
 
SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5Umair Amjad
 
Slides 2-basic sql
Slides 2-basic sqlSlides 2-basic sql
Slides 2-basic sqlAnuja Lad
 
SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3Umair Amjad
 
SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1Umair Amjad
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL FundamentalsBrian Foote
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsSalman Memon
 
Tutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft AccessTutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft Accessmcclellm
 

Viewers also liked (20)

SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2
 
SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9SQL WORKSHOP::Lecture 9
SQL WORKSHOP::Lecture 9
 
SQL WORKSHOP::Lecture 10
SQL WORKSHOP::Lecture 10SQL WORKSHOP::Lecture 10
SQL WORKSHOP::Lecture 10
 
SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6
 
SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12
 
SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4
 
SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11
 
SQL WORKSHOP::Lecture 13
SQL WORKSHOP::Lecture 13SQL WORKSHOP::Lecture 13
SQL WORKSHOP::Lecture 13
 
SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5
 
Slides 2-basic sql
Slides 2-basic sqlSlides 2-basic sql
Slides 2-basic sql
 
Lecture 07 - Basic SQL
Lecture 07 - Basic SQLLecture 07 - Basic SQL
Lecture 07 - Basic SQL
 
Basic SQL Part 2
Basic SQL Part 2Basic SQL Part 2
Basic SQL Part 2
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
 
SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3
 
SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Tutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft AccessTutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft Access
 
SQL Injection
SQL Injection SQL Injection
SQL Injection
 
SQL injection: Not only AND 1=1
SQL injection: Not only AND 1=1SQL injection: Not only AND 1=1
SQL injection: Not only AND 1=1
 

Similar to SQL WORKSHOP::Lecture 7

Les07- Multiple-Column Subqueries.ppt
Les07- Multiple-Column Subqueries.pptLes07- Multiple-Column Subqueries.ppt
Les07- Multiple-Column Subqueries.pptgznfrch1
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueriessiavosh kaviani
 
Simplifying SQL with CTE's and windowing functions
Simplifying SQL with CTE's and windowing functionsSimplifying SQL with CTE's and windowing functions
Simplifying SQL with CTE's and windowing functionsClayton Groom
 
Sq lite functions
Sq lite functionsSq lite functions
Sq lite functionspunu_82
 
Execution plans for mere mortals
Execution plans for mere mortalsExecution plans for mere mortals
Execution plans for mere mortalsMike Lawell
 
How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?loginworks software
 
Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219Peter Schouboe
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding GuidelinesChris Adkin
 
解决Ora 14098分区交换索引不匹配错误
解决Ora 14098分区交换索引不匹配错误解决Ora 14098分区交换索引不匹配错误
解决Ora 14098分区交换索引不匹配错误maclean liu
 
The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210Mahmoud Samir Fayed
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdfKalyankumarVenkat1
 
5.Analytical Function.pdf
5.Analytical Function.pdf5.Analytical Function.pdf
5.Analytical Function.pdfssuser8b6c85
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxHAMEEDHUSSAINBU21CSE
 
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxMSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxgilpinleeanna
 
USE IMDB -- ensures correct database is active G.docx
USE IMDB    -- ensures correct database is active   G.docxUSE IMDB    -- ensures correct database is active   G.docx
USE IMDB -- ensures correct database is active G.docxmadlynplamondon
 

Similar to SQL WORKSHOP::Lecture 7 (20)

Les07- Multiple-Column Subqueries.ppt
Les07- Multiple-Column Subqueries.pptLes07- Multiple-Column Subqueries.ppt
Les07- Multiple-Column Subqueries.ppt
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueries
 
Simplifying SQL with CTE's and windowing functions
Simplifying SQL with CTE's and windowing functionsSimplifying SQL with CTE's and windowing functions
Simplifying SQL with CTE's and windowing functions
 
Sq lite functions
Sq lite functionsSq lite functions
Sq lite functions
 
Execution plans for mere mortals
Execution plans for mere mortalsExecution plans for mere mortals
Execution plans for mere mortals
 
How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?
 
Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
1 z1 051
1 z1 0511 z1 051
1 z1 051
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 
解决Ora 14098分区交换索引不匹配错误
解决Ora 14098分区交换索引不匹配错误解决Ora 14098分区交换索引不匹配错误
解决Ora 14098分区交换索引不匹配错误
 
The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
5.Analytical Function.pdf
5.Analytical Function.pdf5.Analytical Function.pdf
5.Analytical Function.pdf
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
 
Chapter5.ppt
Chapter5.pptChapter5.ppt
Chapter5.ppt
 
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxMSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
 
SQL sheet
SQL sheetSQL sheet
SQL sheet
 
USE IMDB -- ensures correct database is active G.docx
USE IMDB    -- ensures correct database is active   G.docxUSE IMDB    -- ensures correct database is active   G.docx
USE IMDB -- ensures correct database is active G.docx
 
Les06 Subqueries
Les06 SubqueriesLes06 Subqueries
Les06 Subqueries
 

More from Umair Amjad

Automated Process for Auditng in Agile - SCRUM
Automated Process for Auditng in Agile - SCRUMAutomated Process for Auditng in Agile - SCRUM
Automated Process for Auditng in Agile - SCRUMUmair Amjad
 
Data Deduplication: Venti and its improvements
Data Deduplication: Venti and its improvementsData Deduplication: Venti and its improvements
Data Deduplication: Venti and its improvementsUmair Amjad
 
Bead–Sort :: A Natural Sorting Algorithm
Bead–Sort :: A Natural Sorting AlgorithmBead–Sort :: A Natural Sorting Algorithm
Bead–Sort :: A Natural Sorting AlgorithmUmair Amjad
 
Apache logs monitoring
Apache logs monitoringApache logs monitoring
Apache logs monitoringUmair Amjad
 
Exact Cell Decomposition of Arrangements used for Path Planning in Robotics
Exact Cell Decomposition of Arrangements used for Path Planning in RoboticsExact Cell Decomposition of Arrangements used for Path Planning in Robotics
Exact Cell Decomposition of Arrangements used for Path Planning in RoboticsUmair Amjad
 
Ruby on Rails workshop for beginner
Ruby on Rails workshop for beginnerRuby on Rails workshop for beginner
Ruby on Rails workshop for beginnerUmair Amjad
 
DCT based Watermarking technique
DCT based Watermarking techniqueDCT based Watermarking technique
DCT based Watermarking techniqueUmair Amjad
 
Multi-core processor and Multi-channel memory architecture
Multi-core processor and Multi-channel memory architectureMulti-core processor and Multi-channel memory architecture
Multi-core processor and Multi-channel memory architectureUmair Amjad
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Umair Amjad
 

More from Umair Amjad (9)

Automated Process for Auditng in Agile - SCRUM
Automated Process for Auditng in Agile - SCRUMAutomated Process for Auditng in Agile - SCRUM
Automated Process for Auditng in Agile - SCRUM
 
Data Deduplication: Venti and its improvements
Data Deduplication: Venti and its improvementsData Deduplication: Venti and its improvements
Data Deduplication: Venti and its improvements
 
Bead–Sort :: A Natural Sorting Algorithm
Bead–Sort :: A Natural Sorting AlgorithmBead–Sort :: A Natural Sorting Algorithm
Bead–Sort :: A Natural Sorting Algorithm
 
Apache logs monitoring
Apache logs monitoringApache logs monitoring
Apache logs monitoring
 
Exact Cell Decomposition of Arrangements used for Path Planning in Robotics
Exact Cell Decomposition of Arrangements used for Path Planning in RoboticsExact Cell Decomposition of Arrangements used for Path Planning in Robotics
Exact Cell Decomposition of Arrangements used for Path Planning in Robotics
 
Ruby on Rails workshop for beginner
Ruby on Rails workshop for beginnerRuby on Rails workshop for beginner
Ruby on Rails workshop for beginner
 
DCT based Watermarking technique
DCT based Watermarking techniqueDCT based Watermarking technique
DCT based Watermarking technique
 
Multi-core processor and Multi-channel memory architecture
Multi-core processor and Multi-channel memory architectureMulti-core processor and Multi-channel memory architecture
Multi-core processor and Multi-channel memory architecture
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
 

Recently uploaded

Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 

Recently uploaded (20)

Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 

SQL WORKSHOP::Lecture 7

  • 2. 7-2 Objectives After completing this lesson, you shouldAfter completing this lesson, you should be able to do the following:be able to do the following: • Write a multiple-column subquery • Describe and explain the behavior of subqueries when null values are retrieved • Write a subquery in a FROM clause
  • 3. 7-3 Multiple-Column Subqueries Main query MANAGER 10 Subquery SALESMAN 30 MANAGER 10 CLERK 20 Main queryMain query comparescompares MANAGER 10MANAGER 10 Values from a multiple-row andValues from a multiple-row and multiple-column subquerymultiple-column subquery SALESMANSALESMAN 3030 MANAGERMANAGER 1010 CLERKCLERK 2020 toto
  • 4. 7-4 Using Multiple-Column Subqueries Display the order number, product number, andDisplay the order number, product number, and quantity of any item in which the productquantity of any item in which the product number and quantity matchnumber and quantity match bothboth the productthe product number and quantity of an item in order 605.number and quantity of an item in order 605. SQL> SELECT ordid, prodid, qty 2 FROM item 3 WHERE (prodid, qty) IN 4 (SELECT prodid, qty 5 FROM item 6 WHERE ordid = 605) 7 AND ordid <> 605;
  • 5. 7-6 Column Comparisons Pairwise PRODID QTY 101863 100 100861 100 102130 10 100890 5 100870 500 101860 50 Nonpairwise PRODID QTY 101863 100 100861 100 102130 10 100890 5 100870 500 101860 50
  • 6. 7-7 Nonpairwise Comparison Subquery SQL> SELECT ordid, prodid, qty 2 FROM item 3 WHERE prodid IN (SELECT prodid 4 FROM item 5 WHERE ordid = 605) 6 AND qty IN (SELECT qty 7 FROM item 8 WHERE ordid = 605) 9 AND ordid <> 605; Display the order number, product number, andDisplay the order number, product number, and quantity of any item in which the product numberquantity of any item in which the product number and quantity match any product number and anyand quantity match any product number and any quantity of an item in order 605.quantity of an item in order 605.
  • 7. 7-8 Nonpairwise Subquery ORDID PRODID QTY --------- --------- --------- 609 100870 5 616 100861 10 616 102130 10 621 100861 10 618 100870 10 618 100861 50 616 100870 50 617 100861 100 619 102130 100 615 100870 100 617 101860 100 621 100870 100 617 102130 100 . . . 16 rows selected.
  • 8. 7-9 Null Values in a Subquery SQL> SELECT employee.ename 2 FROM emp employee 3 WHERE employee.empno NOT IN 4 (SELECT manager.mgr 5 FROM emp manager); no rows selected.no rows selected.
  • 9. 7-10 Using a Subquery in the FROM Clause ENAME SAL DEPTNO SALAVG ---------- --------- --------- ---------- KING 5000 10 2916.6667 JONES 2975 20 2175 SCOTT 3000 20 2175 ... 6 rows selected. ENAME SAL DEPTNO SALAVG ---------- --------- --------- ---------- KING 5000 10 2916.6667 JONES 2975 20 2175 SCOTT 3000 20 2175 ... 6 rows selected. SQL> SELECT a.ename, a.sal, a.deptno, b.salavg 2 FROM emp a, (SELECT deptno, avg(sal) salavg 3 FROM emp 4 GROUP BY deptno) b 5 WHERE a.deptno = b.deptno 6 AND a.sal > b.salavg;
  • 10. 7-11 Summary • A multiple-column subquery returns more than one column. • Column comparisons in multiple- column comparisons can be pairwise or nonpairwise. • A multiple-column subquery can also be used in the FROM clause of a SELECT statement.

Editor's Notes

  1. Schedule: Timing Topic 20 minutes Lecture 20 minutes Practice 40 minutes Total
  2. Lesson Aim In this lesson, you will learn how to write multiple-column subqueries and subqueries in the FROM clause of a SELECT statement.
  3. Multiple-Column Subqueries So far you have written single-row subqueries and multiple-row subqueries where only one column was compared in the WHERE clause or HAVING clause of the SELECT statement. If you want to compare two or more columns, you must write a compound WHERE clause using logical operators. Multiple-column subqueries enable you to combine duplicate WHERE conditions into a single WHERE clause. Syntax SELECT column, column , ... FROM table WHERE ( column, column , ...) IN (SELECT column, column , ... FROM table WHERE condition );
  4. Using Multiple-Column Subqueries The example on the slide is that of a multiple-column subquery because the subquery returns more than one column. It compares the values in the PRODID column and the QTY column of each candidate row in the ITEM table to the values in the PRODID column and QTY column for items in order 605. First, execute the subquery to see the PRODID and QTY values for each item in order 605. SQL&gt; SELECT prodid, qty 2 FROM item 3 WHERE ordid = 605; PRODID QTY ---------- --------- 100861 100 100870 500 100890 5 101860 50 101863 100 102130 10 6 rows selected.
  5. Using Multiple-Column Subqueries When the SQL statement on the slide is executed, the Oracle server compares the values in both the PRODID and QTY columns and returns those orders where the product number and quantity for that product match both the product number and quantity for an item in order 605. The output of the SQL statement is: ORDID PRODID QTY --------- --------- --------- 617 100861 100 617 100870 500 616 102130 10 The output shows that there are three items in other orders that contain the same product number and quantity as an item in order 605. For example, order 617 has ordered a quantity 500 of product 100870. Order 605 has also ordered a quantity 500 of product 100870. Therefore, that candidate row is part of the output. Class Management Note Demo: l7pair.sql Purpose: To illustrate a pairwise, multiple-column subquery.
  6. Pairwise Versus Nonpairwise Comparisons Column comparisons in a multiple-column subquery can be pairwise comparisons or nonpairwise comparisons. The slide shows the product numbers and quantities of the items in order 605. In the example on the previous slide, a pairwise comparison was executed in the WHERE clause. Each candidate row in the SELECT statement must have both the same product number and same quantity as an item in order 605. This is illustrated on the left side of the slide above. The arrows indicate that both the product number and quantity in a candidate row match a product number and quantity of an item in order 605. A multiple-column subquery can also be a nonpairwise comparison. If you want a nonpairwise comparison (a cross product), you must use a WHERE clause with multiple conditions. A candidate row must match the multiple conditions in the WHERE clause but the values are compared individually. A candidate row must match some product number in order 605 as well as some quantity in order 605, but these values do not need to be in the same row. This is illustrated on the right side of the slide. For example, product 102130 appears in other orders, one order matching the quantity in order 605 (10), and another order having a quantity of 500. The arrows show a sampling of the various quantities ordered for a particular product.
  7. Nonpairwise Comparison Subquery The slide example does a nonpairwise comparison of the columns. It displays the order number, product number, and quantity of any item in which the product number and quantity match any product number and quantity of an item in order 605. Order 605 is not included in the output. Class Management Note Demo: l7nonpair.sql Purpose: To illustrate a nonpairwise, multiple-column subquery.
  8. Nonpairwise Subquery The results of the nonpairwise subquery are shown in the slide. Sixteen candidate rows in the ITEM table match the multiple conditions in the WHERE clause. For example, an item from order 621 is returned from the SQL statement. A product in order 621 (product number 100861) matches a product in an item in order 605. The quantity for product 100861 in order 621 (10) matches the quantity in another item in order 605 (the quantity for product 102130). Class Management Note The highlighted sections in the slide indicate those rows that match some product in order 605, and some quantity in order 605. Note that the rows that are returned from the pairwise comparison are included in the output of the nonpairwise comparison. These rows are not highlighted.
  9. Returning Nulls in the Resulting Set of a Subquery The SQL statement on the slide attempts to display all the employees who do not have any subordinates. Logically, this SQL statement should have returned eight rows. However, the SQL statement does not return any rows. One of the values returned by the inner query is a null value and hence the entire query returns no rows. The reason is that all conditions that compare a null value result in a null. So whenever null values are likely to be part of the resultant set of a subquery, do not use the NOT IN operator. The NOT IN operator is equivalent to !=ALL. Notice that the null value as part of the resultant set of a subquery will not be a problem if you are using the IN operator. The IN operator is equivalent to =ANY. For example, to display the employees who have subordinates, use the following SQL statement: SQL&gt; SELECT employee.ename 2 FROM emp employee 3 WHERE employee.empno IN (SELECT manager.mgr 4 FROM emp manager); ENAME ---------- KING ... 6 rows selected.
  10. Using a Subquery in the FROM Clause You can use a subquery in the FROM clause of a SELECT statement, which is very similar to how views are used. A subquery in the FROM clause of a SELECT statement defines a data source for that particular SELECT statement, and only that SELECT statement. The slide example displays employee names, salaries, department numbers, and average salaries for all the employees who make more than the average salary in their department.
  11. Practice Overview In this practice, you will write multiple-value subqueries.