Tuesday, December 14, 2010

Selecting Maximum and Minimum Values

You can find the maximum value in a column as follows:

SQL> select max(username) from dba_users
  2  /

MAX(USERNAME)
------------------------------
ZEBRA

SQL>


I'm not suggesting you do this at home but you can check the value returned as follows. This SQL uses a correlated subquery. I will try to look at these in more detail in a future post.

SQL> select username from dba_users x
  2  where not exists
  3  (select username from dba_users
  4   where username > x.username)
  5  /

USERNAME
------------------------------
ZEBRA

SQL>


This is how you find the minimum value:

SQL> select min(username) from dba_users
  2  /

MIN(USERNAME)
------------------------------
ANONYMOUS

SQL>

No comments: