GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Mainframe  >  DB2
Go To First  |  Previous Question  |  Next Question 
 DB2  |  Question 194 of 198    Print  
Group By
Can you select any column in a Group by query even if that column is not groupable?
e.g. Select Dept, Empno, Ave(salary) From T1 Group by Dept
Can we select empno in the above query?



  
Total Answers and Comments: 4 Last Update: August 07, 2009     Asked by: madhurap 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Vaishali Sisodia
 
Group By nothing but it is a summarization(grouped) of rows on tha basis of column or columns. It apply some sort of aggregate function to certain columns like
SELECT name, sum(salary)
FROM emp Where
GROUP BY name;
* In this SELECT statement name column doesnt have aggregate function so it should be GROUP BY.

Having Clause is used only with GROUP BY clause for filtration purpose. When u want to apply some condition with in SELECT statement which use GROUP BY clause, use HAVING clause to apply condition. like
SELECT department, SUM(sales) as "Total sales"
FROM order_details
GROUP BY department
HAVING SUM(sales) > 1000;
*In this SELECT statement HAVING clause is used to filter the records that a GROUP BY returns.

Order BY clause is used to sort the records in Ascending or Descending Order. By default it sort the records in Ascending Order. ORDER BY clause can only be used in SELECT statements.
(without GROUP BY) like
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city;

(With Group By) Like
SELECT department, SUM(sales) as "Total sales"
FROM order_details
GROUP BY department
HAVING SUM(sales) > 1000
ORDER BY department;

SELECT name, dept, SUM(salary)
FROM emp
GROUP BY name;
* This SELECT statement give ERROR (in SPUFI, It gives -112 Error Code). because GROUP BY clause didnt apply to dept column.
* if any column have aggregate function(salary) in SELECT Statement, u should apply GROUP BY Clause to other columns which dont have Aggregate function(name, dept).

Correct SELECT Statement :  

SELECT name, dept, SUM(salary)
FROM emp
GROUP BY name, dept;

Plz Confirm my Ans is correct or not.



Above answer was rated as good by the following members:
kingP
August 07, 2008 06:54:52   #1  
Upendrapcu Member Since: July 2008   Contribution: 2    

RE: Group By
Yes we can select the emp no field
 
Is this answer useful? Yes | No
December 05, 2008 02:45:50   #2  
Vaishali Sisodia Member Since: December 2008   Contribution: 1    

RE: Group By
Group By nothing but it is a summarization(grouped) of rows on tha basis of column or columns. It apply some sort of aggregate function to certain columns like
SELECT name sum(salary)
FROM emp Where
GROUP BY name;
* In this SELECT statement name column doesnt have aggregate function so it should be GROUP BY.

Having Clause is used only with GROUP BY clause for filtration purpose. When u want to apply some condition with in SELECT statement which use GROUP BY clause use HAVING clause to apply condition. like
SELECT department SUM(sales) as "Total sales"
FROM order_details
GROUP BY department
HAVING SUM(sales) > 1000;
*In this SELECT statement HAVING clause is used to filter the records that a GROUP BY returns.

Order BY clause is used to sort the records in Ascending or Descending Order. By default it sort the records in Ascending Order. ORDER BY clause can only be used in SELECT statements.
(without GROUP BY) like
SELECT supplier_city
FROM suppliers
WHERE supplier_name 'IBM'
ORDER BY supplier_city;

(With Group By) Like
SELECT department SUM(sales) as "Total sales"
FROM order_details
GROUP BY department
HAVING SUM(sales) > 1000
ORDER BY department;

SELECT name dept SUM(salary)
FROM emp
GROUP BY name;
* This SELECT statement give ERROR (in SPUFI It gives -112 Error Code). because GROUP BY clause didnt apply to dept column.
* if any column have aggregate function(salary) in SELECT Statement u should apply GROUP BY Clause to other columns which dont have Aggregate function(name dept).

Correct SELECT Statement :

SELECT name dept SUM(salary)
FROM emp
GROUP BY name dept;

Plz Confirm my Ans is correct or not.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
December 19, 2008 19:48:45   #3  
serken Member Since: December 2008   Contribution: 8    

RE: Group By
Instead of typing about 100 lines in previous answer the actual answer to the original question is:

NO EMPNO CANNOT BE SELECTED IN THIS WAY.

Period.

 
Is this answer useful? Yes | No
August 07, 2009 03:02:21   #4  
Anand_Hakke Member Since: August 2009   Contribution: 1    

RE: Group By
No It is not posssible to select ungrouped column in a query.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape