Home | Previous Page | Next Page   Appendix C. Application Tuning Examples > Using SQL Extensions >

Retrieving Only the Middle Rows from a Sorted Set

The MIDDLE clause adds selection capabilities similar to those that the FIRST clause provides. Use the MIDDLE clause to select one or more rows that fall in the middle set of values. To return the median values of an ordered set, use the ORDER BY clause.

For example, the following query selects the 10 employees with salaries in the median range:

SELECT MIDDLE 10 name, salary
FROM emp
ORDER BY salary DESC

The MIDDLE clause can replace complex SQL statements that use the FOR EACH and IF...THEN...ELIF...ENDIF construction in SPL routines or CURSOR statements to retrieve the middle rows of a sorted range, which provides a performance improvement over other formulations of this query.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]