Exists Predicate - HP Neoview SQL Reference Manual

Hide thumbs Also See for Neoview SQL:
Table of Contents

Advertisement

...
--- 35 row(s) selected.
The first subquery of this query determines the minimum salary of employees from other
departments whose salary is greater than the average salary for department 1500. The main
query then finds the names of employees who are not in department 1500 and whose salary
is less than the minimum salary determined by the first subquery.

EXISTS Predicate

The EXISTS predicate determines whether any rows are selected by a subquery. If the subquery
finds at least one row that satisfies its search condition, the predicate evaluates to true. Otherwise,
if the result table of the subquery is empty, the predicate is false.
[NOT] EXISTS subquery
subquery
specifies the operand of the predicate. A subquery is a query expression enclosed in
parentheses. An EXISTS subquery is typically correlated with an outer query. See
(page
307).
Examples of EXISTS
Find locations of employees with job code 300:
SELECT deptnum, location FROM persnl.dept D
WHERE EXISTS
(SELECT jobcode FROM persnl.employee E
WHERE D.deptnum = E.deptnum AND jobcode = 300);
DEPTNUM
-------
3000
3100
3200
3300
3500
--- 5 row(s) selected.
In the preceding example, the EXISTS predicate contains a subquery that determines which
locations have employees with job code 300. The subquery depends on the value of
D.DEPTNUM from the outer query and must be evaluated for each row of the result table
where D.DEPTNUM equals E.DEPTNUM. The column D.DEPTNUM is an example of an
outer reference.
Search for departments that have no employees with job code 420:
SELECT deptname FROM persnl.dept D
WHERE NOT EXISTS
(SELECT jobcode FROM persnl.employee E
WHERE D.deptnum = E.deptnum AND jobcode = 420);
DEPTNAME
------------
FINANCE
PERSONNEL
INVENTORY
...
--- 11 row(s) selected.
Search for parts with less than 20 units in the inventory:
SELECT partnum, suppnum
FROM invent.partsupp PS
LOCATION
-------------
NEW YORK
TORONTO
FRANKFURT
LONDON
HONG KONG
"Subquery"
Predicates
291

Advertisement

Table of Contents
loading

Table of Contents