If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Does HAVING clause accept wildcards?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-21-09, 22:10
un4dbf un4dbf is offline
Registered User
 
Join Date: Jun 2009
Posts: 3
Does HAVING clause accept wildcards?

Which two of the following statements are true about the HAVING clause?
A. The HAVING clause is used in place of the WHERE clause.
B. The HAVING clause uses the same syntax as the WHERE clause.
C. The HAVING clause can only be used with the GROUP BY clause.
D. The HAVING clause accepts wildcards.
E. The HAVING clause uses the same syntax as the IN clause.

I would say answer is B,C,D... DBAs do you agree?

I know B and C are 100% true... what abt D, Does HAVING clause accept wildcards?
Reply With Quote
  #2 (permalink)  
Old 06-22-09, 01:16
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Which two of the following statements are true about the HAVING clause?
Reply With Quote
  #3 (permalink)  
Old 06-22-09, 02:41
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by un4dbf
D. The HAVING clause accepts wildcards.
The following is valid SQL:
Code:
SELECT col1, COUNT(*) FROM tbl GROUP BY col1
HAVING MIN(col2) LIKE 'A%'
(meaning, under some assumptions: only show groups that have at least an entry starting with an "A".)
Quote:
Originally Posted by un4dbf
E. The HAVING clause uses the same syntax as the IN clause.
The following is valid SQL:
Code:
SELECT col1, COUNT(*) FROM tbl GROUP BY col1
HAVING SUM(col3) IN (20, 30, 40)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #4 (permalink)  
Old 06-22-09, 11:22
un4dbf un4dbf is offline
Registered User
 
Join Date: Jun 2009
Posts: 3
So you mean, all below 3 are true?
B. The HAVING clause uses the same syntax as the WHERE clause.
C. The HAVING clause can only be used with the GROUP BY clause.
D. The HAVING clause accepts wildcards.
Reply With Quote
  #5 (permalink)  
Old 06-22-09, 12:17
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by un4dbf
So you mean, all below 3 are true?
B. The HAVING clause uses the same syntax as the WHERE clause.
C. The HAVING clause can only be used with the GROUP BY clause.
D. The HAVING clause accepts wildcards.
Actually, I would personally go for C and D.
B is true, but to a lesser extent than D.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #6 (permalink)  
Old 06-22-09, 18:39
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
I would go for B and C.

By 'uses the same syntax as the WHERE clause', that means you can use an IN clause or a Like in a compare

C is a definite. You need a Group By to use a Having clause as it filters the result of the Group By.

(It really is a poorly worded list of answers, however)
Reply With Quote
  #7 (permalink)  
Old 06-22-09, 21:07
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by Stealth_DBA
I would go for B and C.

By 'uses the same syntax as the WHERE clause', that means you can use an IN clause or a Like in a compare
I doubt that. There are quite a few restrictions on what column references and functions you can use in the HAVING clause, especially if GROUP BY is also present. For that reason I would conclude that HAVING does _not_ use the same syntax as WHERE.
Reply With Quote
  #8 (permalink)  
Old 06-22-09, 22:31
un4dbf un4dbf is offline
Registered User
 
Join Date: Jun 2009
Posts: 3
DB2 9 Fundamentals Certification Study Guide by Roger E. Sanders says B and C as correct answers. I totally disagree with this as i have personally coded HAVING clause with wildcards and it worked with out any issues.
Reply With Quote
  #9 (permalink)  
Old 06-22-09, 22:47
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
I think the question should either be removed or restructured. While I thought that "C. The HAVING clause can only be used with the GROUP BY clause." was a given (as I have only used or seen it used in that fashion), according to the DB2 V9.5 SQL Reference, Vol.1:
Quote:
When HAVING is used without GROUP BY, the select list can only include column names when they are arguments to an aggregate function, correlated column references, global variables, host variables, literals, special registers, SQL variables, or SQL parameters.
So, in certain situations, you do NOT have to have a GROUP BY when using a HAVING clause.
Reply With Quote
  #10 (permalink)  
Old 10-08-09, 14:54
dinerroll1066 dinerroll1066 is offline
Registered User
 
Join Date: Oct 2009
Posts: 5
I know this thread is old... but i couldn't resist:

C is false, while HAVING is typically used with a GROUP BY, using it without one will still work. The optimizer will treat the whole resultset as one group and do the filtering on that... effectively making the HAVING logically function like a where.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On