SlideShare a Scribd company logo
DB2 interview question on SQL Query
This question is asked by one of the blog reader and the question is as below:
Table Name : Item
ItemIDItemName...
1 A
2 B
3 C
Table Name : Purchase
PurOrdNumItemID
45671 1
45672 3
45673 1
45674 1
45675 2
45676 3
45677 1
45678 1
Expected Output:
ItemIDItemNameItemsSoldMoreThanOnceIndicator
1 A 5 Y
3 C 2 Y
2 B 1 N
Solution:
There are two tables and they need to be matched against Item id and if they match,we need
to find how many times that combination has appeared in purchase table.Once done we
need to move Y if the count is more than 1 and N if the the count is 1 to the indicator.
The count can be achieved by joining the tables and by using GROUP BY.
Have used table names as
item table as ITEMTBL
Purchase table as PURTBL
The Query will look as below
SELECT
A.ITEMID ITM,
A.ITEMNAME ITMNAME,
COUNT(*) COUNT
FROM
ITEMTBL A,
PURTBL B
WHERE A.ITEMID = B.ITEMID
GROUP BY A.ITEMID,A.ITEMNAME
This will give the output as below:
---------+---------+-----
ITM ITMNAME COUNT
---------+---------+-----
1 A 5
2 B 1
3 C 2
But we cannot decide on the indicator in the same query as GROUP BY expects all columns
to be there with GROUP BY should only be there in SELECT .
If we have more columns in SELECT and all are not mentioned in GROUP BY then query
will not run and it will give you an error.
So have decided to use the above query as sub query and tried to derive the indicator based
on the count.
The final Query will look like as below:
SELECT ITM,ITMNAME,COUNT,
CASE COUNT
WHEN 1 THEN 'N'
ELSE 'Y'
END INDICATOR
FROM
(SELECT
A.ITEMID ITM,
A.ITEMNAME ITMNAME,
COUNT(*) COUNT
FROM
ITEMTBL A,
PURTBL B
WHERE A.ITEMID = B.ITEMID
GROUP BY A.ITEMID,A.ITEMNAME) AS TBL
CASE statement will derive the indicator as we expect based on the count field.
The output will be as below:
---------+---------+---------+---------+--
ITM ITMNAME COUNT INDICATOR
---------+---------+---------+---------+--
1 A 5 Y
2 B 1 N
3 C 2 Y
This query helps us to understand GROUP BY statement,considering sub query as a result
table and usage of CASE statement to derive some field based on any of the available
columns.
There is another way of achieving the same and same can be found in
http://clearmainframeinterview.blogspot.com/

More Related Content

What's hot

Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
primeteacher32
 
Number system
Number systemNumber system
Number system
ankush9927
 
Arithmetic Operations
Arithmetic OperationsArithmetic Operations
Arithmetic Operations
gueste99d9a
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Rakotoarison Louis Frederick
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
Bar graph
Bar graphBar graph
Bar graph
John Reinhardt
 
Python for Data Science
Python for Data SciencePython for Data Science
Python for Data Science
Panimalar Engineering College
 
Programming Problem 3
Programming Problem 3Programming Problem 3
Programming Problem 3
Dwight Sabio
 
Number System
Number SystemNumber System
Number System
MusPaintal
 
2-D array
2-D array2-D array
Floating Point Representation premium.pptx
Floating Point Representation premium.pptxFloating Point Representation premium.pptx
Floating Point Representation premium.pptx
shomikishpa
 
Lecture2 binary multiplication
Lecture2 binary multiplicationLecture2 binary multiplication
Lecture2 binary multiplication
景文 饶
 
Joins
JoinsJoins
Arithmetic circuits
Arithmetic circuitsArithmetic circuits
Arithmetic circuits
Sanjay Saluth
 
Complex inner joins
Complex inner joinsComplex inner joins
Complex inner joins
jfp05
 
How to use vlookup in MS Excel
How to use vlookup in MS ExcelHow to use vlookup in MS Excel
How to use vlookup in MS Excel
Jaspal Singh
 
Sql joins
Sql joinsSql joins
Sql joins
Siddhesh Palkar
 
Array
ArrayArray
Array
hjasjhd
 

What's hot (18)

Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
 
Number system
Number systemNumber system
Number system
 
Arithmetic Operations
Arithmetic OperationsArithmetic Operations
Arithmetic Operations
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
Bar graph
Bar graphBar graph
Bar graph
 
Python for Data Science
Python for Data SciencePython for Data Science
Python for Data Science
 
Programming Problem 3
Programming Problem 3Programming Problem 3
Programming Problem 3
 
Number System
Number SystemNumber System
Number System
 
2-D array
2-D array2-D array
2-D array
 
