SlideShare a Scribd company logo
1 of 2
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

Lecture2 binary multiplication
Lecture2 binary multiplicationLecture2 binary multiplication
Lecture2 binary multiplication
景文 饶
 
Complex inner joins
Complex inner joinsComplex inner joins
Complex inner joins
jfp05
 

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

Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01
Getachew Ganfur
 
Co question bank LAKSHMAIAH
Co question bank LAKSHMAIAH Co question bank LAKSHMAIAH
Co question bank LAKSHMAIAH
veena babu
 
Alu design-project
Alu design-projectAlu design-project
Alu design-project
alphankg1
 

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
 
Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptxServer Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
auzee32
 
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
 
HALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONSHALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONS
Talia Strnad
 

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

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 

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/