SQL/MP Query Guide

Retrieving Data: How to Write Queries
HP NonStop SQL/MP Query Guide524488-003
1-9
Specifying Search Conditions
The WHERE clause and HAVING clause are described next. The ON clause is
described in Combining Data From More Than One Table on page 1-51.
The WHERE Clause
Suppose that you want a report of all employees whose salaries are greater than
$50,000. You can add a WHERE clause to restrict the number of rows returned. Only
those employees who earn more than $50,000 are included in the report.
Example 1-6 shows the query and its results.
The HAVING Clause
You can use the HAVING clause to restrict groups selected by a prior GROUP BY
clause; you should use it only in conjunction with the GROUP BY clause.
Example 1-7 shows a SELECT statement with a HAVING clause. Note that the part
numbers with a total quantity ordered of 20 or less (such as 212) do not appear in the
result:
Example 1-6. SELECT Statement With WHERE Clause
SELECT LAST_NAME, FIRST_NAME, SALARY
FROM EMPLOYEE
WHERE SALARY > 50000 ;
LAST_NAME FIRST_NAME SALARY
------------- ------------- ----------
GREEN ROGER 175500.00
. . .
. . .
MILLER MARY 56000.00
HENDERSON BEN 65000.00
--- 16 row(s) selected.
Example 1-7. SELECT Statement With HAVING Clause
SELECT PARTNUM, SUM (QTY_ORDERED)
FROM ODETAIL
GROUP BY PARTNUM
HAVING SUM (QTY_ORDERED) > 20 ;
PARTNUM (EXPR)
------- ----------
244 47
. .
. .
7301 96
--- 20 row(s) selected.