Floating Point Representation premium.pptx
Floating Point Representation premium.pptxFloating Point Representation premium.pptx
Floating Point Representation premium.pptx
 
Lecture2 binary multiplication
Lecture2 binary multiplicationLecture2 binary multiplication
Lecture2 binary multiplication
 
Joins
JoinsJoins
Joins
 
Arithmetic circuits
Arithmetic circuitsArithmetic circuits
Arithmetic circuits
 
Complex inner joins
Complex inner joinsComplex inner joins
Complex inner joins
 
How to use vlookup in MS Excel
How to use vlookup in MS ExcelHow to use vlookup in MS Excel
How to use vlookup in MS Excel
 
Sql joins
Sql joinsSql joins
Sql joins
 
Array
ArrayArray
Array
 

Viewers also liked

If NoSQL is your answer, you are probably asking the wrong question.
If NoSQL is your answer, you are probably asking the wrong question.If NoSQL is your answer, you are probably asking the wrong question.
If NoSQL is your answer, you are probably asking the wrong question.
Lukas Smith
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01
Getachew Ganfur
 
How to avoid the risk of being punished by Google algorithm updates?
How to avoid the risk of being punished by Google algorithm updates? How to avoid the risk of being punished by Google algorithm updates?
How to avoid the risk of being punished by Google algorithm updates?
Wojtek Blazalek
 
Pass4sure 640-864 Questions Answers
Pass4sure 640-864 Questions AnswersPass4sure 640-864 Questions Answers
Pass4sure 640-864 Questions Answers
Roxycodone Online
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
Yi-Lung Tsai
 
Question answering in linked data
Question answering in linked dataQuestion answering in linked data
Question answering in linked data
Reza Ramezani
 
Co question bank LAKSHMAIAH
Co question bank LAKSHMAIAH Co question bank LAKSHMAIAH
Co question bank LAKSHMAIAH
veena babu
 
Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf
Shriya agrawal
 
Csc 130 class 2 problem analysis and flow charts(2)
Csc 130 class 2   problem analysis and flow charts(2)Csc 130 class 2   problem analysis and flow charts(2)
Csc 130 class 2 problem analysis and flow charts(2)
Puneet narula
 
8 Bit A L U
8 Bit  A L U8 Bit  A L U
8 Bit A L U
stevencollins
 
Alu design-project
Alu design-projectAlu design-project
Alu design-project
alphankg1
 
8 bit alu design
8 bit alu design8 bit alu design
8 bit alu design
Shobhan Pujari
 
Computer questions with answers
Computer questions with answersComputer questions with answers
Computer questions with answers
letsguru guru
 
Normalization and Codd's Rule
Normalization and Codd's Rule Normalization and Codd's Rule
Normalization and Codd's Rule
lubna19
 
8 bit single cycle processor
8 bit single cycle processor8 bit single cycle processor
8 bit single cycle processor
Dhaval Kaneria
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
Rahul Borthakur
 
NETWORK COMPONENTS
NETWORK COMPONENTSNETWORK COMPONENTS
NETWORK COMPONENTS
bwire sedrick
 
100 general knowledge questions and answers gk quiz
100 general knowledge questions and answers   gk quiz100 general knowledge questions and answers   gk quiz
100 general knowledge questions and answers gk quiz
nands1234
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Oum Saokosal
 

Viewers also liked (20)

If NoSQL is your answer, you are probably asking the wrong question.
If NoSQL is your answer, you are probably asking the wrong question.If NoSQL is your answer, you are probably asking the wrong question.
If NoSQL is your answer, you are probably asking the wrong question.
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01
 
How to avoid the risk of being punished by Google algorithm updates?
How to avoid the risk of being punished by Google algorithm updates? How to avoid the risk of being punished by Google algorithm updates?
How to avoid the risk of being punished by Google algorithm updates?
 
Pass4sure 640-864 Questions Answers
Pass4sure 640-864 Questions AnswersPass4sure 640-864 Questions Answers
Pass4sure 640-864 Questions Answers
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Question answering in linked data
Question answering in linked dataQuestion answering in linked data
Question answering in linked data
 
Co question bank LAKSHMAIAH
Co question bank LAKSHMAIAH Co question bank LAKSHMAIAH
Co question bank LAKSHMAIAH
 
Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf
 
Csc 130 class 2 problem analysis and flow charts(2)
Csc 130 class 2   problem analysis and flow charts(2)Csc 130 class 2   problem analysis and flow charts(2)
Csc 130 class 2 problem analysis and flow charts(2)
 
8 Bit A L U
8 Bit  A L U8 Bit  A L U
8 Bit A L U
 
Alu design-project
Alu design-projectAlu design-project
Alu design-project
 
8 bit alu design
8 bit alu design8 bit alu design
8 bit alu design
 
Computer questions with answers
Computer questions with answersComputer questions with answers
Computer questions with answers
 
Normalization and Codd's Rule
Normalization and Codd's Rule Normalization and Codd's Rule
Normalization and Codd's Rule
 
8 bit single cycle processor
8 bit single cycle processor8 bit single cycle processor
8 bit single cycle processor
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
 
NETWORK COMPONENTS
NETWORK COMPONENTSNETWORK COMPONENTS
NETWORK COMPONENTS
 
100 general knowledge questions and answers gk quiz
100 general knowledge questions and answers   gk quiz100 general knowledge questions and answers   gk quiz
100 general knowledge questions and answers gk quiz
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
 

Similar to Db2 interview question on sql query.Explains GROUP BY and CASE Statement.

Crosstab query techniques
Crosstab query techniquesCrosstab query techniques
Crosstab query techniques
aabaap
 
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
 
Top N and bottom N view on the same worksheet In Tableau
Top N and bottom N view on the same worksheet In TableauTop N and bottom N view on the same worksheet In Tableau
Top N and bottom N view on the same worksheet In Tableau
Ranganna Naik
 
Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptxServer Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
auzee32
 
003.query
003.query003.query
Simple Spreadsheet Tips
Simple Spreadsheet TipsSimple Spreadsheet Tips
Simple Spreadsheet Tips
Inside Access
 
data-exp-Viz-00-2.pdf
data-exp-Viz-00-2.pdfdata-exp-Viz-00-2.pdf
data-exp-Viz-00-2.pdf
betsegaw123
 
Correlated update vs merge
Correlated update vs mergeCorrelated update vs merge
Correlated update vs merge
Heribertus Bramundito
 
New folderAssignment _Sem2_2013.docxITECH10005000 Programmin.docx
New folderAssignment _Sem2_2013.docxITECH10005000 Programmin.docxNew folderAssignment _Sem2_2013.docxITECH10005000 Programmin.docx
New folderAssignment _Sem2_2013.docxITECH10005000 Programmin.docx
henrymartin15260
 
Part2 (1 Examen)
Part2 (1 Examen)Part2 (1 Examen)
Part2 (1 Examen)
guestcf14bd2
 
Pivot table
Pivot tablePivot table
Pivot table
Mukesh Tekwani
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
Best SEO Tampa
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
MrHello6
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Bis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-newBis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-new
assignmentcloud85
 
284566820 1 z0-061(1)
284566820 1 z0-061(1)284566820 1 z0-061(1)
284566820 1 z0-061(1)
panagara
 
Cs practical file
Cs practical fileCs practical file
Cs practical file
Shailendra Garg
 
HALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONSHALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONS
Talia Strnad
 
SQL CHEAT SHEET
SQL CHEAT SHEETSQL CHEAT SHEET
SQL CHEAT SHEET
GadiOrellana
 
Bis 345-final-exam-guide-set-2-new
Bis 345-final-exam-guide-set-2-newBis 345-final-exam-guide-set-2-new
Bis 345-final-exam-guide-set-2-new
assignmentcloud85
 

Similar to Db2 interview question on sql query.Explains GROUP BY and CASE Statement. (20)

Crosstab query techniques
Crosstab query techniquesCrosstab query techniques
Crosstab query techniques
 
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)
 
Top N and bottom N view on the same worksheet In Tableau
Top N and bottom N view on the same worksheet In TableauTop N and bottom N view on the same worksheet In Tableau
Top N and bottom N view on the same worksheet In Tableau
 
Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptxServer Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
 
003.query
003.query003.query
003.query
 
Simple Spreadsheet Tips
Simple Spreadsheet TipsSimple Spreadsheet Tips
Simple Spreadsheet Tips
 
data-exp-Viz-00-2.pdf
data-exp-Viz-00-2.pdfdata-exp-Viz-00-2.pdf
data-exp-Viz-00-2.pdf
 
Correlated update vs merge
Correlated update vs mergeCorrelated update vs merge
Correlated update vs merge
 
New folderAssignment _Sem2_2013.docxITECH10005000 Programmin.docx
New folderAssignment _Sem2_2013.docxITECH10005000 Programmin.docxNew folderAssignment _Sem2_2013.docxITECH10005000 Programmin.docx
New folderAssignment _Sem2_2013.docxITECH10005000 Programmin.docx
 
Part2 (1 Examen)
Part2 (1 Examen)Part2 (1 Examen)
Part2 (1 Examen)
 
Pivot table
Pivot tablePivot table
Pivot table
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
 
Bis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-newBis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-new
 
284566820 1 z0-061(1)
284566820 1 z0-061(1)284566820 1 z0-061(1)
284566820 1 z0-061(1)
 
Cs practical file
Cs practical fileCs practical file
Cs practical file
 
HALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONSHALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONS
 
SQL CHEAT SHEET
SQL CHEAT SHEETSQL CHEAT SHEET
SQL CHEAT SHEET
 
Bis 345-final-exam-guide-set-2-new
Bis 345-final-exam-guide-set-2-newBis 345-final-exam-guide-set-2-new
Bis 345-final-exam-guide-set-2-new
 

Recently uploaded

一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
Reetu63
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
KrishnaveniMohan1
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
TMU毕业证书精仿办理
TMU毕业证书精仿办理TMU毕业证书精仿办理
TMU毕业证书精仿办理
aeeva
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
sandeepmenon62
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
campbellclarkson
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Beginner's Guide to Observability@Devoxx PL 2024
Beginner's  Guide to Observability@Devoxx PL 2024Beginner's  Guide to Observability@Devoxx PL 2024
Beginner's Guide to Observability@Devoxx PL 2024
michniczscribd
 
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in NashikUpturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio, Inc.
 
The Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdfThe Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdf
mohitd6
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
OnePlan Solutions
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
Alina Yurenko
 
Stork Product Overview: An AI-Powered Autonomous Delivery Fleet
Stork Product Overview: An AI-Powered Autonomous Delivery FleetStork Product Overview: An AI-Powered Autonomous Delivery Fleet
Stork Product Overview: An AI-Powered Autonomous Delivery Fleet
Vince Scalabrino
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Peter Caitens
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 

Recently uploaded (20)

一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
TMU毕业证书精仿办理
TMU毕业证书精仿办理TMU毕业证书精仿办理
TMU毕业证书精仿办理
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Beginner's Guide to Observability@Devoxx PL 2024
Beginner's  Guide to Observability@Devoxx PL 2024Beginner's  Guide to Observability@Devoxx PL 2024
Beginner's Guide to Observability@Devoxx PL 2024
 
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in NashikUpturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in Nashik
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
 
The Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdfThe Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdf
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
 
Stork Product Overview: An AI-Powered Autonomous Delivery Fleet
Stork Product Overview: An AI-Powered Autonomous Delivery FleetStork Product Overview: An AI-Powered Autonomous Delivery Fleet
Stork Product Overview: An AI-Powered Autonomous Delivery Fleet
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 

Db2 interview question on sql query.Explains GROUP BY and CASE Statement.

  • 1. DB2 interview question on SQL Query This question is asked by one of the blog reader and the question is as below: Table Name : Item ItemIDItemName... 1 A 2 B 3 C Table Name : Purchase PurOrdNumItemID 45671 1 45672 3 45673 1 45674 1 45675 2 45676 3 45677 1 45678 1 Expected Output: ItemIDItemNameItemsSoldMoreThanOnceIndicator 1 A 5 Y 3 C 2 Y 2 B 1 N Solution: There are two tables and they need to be matched against Item id and if they match,we need to find how many times that combination has appeared in purchase table.Once done we need to move Y if the count is more than 1 and N if the the count is 1 to the indicator. The count can be achieved by joining the tables and by using GROUP BY. Have used table names as item table as ITEMTBL Purchase table as PURTBL The Query will look as below SELECT A.ITEMID ITM, A.ITEMNAME ITMNAME, COUNT(*) COUNT FROM ITEMTBL A, PURTBL B WHERE A.ITEMID = B.ITEMID GROUP BY A.ITEMID,A.ITEMNAME This will give the output as below: ---------+---------+----- ITM ITMNAME COUNT ---------+---------+-----
  • 2. 1 A 5 2 B 1 3 C 2 But we cannot decide on the indicator in the same query as GROUP BY expects all columns to be there with GROUP BY should only be there in SELECT . If we have more columns in SELECT and all are not mentioned in GROUP BY then query will not run and it will give you an error. So have decided to use the above query as sub query and tried to derive the indicator based on the count. The final Query will look like as below: SELECT ITM,ITMNAME,COUNT, CASE COUNT WHEN 1 THEN 'N' ELSE 'Y' END INDICATOR FROM (SELECT A.ITEMID ITM, A.ITEMNAME ITMNAME, COUNT(*) COUNT FROM ITEMTBL A, PURTBL B WHERE A.ITEMID = B.ITEMID GROUP BY A.ITEMID,A.ITEMNAME) AS TBL CASE statement will derive the indicator as we expect based on the count field. The output will be as below: ---------+---------+---------+---------+-- ITM ITMNAME COUNT INDICATOR ---------+---------+---------+---------+-- 1 A 5 Y 2 B 1 N 3 C 2 Y This query helps us to understand GROUP BY statement,considering sub query as a result table and usage of CASE statement to derive some field based on any of the available columns. There is another way of achieving the same and same can be found in http://clearmainframeinterview.blogspot.com/