Contents / Previous / Next


CASE Clause

Although SQL is not a procedural programming language, it does allow conditional control over the data returned from a query.

The WHERE clause uses comparisons to control row selection.
The CASE statement allows comparisons in column output.

Example:

SELECT id, name, CASE WHEN semester < 3 THEN 'junior' ELSE 'senior' END FROM stud ; id | name | case ----+------+-------- 1 | fred | junior 3 | tom | junior 4 | john | senior 2 | lisa | junior (4 rows) CASE allows the creation of conditional values, which can be used for output or for further processing in the same query.