pcplans.com § Help and Support How to: using SQL the HAVING clause  

How to: using SQL the HAVING clause

Article ID:9940
Last Review:Thursday, January 08, 2009
Revision:3.0

INTRODUCTION

How to: using SQL the HAVING clause

symptomsIMPORTANT

How to: using SQL the HAVING clause


IMPORTANT

How to: using SQL the HAVING clause
The HAVING clause should follow the GROUP BY clause if you are going to use it.
back to the top

How to: using SQL the HAVING clause


HAVING clause
The HAVING clause allows you to specify conditions on the rows for each group - in other words, which rows should be selected will be based on the conditions you specify. The HAVING clause should follow the GROUP BY clause if you are going to use it.
HAVING clause syntax:


SELECT column1, 
SUM(column2)
FROM "list-of-tables"
GROUP BY "column-list"
HAVING "condition";

HAVING can best be described by example. Let's say you
have an employee table containing the employee's name, department,
salary, and age. If you would like to select the average salary for
each employee in each department, you could enter:

SELECT dept, avg(salary)
FROM employee
GROUP BY dept;

But, let's say that you want to ONLY calculate & display the average
if their salary is over 20000:



SELECT dept, avg(salary)
FROM employee
GROUP BY dept
HAVING avg(salary) > 20000;





  • NOTE: How to: using SQL the HAVING clause
  • back to the top




    Last Reviewed:1/8/2009
    Keywords: kbHow kbhowtoHow kbHOWTOHow #9940 kbAudITProHow



        Contact

       ©1999-2008 Support at pcplans.com