Range

We wish to obtain the range (minimum and maximum) values of a column or a column.

Note that while it is most common to compute the range for numeric data, we may compute the range ( min and max values) for other data types to mean the following:

  • For numeric: The smallest and largest numeric values.
  • For string: The alphabetically first and last string values.
  • For date-time: The earliest and latest date-time values.

Min or Max

We wish to obtain the minimum (or maximum) value of a column of a table.

In this example, we wish to obtain the minimum and maximum values of the column col_2 for each group where the groups are defined by the column col_1.

SELECT col_1,
       MIN(col_2) col_2_min,
       MAX(col_2) col_2_max
FROM table_1
GROUP BY col_1

Here is how this works:

  • We use MIN() and MAX() from base R to identify the minimum and maximum value in a vector of values respectively.
  • Note that both MIN and MAX ignore NULL values by default.
SQL
I/O