0% found this document useful (0 votes)
11 views53 pages

Notes-FUNCTION in SQL Snowflake

The document provides an overview of SQL functions supported by the Snowflake platform, focusing on scalar functions that return a single value per invocation. It details various categories of functions, including conversion, date & time, numeric, and string functions, while noting which functions are included in the syllabus. Additionally, it includes syntax and examples for key functions like CAST, DATE_PART, and LAST_DAY.

Uploaded by

jidneshpawar4727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views53 pages

Notes-FUNCTION in SQL Snowflake

The document provides an overview of SQL functions supported by the Snowflake platform, focusing on scalar functions that return a single value per invocation. It details various categories of functions, including conversion, date & time, numeric, and string functions, while noting which functions are included in the syllabus. Additionally, it includes syntax and examples for key functions like CAST, DATE_PART, and LAST_DAY.

Uploaded by

jidneshpawar4727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Department of Computer Engineering

NOTES ON SQL FUNCTIONS


Using
Snowflake Platform
About functions
Snowflake supports most of the standard functions defined in SQL:1999, as well as
parts of the SQL:2003 analytic extensions.

Note: Only the red highlighted functions are kept in your syllabus.

Scalar Functions
A scalar function is a function that returns one value per invocation; in most cases,
you can think of this as returning one value per row. This contrasts with Aggregate
Functions, which return one value per group of rows.

Category Description
Included in the Syllabus
Conversion Functions Convert expressions from one data type to another data
type.
Date & Time Functions Manipulate dates, times, and timestamps.
Numeric Functions Perform rounding, truncation, exponent, root, logarithmic,
and trigonometric operations on numeric values.
String & Binary Functions Manipulate and transform string input.

Not included in the syllabus


Bitwise Expression Perform bitwise operations on expressions.
Functions
Conditional Expression Manipulate conditional expressions.
Functions
Context Functions Provide contextual information about the current environment,
session, and object.

Data Generation Functions Generate random or sequential values.

Encryption Functions Perform encryption and decryption on VARCHAR or


BINARY values.
File Functions Access files staged in cloud storage.
Department of Computer Engineering
Category Description
Geospatial Functions Work with geospatial data.

Semi-structured and Work with semi-structured data (JSON, Avro, etc.).


Structured Data Functions

String Functions (Regular Subset of strings functions for performing operations on items
Expressions) that match a regular expression.
Hash Functions Hash values to signed 64-bit integers using a deterministic
algorithm.
Metadata Functions Retrieve data or metadata about database objects (e.g. tables)
or files (e.g. staged files).

 Aggregate Functions — functions that take multiple rows/values as


input and return a single value.
 Window Functions — subset of aggregate functions that can operate on a
subset of rows.
 Table Functions — functions that return results in tabular format.
 System Functions — functions that perform control operations or return
system-level information.

-----------------------------------------------------------------------------------------------
Scalar Functions — functions that take a single row/value as input and return a
single value:

1. Conversion Functions

CAST , ::
Converts a value of one data type into another data type. The semantics of CAST
are the same as the semantics of the corresponding TO_ datatype conversion
functions. If the cast is not possible, an error is raised. For more details, see the
individual TO_ datatype conversion functions.

The :: operator provides alternative syntax for CAST.

Syntax
Department of Computer Engineering
CAST( <source_expr> AS <target_data_type> )

[ RENAME FIELDS | ADD FIELDS ]

<source_expr> :: <target_data_type>

Arguments
source_expr

Expression of any supported data type to be converted into a different data


type.

target_data_type

The data type to which to convert the expression. If the data type supports
additional properties, such as precision and scale (for numbers/decimals),
the properties can be included.

RENAME FIELDS

For structured OBJECTs, specifies that you want to change the OBJECT to
use different key-value pairs. The values in the original object are copied to
the new key-value pairs in the order in which they appear.

For an example, see Example: Changing the Key Names in an OBJECT.

ADD FIELDS

For structured OBJECTs, specifies that you want to add key-value pairs to the
OBJECT.

For an example, see Example: Adding Keys to an OBJECT.

The values for the newly added keys will be set to NULL. If you want to
assign a value to these keys, call the OBJECT_INSERT function instead.

Examples
Convert a string containing a number to a decimal with specified scale (2):
Department of Computer Engineering
SELECT CAST('1.2345' AS DECIMAL(15,2));
+---------------------------------+
| CAST('1.2345' AS DECIMAL(15,2)) |
|---------------------------------|
| 1.23 |
+---------------------------------+

Convert the same string to a decimal with scale 5, using the :: notation:

SELECT '1.2345'::DECIMAL(15,5);
+-------------------------+
| '1.2345'::DECIMAL(15,5) |
|-------------------------|
| 1.23450 |
+-------------------------+

Convert a number to an integer:

SELECT CAST(1.56 AS INTEGER);


+-----------------------+
| CAST(1.56 AS INTEGER) |
|-----------------------|
| 2|
+-----------------------+

Convert a string containing a date to a timestamp:

SELECT CAST('05-Mar-2014' AS TIMESTAMP);


+----------------------------------+
| CAST('05-MAR-2014' AS TIMESTAMP) |
|----------------------------------|
| 2014-03-05 00:00:00.000 |
+----------------------------------+

2. Date & Time Functions


This family of functions can be used to construct, convert, extract, or modify
DATE/TIME/TIMESTAMP data.

List of Functions
Department of Computer Engineering
Sub-category Function Notes

Extraction DATE_PART Accepts all date and


time parts (see next
section for details).
DAYNAME
EXTRACT Alternative
for DATE_PART.
HOUR / MINUTE / SECOND Alternative
for DATE_PART.
LAST_DAY Accepts relevant date
parts (see next
section for details).
MONTHNAME
NEXT_DAY
PREVIOUS_DAY
YEAR* / DAY* / WEEK* / MONTH / Alternative
QUARTER for DATE_PART.

Not included in the Syllabus


Construction DATE_FROM_PARTS
TIME_FROM_PARTS

TIMESTAMP_FROM_PARTS

Addition/Subtraction ADD_MONTHS
DATEADD Accepts relevant date
and time parts (see
next section for
details).
DATEDIFF Accepts relevant date
and time parts (see
next section for
details).
MONTHS_BETWEEN (included in the
syllabus)
TIMEADD Alias for DATEADD.
TIMEDIFF Alias for DATEDIFF.
TIMESTAMPADD Alias for DATEADD.
TIMESTAMPDIFF Alias for DATEDIFF.
Department of Computer Engineering
Sub-category Function Notes

Truncation DATE_TRUNC Accepts relevant date


and time parts (see
next section for
details).
TIME_SLICE Allows a time to be
“rounded” to the start
of an evenly-spaced
interval.
TRUNC Alternative
for DATE_TRUNC.
Conversion TO_DATE , DATE
TO_TIME , TIME
TO_TIMESTAMP / TO_TIMESTAMP_*
Time Zone CONVERT_TIMEZONE
Alerts LAST_SUCCESSFUL_SCHEDULED_TIME
SCHEDULED_TIME

2.1 DATE_PART
Extracts the specified date or time part from a date, time, or timestamp.

Alternatives:
Department of Computer Engineering
EXTRACT , HOUR / MINUTE / SECOND , YEAR* / DAY* / WEEK* /
MONTH / QUARTER

Syntax
DATE_PART( <date_or_time_part> , <date_or_time_expr> )

Returns
The data type of the return value is NUMBER.

Usage Notes
 date_or_time_part must be one of the values listed in Supported Date and Time
Parts.
 When date_or_time_part is week (or any of its variations), the output is
controlled by the WEEK_START session parameter.
 When date_or_time_part is dayofweek or yearofweek (or any of their
variations), the output is controlled by
the WEEK_OF_YEAR_POLICY and WEEK_START session
parameters.

For more details, including examples, see Calendar Weeks and Weekdays.

 date_or_time_expr can be a date, time, or timestamp.

Examples
This shows a simple example of extracting part of a DATE:

SELECT DATE_PART(QUARTER, '2013-05-08'::DATE);


+----------------------------------------+
| DATE_PART(QUARTER, '2013-05-08'::DATE) |
|----------------------------------------|
| 2|
+----------------------------------------+

This shows an example of extracting part of a TIMESTAMP:

SELECT TO_TIMESTAMP('2013-05-08T23:39:20.123-07:00') AS
"TIME_STAMP1",
Department of Computer Engineering
DATE_PART(YEAR, "TIME_STAMP1") AS "EXTRACTED YEAR";
+-------------------------+----------------+
| TIME_STAMP1 | EXTRACTED YEAR |
|-------------------------+----------------|
| 2013-05-08 23:39:20.123 | 2013 |
+-------------------------+----------------+

This shows an example of converting a TIMESTAMP to the number of seconds


since the beginning of the Unix epoch (midnight January 1, 1970):

SELECT TO_TIMESTAMP('2013-05-08T23:39:20.123-07:00') AS
"TIME_STAMP1",
DATE_PART(EPOCH_SECOND, "TIME_STAMP1") AS "EXTRACTED
EPOCH SECOND";
+-------------------------+------------------------+
| TIME_STAMP1 | EXTRACTED EPOCH SECOND |
|-------------------------+------------------------|
| 2013-05-08 23:39:20.123 | 1368056360 |
+-------------------------+------------------------+

This shows an example of converting a TIMESTAMP to the number of


milliseconds since the beginning of the Unix epoch (midnight January 1, 1970):

SELECT TO_TIMESTAMP('2013-05-08T23:39:20.123-07:00') AS
"TIME_STAMP1",
DATE_PART(EPOCH_MILLISECOND, "TIME_STAMP1") AS
"EXTRACTED EPOCH MILLISECOND";
+-------------------------+-----------------------------+
| TIME_STAMP1 | EXTRACTED EPOCH MILLISECOND |
|-------------------------+-----------------------------|
| 2013-05-08 23:39:20.123 | 1368056360123 |
+-------------------------+-----------------------------+

Categories:

Date & Time Functions

2.2 EXTRACT
Extracts the specified date or time part from a date, time, or timestamp.
Department of Computer Engineering
Alternative for DATE_PART.

Syntax
EXTRACT( <date_or_time_part> FROM <date_or_time_expr> )

Usage Notes
 date_or_time_part must be one of the values listed in Supported Date and Time
Parts.
 For additional usage notes, see Returns for DATE_PART.

Examples
SELECT EXTRACT(YEAR FROM TO_TIMESTAMP('2013-05-
08T23:39:20.123-07:00')) AS v
FROM (values(1)) v1;

------+
V |
------+
2013 |
------+

2.3 HOUR / MINUTE / SECOND


Extracts the corresponding time part from a time or timestamp value.

These functions are alternatives to using the DATE_PART (or EXTRACT) function
with the equivalent time part (see Supported Date and Time Parts).

Syntax
HOUR( <time_or_timestamp_expr> )

MINUTE( <time_or_timestamp_expr> )

SECOND( <time_or_timestamp_expr> )
Department of Computer Engineering
Usage Notes
Function Name Time Part Extracted from Time / Timestamp Possible Values
HOUR Hour of the specified day 0 to 23
MINUTE Minute of the specified hour 0 to 59
SECOND Second of the specified minute 0 to 59

Examples
This demonstrates the HOUR, MINUTE, and SECOND functions:

SELECT '2013-05-08T23:39:20.123-07:00'::TIMESTAMP AS TSTAMP,


HOUR(tstamp) AS "HOUR",
MINUTE(tstamp) AS "MINUTE",
SECOND(tstamp) AS "SECOND";
+-------------------------+------+--------+--------+
| TSTAMP | HOUR | MINUTE | SECOND |
|-------------------------+------+--------+--------|
| 2013-05-08 23:39:20.123 | 23 | 39 | 20 |
+-------------------------+------+--------+--------+

2.4 LAST_DAY
Returns the last day of the specified date part for a date or timestamp. Commonly
used to return the last day of the month for a date or timestamp.

Syntax
LAST_DAY( <date_or_time_expr> [ , <date_part> ] )

Usage Notes
 (Required) must be a date or timestamp expression.
date_or_time_expr
 date_part (Optional) is the date part for which the last day is returned. Possible
values are year, quarter, month, or week (or any of their supported variations). For
details, see Supported Date and Time Parts.

The default is month.


Department of Computer Engineering
 When date_part is week (or any of its variations), the output is controlled by
the WEEK_START session parameter. For more details, including examples,
see Calendar Weeks and Weekdays.
 The return value is always a date, even if date_or_time_expr is a timestamp.

Examples
Return the last day of the month for the specified date (from a timestamp):

SELECT TO_DATE('2015-05-08T23:39:20.123-07:00') AS "DATE",


LAST_DAY("DATE") AS "LAST DAY OF MONTH";
+------------+-------------------+
| DATE | LAST DAY OF MONTH |
|------------+-------------------|
| 2015-05-08 | 2015-05-31 |
+------------+-------------------+

Return the last day of the year for the specified date (from a timestamp):

SELECT TO_DATE('2015-05-08T23:39:20.123-07:00') AS "DATE",


LAST_DAY("DATE", 'year') AS "LAST DAY OF YEAR";
+------------+------------------+
| DATE | LAST DAY OF YEAR |
|------------+------------------|
| 2015-05-08 | 2015-12-31 |
+------------+------------------+

2.5 MONTHNAME
Extracts the three-letter month name from the specified date or timestamp.

Syntax
MONTHNAME( <date_or_timestamp_expr> )

Examples
SELECT MONTHNAME(TO_DATE('2015-05-01')) AS MONTH;

-------+
MONTH |
Department of Computer Engineering
-------+
May |
-------+
SELECT MONTHNAME(TO_TIMESTAMP('2015-04-03 10:00')) AS MONTH;

-------+
MONTH |
-------+
Apr |
-------+

2.6 NEXT_DAY
Returns the date of the first specified DOW (day of week) that occurs after the
input date.

Syntax
NEXT_DAY( <date_or_time_expr> , <dow_string> )

Arguments
date_or_time_expr

Specifies the input date; can be a date or timestamp.

dow_string

Specifies the day of week used to calculate the date for the previous day. The
value can be a string literal or an expression that returns a string. The string
must start with the first two characters (case-insensitive) of the day name:

 su (Sunday)
 mo (Monday)
 tu (Tuesday)
 we (Wednesday)
 th (Thursday)
 fr (Friday)
 sa (Saturday)
Department of Computer Engineering
Any leading spaces and trailing characters, including spaces, in the string are
ignored.

Usage Notes
 The return value is always a date regardless of whether date_or_time_expr is a
date or timestamp.

Examples
Return the date of the next Friday that occurs after the current date:

SELECT CURRENT_DATE() AS "Today's Date",


NEXT_DAY("Today's Date", 'Friday ') AS "Next Friday";

+--------------+-------------+
| Today's Date | Next Friday |
|--------------+-------------|
| 2018-06-12 | 2018-06-15 |
+--------------+-------------+

2.7 PREVIOUS_DAY
Returns the date of the first specified DOW (day of week) that occurs before the
input date.

Syntax
PREVIOUS_DAY( <date_or_time_expr> , <dow> )

Arguments
date_or_time_expr

Specifies the input date; can be a date or timestamp.

dow_string
Department of Computer Engineering
Specifies the day of week used to calculate the date for the previous day. The
value can be a string literal or an expression that returns a string. The string
must start with the first two characters (case-insensitive) of the day name:

 su (Sunday)
 mo (Monday)
 tu (Tuesday)
 we (Wednesday)
 th (Thursday)
 fr (Friday)
 sa (Saturday)

Any leading spaces and trailing characters, including spaces, in the string
are ignored.

Usage Notes
 The return value is always a date regardless of whether date_or_time_expr is a
date or timestamp.

Examples
Return the date of the previous Friday that occurred before the current date:

SELECT CURRENT_DATE() AS "Today's Date",


PREVIOUS_DAY("Today's Date", 'Friday ') AS "Previous Friday";

+--------------+-----------------+
| Today's Date | Previous Friday |
|--------------+-----------------|
| 2018-06-12 | 2018-06-08 |
+--------------+-----------------+

2.8 MONTHS_BETWEEN
Returns the number of months between two DATE or TIMESTAMP values.

For example, MONTHS_BETWEEN('2020-02-01'::DATE, '2020-01-01'::DATE) returns


1.0.
Department of Computer Engineering
Syntax
MONTHS_BETWEEN ( <date_expr1> , <date_expr2> )

Examples
This example shows differences in whole months. The first pair of dates have the
same day of the month (the 15th). The second pair of dates are both the last days in
their respective months (February 28th and March 31st).

SELECT
MONTHS_BETWEEN('2019-03-15'::DATE,
'2019-02-15'::DATE) AS MonthsBetween1,
MONTHS_BETWEEN('2019-03-31'::DATE,
'2019-02-28'::DATE) AS MonthsBetween2;
+----------------+----------------+
| MONTHSBETWEEN1 | MONTHSBETWEEN2 |
|----------------+----------------|
| 1.000000 | 1.000000 |
+----------------+----------------+

3. Numeric Functions
Numeric functions operate on numeric values and perform operations such as
rounding and exponentiation.

Rounding and Truncation ABS


CEIL
FLOOR
MOD
ROUND
SIGN
TRUNCATE , TRUNC

3.1 ABS
Returns the absolute value of a numeric expression.
Department of Computer Engineering
Syntax
ABS( <num_expr> )

Examples
SELECT column1, abs(column1)
FROM (values (0), (1), (-2), (3.5), (-4.5), (null));
+---------+--------------+
| COLUMN1 | ABS(COLUMN1) |
|---------+--------------|
| 0.0 | 0.0 |
| 1.0 | 1.0 |
| -2.0 | 2.0 |
| 3.5 | 3.5 |
| -4.5 | 4.5 |
| NULL | NULL |
+---------+--------------+

3.2 CEIL
Returns values from input_expr rounded to the nearest equal or larger integer, or to the
nearest equal or larger value with the specified number of places after the decimal
point.

Syntax
CEIL( <input_expr> [, <scale_expr> ] )

Arguments
input_expr

The value or expression to operate on. The data type should be one of the
numeric data types, such as FLOAT or NUMBER.

scale_expr

The number of digits the output should include after the decimal point. The
expression should evaluate to an integer from -38 to +38.
Department of Computer Engineering
The default scale_expr is zero, meaning that the function removes all digits after
the decimal point.

For information about negative scales, see the Usage Notes below.

Returns
The data type of the returned value is NUMBER(precision, scale).

If the input scale was greater than or equal to zero, then the output scale generally
matches the input scale.

If the input scale was negative, then the output scale is 0.

For example:

 The data type returned by CEIL(3.14, 1) is NUMBER(4, 1).


 The data type returned by CEIL(3.14, 0) is NUMBER(4, 0).
 The data type returned by CEIL(33.33, -1) is NUMBER(5, 0).

If the scale is zero, then the value is effectively an integer.

3.3 FLOOR
Returns values from input_expr rounded to the nearest equal or smaller integer, or to
the nearest equal or smaller value with the specified number of places after the
decimal point.

Syntax
FLOOR( <input_expr> [, <scale_expr> ] )

Arguments
input_expr

The value or expression to operate on. The data type should be one of the
numeric data types, such as FLOAT or NUMBER.

scale_expr
Department of Computer Engineering
The number of digits the output should include after the decimal point. The
expression should evaluate to an integer from -38 to +38.

The default scale_expr is zero, meaning that the function removes all digits after
the decimal point.

For information about negative scales, see the Usage Notes below.

Returns
The data type of the returned value is NUMBER(precision, scale).

If the input scale was greater than or equal to zero, then the output scale generally
matches the input scale.

If the input scale was negative, then the output scale is 0.

For example:

 The data type returned by FLOOR(3.14, 1) is NUMBER(4, 1).


 The data type returned by FLOOR(3.14, 0) is NUMBER(4, 0).
 The data type returned by FLOOR(33.33, -1) is NUMBER(5, 0).

If the scale is zero, then the value is effectively an integer.

3.4 MOD
Returns the remainder of input expr1 divided by input expr2.

Equivalent to the modulo arithmetic operator (e.g. expr1 % expr2 ).

Syntax
MOD( <expr1> , <expr2> )

Arguments
expr1

A numeric expression.

expr2
Department of Computer Engineering
A numeric expression.

Returns
Returns either an integer or a fixed-point decimal number.

Usage Notes
 Both expr1 and expr2 must be numeric expressions. They are not required to be
integers.

Examples
The following example shows usage of the MOD() function on both integer and non-
integer values:

SELECT MOD(3, 2) AS mod1, MOD(4.5, 1.2) AS mod2;

Output:

+------+------+
| MOD1 | MOD2 |
+------+------+
| 1 | 0.9 |
+------+------+

3.5 ROUND
Returns rounded values for input_expr.

Syntax
ROUND( <input_expr> [ , <scale_expr> [ , <rounding_mode> ] ] )
ROUND( EXPR => <input_expr> ,
SCALE => <scale_expr>
[ , ROUNDING_MODE => <rounding_mode> ] )
Department of Computer Engineering
Arguments
Required:

input_expr OR EXPR => input_expr

The value or expression to operate on. The data type should be one of the
numeric data types, such as FLOAT or NUMBER.

If you specify the EXPR => named argument, you must also specify
the SCALE => named argument.

Optional:

Examples
This first example shows a simple use of ROUND, with the default number of decimal
places (0):

SELECT ROUND(135.135), ROUND(-975.975);


+----------------+-----------------+
| ROUND(135.135) | ROUND(-975.975) |
|----------------+-----------------|
| 135 | -976 |
+----------------+-----------------+

3.6 SIGN
Returns the sign of its argument:

 -1 if the argument is negative.


 1 if it is positive.
 0 if it is 0.

Syntax
SIGN( <expr> )
Department of Computer Engineering
Examples
SELECT SIGN(5), SIGN(-1.35e-10), SIGN(0);

---------+-----------------+---------+
SIGN(5) | SIGN(-1.35E-10) | SIGN(0) |
---------+-----------------+---------+
1 | -1 |0 |
---------+-----------------+---------+

3.7 TRUNCATE , TRUNC


Rounds the input expression down to the nearest (or equal) integer closer to zero, or
to the nearest equal or smaller value with the specified number of places after the
decimal point.

These functions are synonymous.

Note
TRUNC is overloaded; it can also be used as a date/time function to truncate dates,
times, and timestamps to a specified part.

Syntax
TRUNCATE( <input_expr> [ , <scale_expr> ] )

TRUNC( <input_expr> [ , <scale_expr> ] )

Arguments
input_expr

The value or expression to operate on. The data type should be one of the
numeric data types, such as FLOAT or NUMBER.

scale_expr

The number of digits the output should include after the decimal point. The
expression should evaluate to an integer from -38 to +38.
Department of Computer Engineering
The default scale_expr is zero, meaning that the function removes all digits after
the decimal point.

For information about negative scales, see the Usage Notes below.

Returns
The data type of the returned value is NUMBER(precision, scale).

If the input scale was greater than or equal to zero, then the output scale generally
matches the input scale.

If the input scale was negative, then the output scale is 0.

For example:

 The data type returned by TRUNCATE(3.14, 1) is NUMBER(4, 1).


 The data type returned by TRUNCATE(3.14, 0) is NUMBER(4, 0).
 The data type returned by TRUNCATE(33.33, -1) is NUMBER(5, 0).

If the scale is zero, then the value is effectively an integer.

Examples
The following examples demonstrate the TRUNC function.

SELECT DISTINCT n, TRUNCATE(n)


FROM test_1
ORDER BY n;
+----------+-------------+
| N | TRUNCATE(N) |
|----------+-------------|
| -975.975 | -975 |
| 135.135 | 135 |
+----------+-------------+

4. String Functions (Regular Expressions)


These string functions perform operations that match a regular expression (often referred to as
a “regex”).

List of Regex Functions


Department of Computer Engineering
Function Notes
Included in the syllabus
REGEXP Alias for RLIKE.
REGEXP_LIKE Alias for RLIKE.
REGEXP_SUBSTR
Not included in the syllabus
REGEXP_COUNT
REGEXP_EXTRACT_ALL Alias for REGEXP_SUBSTR_ALL.
REGEXP_INSTR
REGEXP_REPLACE
REGEXP_SUBSTR_ALL
RLIKE

4.1 REGEXP
Returns true if the subject matches the specified pattern. Both inputs must be text
expressions.

REGEXP is similar to the LIKE function, but with POSIX extended regular
expressions instead of SQL LIKE pattern syntax. It supports more complex matching
conditions than LIKE.

Syntax
<subject> REGEXP <pattern>

Arguments
Required:

subject

Subject to match.

pattern

Pattern to match.
Department of Computer Engineering
Returns
The data type of the returned value is BOOLEAN.

Examples
The example below shows how to use REGEXP with a simple wildcard expression:

Create the table and load data:

CREATE OR REPLACE TABLE strings (v VARCHAR(50));


INSERT INTO strings (v) VALUES
('San Francisco'),
('San Jose'),
('Santa Clara'),
('Sacramento');

Use wildcards to search for a pattern:

SELECT v
FROM strings
WHERE v REGEXP 'San* [fF].*'
ORDER BY v;
+---------------+
|V |
|---------------|
| San Francisco |
+---------------+

The backslash character \ is the escape character in regular expressions, and specifies
special characters or groups of characters. For example, \s is the regular expression
for whitespace.

The Snowflake string parser, which parses literal strings, also treats backslash as an
escape character. For example, a backslash is used as part of the sequence of
characters that specifies a tab character. Thus to create a string that contains a single
backslash, you must specify two backslashes. For example, compare the string in the
input statement below with the corresponding string in the output:

INSERT INTO strings (v) VALUES


('Contains embedded single \\backslash')
;
SELECT *
Department of Computer Engineering
FROM strings
ORDER BY v;
+-------------------------------------+
|V |
|-------------------------------------|
| Contains embedded single \backslash |
| Sacramento |
| San Francisco |
| San Jose |
| Santa Clara |
+-------------------------------------+

This example shows how to search for strings that start with “San”, where “San” is
a complete word (e.g. not part of “Santa”). \b is the escape sequence for a word
boundary.

select v, v regexp 'San\\b.*' AS MATCHES


from strings
order by v;
+-------------------------------------+---------+
|V | MATCHES |
|-------------------------------------+---------|
| Contains embedded single \backslash | False |
| Sacramento | False |
| San Francisco | True |
| San Jose | True |
| Santa Clara | False |
+-------------------------------------+---------+

This example shows how to search for a blank followed by a backslash. Note that
the single backslash to search for is represented by four backslashes below; for
REGEXP to look for a literal backslash, that backslash must be escaped, so you need
two backslashes. The string parser requires that each of those backslashes be
escaped, so the expression contains four backslashes to represent the one backslash
that the expression is searching for:

select v, v regexp '.*\\s\\\\.*' AS MATCHES


from strings
order by v;
+-------------------------------------+---------+
|V | MATCHES |
|-------------------------------------+---------|
| Contains embedded single \backslash | True |
| Sacramento | False |
| San Francisco | False |
Department of Computer Engineering
| San Jose | False |
| Santa Clara | False |
+-------------------------------------+---------+

The following example is the same as the preceding example, except that it uses $$ as
a string delimiter to tell the string parser that the string is a literal and that
backslashes should not be interpreted as escape sequences. (The backslashes are still
interpreted as escape sequences by REGEXP.)

select v, v regexp $$.*\s\\.*$$ AS MATCHES


from strings
order by v;
+-------------------------------------+---------+
|V | MATCHES |
|-------------------------------------+---------|
| Contains embedded single \backslash | True |
| Sacramento | False |
| San Francisco | False |
| San Jose | False |
| Santa Clara | False |
+-------------------------------------+---------+

4.2 REGEXP_LIKE
Returns true if the subject matches the specified pattern. Both inputs must be text
expressions.

REGEXP_LIKE is similar to the LIKE function, but with POSIX extended regular
expressions instead of SQL LIKE pattern syntax. It supports more complex matching
conditions than LIKE.

Syntax
REGEXP_LIKE( <subject> , <pattern> [ , <parameters> ] )

Arguments
Required:

subject
Department of Computer Engineering
Subject to match.

pattern

Pattern to match.

Optional:

parameters

String of one or more characters that specifies the parameters used for
searching for matches. Supported values:

c ,i,m,e,s

Returns
The data type of the returned value is BOOLEAN.

Usage Notes
 The function implicitly anchors a pattern at both ends (i.e. '' automatically
becomes '^$', and 'ABC' automatically becomes '^ABC$'). To match any string
starting with ABC, the pattern would be 'ABC.*'.
 The backslash character ( \) is the escape character. For more information,
see Specifying Regular Expressions in Single-Quoted String Constants.
 For more usage notes, see the General Usage Notes for regular expression
functions.

Collation Details
Arguments with collation specifications are currently not supported.

Examples
Create a table with names of cities:

CREATE OR REPLACE TABLE cities(city varchar(20));


INSERT INTO cities VALUES
('Sacramento'),
('San Francisco'),
Department of Computer Engineering
('San Jose'),
(null);

Execute a case-sensitive query with a wildcard:

SELECT * FROM cities WHERE REGEXP_LIKE(city, 'san.*');


+------+
| CITY |
|------|
+------+

Execute a case-insensitive query with a wildcard:

SELECT * FROM cities WHERE REGEXP_LIKE(city, 'san.*', 'i');


+---------------+
| CITY |
|---------------|
| San Francisco |
| San Jose |
+---------------+

5. Aggregate Functions
Aggregate functions operate on values across rows to perform mathematical
calculations such as sum, average, counting, minimum/maximum values, standard
deviation, and estimation, as well as some non-mathematical operations.

An aggregate function takes multiple rows (actually, zero, one, or more rows) as
input and produces a single output. In contrast, scalar functions take one row as input
and produce one row (one value) as output.

An aggregate function always returns exactly one row, even when the input
contains zero rows. Typically, if the input contained zero rows, the output is NULL.
However, an aggregate function could return 0, an empty string, or some other value
when passed zero rows.

General Aggregation
AVG
COUNT
COUNT_IF
Department of Computer Engineering
MAX
MAX_BY
MEDIAN
MIN
MIN_BY
SUM

5.1 AVG

Returns the average of non-NULL records. If all records inside a group are NULL,
the function returns NULL.

Syntax
Aggregate function

AVG( [ DISTINCT ] <expr1> )

Window function

AVG( [ DISTINCT ] <expr1> ) OVER (


[ PARTITION BY <expr2> ]
[ ORDER BY <expr3> [ ASC | DESC ] [ <window_frame>
]]
)

Arguments
expr1

This is an expression that evaluates to a numeric data type (INTEGER,


FLOAT, DECIMAL, etc.).

expr2

This is the optional expression to partition by.

expr3

This is the optional expression to order by within each partition.


Department of Computer Engineering
Examples
Setup:

CREATE OR REPLACE TABLE avg_example(int_col int, d decimal(10,5),


s1 varchar(10), s2 varchar(10));
INSERT INTO avg_example VALUES
(1, 1.1, '1.1','one'),
(1, 10, '10','ten'),
(2, 2.4, '2.4','two'),
(2, NULL, NULL, 'NULL'),
(3, NULL, NULL, 'NULL'),
(NULL, 9.9, '9.9','nine');

Show the data:

SELECT *
FROM avg_example
ORDER BY int_col, d;
+---------+----------+------+------+
| INT_COL | D | S1 | S2 |
|---------+----------+------+------|
| 1 | 1.10000 | 1.1 | one |
| 1 | 10.00000 | 10 | ten |
| 2 | 2.40000 | 2.4 | two |
| 2 | NULL | NULL | NULL |
| 3 | NULL | NULL | NULL |
| NULL | 9.90000 | 9.9 | nine |
+---------+----------+------+------+

Calculate the average of the columns that are numeric or that can be converted to
numbers:

SELECT AVG(int_col), AVG(d)


FROM avg_example;
+--------------+---------------+
| AVG(INT_COL) | AVG(D) |
|--------------+---------------|
| 1.800000 | 5.85000000000 |
+--------------+---------------+
Department of Computer Engineering
Combine AVG with GROUP BY to calculate the averages of different groups:

SELECT int_col, AVG(d), AVG(s1)


FROM avg_example
GROUP BY int_col
ORDER BY int_col;
+---------+---------------+---------+
| INT_COL | AVG(D) | AVG(S1) |
|---------+---------------+---------|
| 1 | 5.55000000000 | 5.55 |
| 2 | 2.40000000000 | 2.4 |
| 3| NULL | NULL |
| NULL | 9.90000000000 | 9.9 |
+---------+---------------+---------+

Use as a simple window function:

SELECT
int_col,
AVG(int_col) OVER(PARTITION BY int_col)
FROM avg_example
ORDER BY int_col;
+---------+-----------------------------------------+
| INT_COL | AVG(INT_COL) OVER(PARTITION BY INT_COL) |
|---------+-----------------------------------------|
| 1| 1.000 |
| 1| 1.000 |
| 2| 2.000 |
| 2| 2.000 |
| 3| 3.000 |
| NULL | NULL |
+---------+-----------------------------------------+

5.2 COUNT

Returns either the number of non-NULL records for the specified columns, or the
total number of records.

Syntax
Department of Computer Engineering
Aggregate function

COUNT( [ DISTINCT ] <expr1> [ , <expr2> ... ] )

COUNT( * )

Examples
This is an example of using COUNT with NULL values. The query also includes
some COUNT(DISTINCT) operations:

CREATE TABLE basic_example (i_col INTEGER, j_col INTEGER);


INSERT INTO basic_example VALUES
(11,101), (11,102), (11,NULL), (12,101), (NULL,101), (NULL,102);
SELECT *
FROM basic_example
ORDER BY i_col;
+-------+-------+
| I_COL | J_COL |
|-------+-------|
| 11 | 101 |
| 11 | 102 |
| 11 | NULL |
| 12 | 101 |
| NULL | 101 |
| NULL | 102 |
+-------+-------+
SELECT COUNT(*), COUNT(i_col), COUNT(DISTINCT i_col), COUNT(j_col),
COUNT(DISTINCT j_col) FROM basic_example;
+----------+--------------+-----------------------+--------------+-----------------------+
| COUNT(*) | COUNT(I_COL) | COUNT(DISTINCT I_COL) | COUNT(J_COL) |
COUNT(DISTINCT J_COL) |
|----------+--------------+-----------------------+--------------+-----------------------|
| 6| 4| 2| 5| 2|
+----------+--------------+-----------------------+--------------+-----------------------+
SELECT i_col, COUNT(*), COUNT(j_col)
FROM basic_example
GROUP BY i_col
ORDER BY i_col;
+-------+----------+--------------+
| I_COL | COUNT(*) | COUNT(J_COL) |
|-------+----------+--------------|
| 11 | 3| 2|
| 12 | 1| 1|
Department of Computer Engineering
| NULL | 2| 2|
+-------+----------+--------------+

5.3 COUNT_IF

Returns the number of records that satisfy a condition or NULL if no records satisfy
the condition.

Syntax
Aggregate function

COUNT_IF( <condition> )

Window function

COUNT_IF( <condition> )
OVER ( [ PARTITION BY <expr1> ] [ ORDER BY <expr2> [ ASC | DESC
] [ <window_frame> ] ] )

Arguments
condition

The condition is an expression that should evaluate to a BOOLEAN value


(True, False, or NULL)

expr1

The column to partition on, if you want the result to be split into multiple
windows.

expr2

The column to order each window on. Note that this is separate from any
ORDER BY clause to order the final result set.

Returns
Department of Computer Engineering
If the function does not return NULL, the data type of the returned value is
NUMBER.

Usage Notes
 When this function is called as a window function:
 If an ORDER BY sub-clause is used inside the OVER() clause, then a
window frame must be used. If no window frame is specified, then the
default is a cumulative window frame:

Examples
The examples in this section demonstrate how to use the COUNT_IF function.

The following statements set up a table for use in the examples:

CREATE TABLE basic_example (i_col INTEGER, j_col INTEGER);


INSERT INTO basic_example VALUES
(11,101), (11,102), (11,NULL), (12,101), (NULL,101), (NULL,102);
SELECT *
FROM basic_example
ORDER BY i_col;
+-------+-------+
| I_COL | J_COL |
|-------+-------|
| 11 | 101 |
| 11 | 102 |
| 11 | NULL |
| 12 | 101 |
| NULL | 101 |
| NULL | 102 |
+-------+-------+

The following example passes in TRUE for the condition, which returns the count of
all rows in the table:

SELECT COUNT_IF(TRUE) FROM basic_example;


+----------------+
| COUNT_IF(TRUE) |
|----------------|
| 6|
Department of Computer Engineering
+----------------+

The following example returns the number of rows where the value in J_COL is
greater than the value in I_COL:

SELECT COUNT_IF(j_col > i_col) FROM basic_example;


+-------------------------+
| COUNT_IF(J_COL > I_COL) |
|-------------------------|
| 3|
+-------------------------+

5.4 MAX

Returns the maximum value for the records within expr. NULL values are ignored
unless all the records are NULL, in which case a NULL value is returned.

Syntax¶
Aggregate function

MAX( <expr> )

Examples
The following examples demonstrate how to use the MAX function.

Create a table and data:

CREATE OR REPLACE TABLE sample_table(k CHAR(4), d CHAR(4));

INSERT INTO sample_table VALUES


('1', '1'), ('1', '5'), ('1', '3'),
('2', '2'), ('2', NULL),
('3', NULL),
(NULL, '7'), (NULL, '1');
Department of Computer Engineering
Display the data:

SELECT k, d
FROM sample_table
ORDER BY k, d;
+------+------+
|K |D |
|------+------|
|1 |1 |
|1 |3 |
|1 |5 |
|2 |2 |
| 2 | NULL |
| 3 | NULL |
| NULL | 1 |
| NULL | 7 |
+------+------+

Use the MAX function to retrieve the largest value in the column named d:

SELECT MAX(d) FROM sample_table;


+--------+
| MAX(D) |
|--------|
|7 |
+--------+

Combine the GROUP BY clause with the MAX function to retrieve the largest values in each
group (where each group is based on the value of column k):

SELECT k, MAX(d)
FROM sample_table
GROUP BY k
ORDER BY k;
+------+--------+
| K | MAX(D) |
|------+--------|
|1 |5 |
|2 |2 |
| 3 | NULL |
| NULL | 7 |
+------+--------+

5.5 MAX_BY
Department of Computer Engineering
Finds the row(s) containing the maximum value for a column and returns the value
of another column in that row.

For example, if a table contains the


columns employee_id and salary, MAX_BY(employee_id, salary) returns the value of
the employee_id column for the row that has the highest value in the salary column.

If multiple rows contain the specified maximum value, the function is non-
deterministic.

To return values for multiple rows, specify the


optional maximum_number_of_values_to_return argument. With this additional
argument:

 The function returns an ARRAY containing the values of a column for the
rows with the highest values of a specified column.
 The values in the ARRAY are sorted by their corresponding values in the
column containing the maximum values.
 If multiple rows contain these highest values, the function is non-
deterministic.

For example, MAX_BY(employee_id, salary, 5) returns an ARRAY of values of


the employee_id column for the five rows containing the highest values in
the salary column. The IDs in the ARRAY are sorted by the corresponding values in
the salary column.

Syntax
MAX_BY( <col_to_return>, <col_containing_maximum> [ ,
<maximum_number_of_values_to_return> ] )

Arguments
Required:

col_to_return

Column containing the value to return.

col_containing_maximum

Column containing the maximum value.


Department of Computer Engineering
Optional:

maximum_number_of_values_to_return

Constant integer specifying the maximum number of values to return. You


must specify a positive number. The maximum number that you can specify
is 1000.

Returns
 If maximum_number_of_values_to_return is not specified, the function returns a
value of the same type as col_to_return.
 If maximum_number_of_values_to_return is specified, the function returns an
ARRAY containing values of the same type as col_to_return. The values in the
ARRAY are sorted by their corresponding col_containing_maximum values.

For example, MAX_BY(employee_id, salary, 5) returns the IDs of the


employees with the highest five salaries, sorted by salary (in descending
order).

Usage Notes
 The function ignores NULL values in col_containing_maximum.
 If all values in col_containing_maximum are NULL, the function returns
NULL (regardless of whether the
optional maximum_number_of_values_to_return argument is specified).

Examples
The following examples demonstrate how to use the MAX_BY function.

To run these examples, execute the following statements to set up the table and data
for the examples:

CREATE OR REPLACE TABLE employees(employee_id NUMBER,


department_id NUMBER, salary NUMBER);

INSERT INTO employees VALUES


(1001, 10, 10000),
(1020, 10, 9000),
(1030, 10, 8000),
Department of Computer Engineering
(900, 20, 15000),
(2000, 20, NULL),
(2010, 20, 15000),
(2020, 20, 8000);

Execute the following statement to view the contents of this table:

SELECT * FROM employees;


+-------------+---------------+--------+
| EMPLOYEE_ID | DEPARTMENT_ID | SALARY |
|-------------+---------------+--------|
| 1001 | 10 | 10000 |
| 1020 | 10 | 9000 |
| 1030 | 10 | 8000 |
| 900 | 20 | 15000 |
| 2000 | 20 | NULL |
| 2010 | 20 | 15000 |
| 2020 | 20 | 8000 |
+-------------+---------------+--------+

The following example returns the ID of the employee with the highest salary:

SELECT MAX_BY(employee_id, salary) FROM employees;


+-----------------------------+
| MAX_BY(EMPLOYEE_ID, SALARY) |
|-----------------------------|
| 900 |
+-----------------------------+

Note the following:

 Because more than one row contains the maximum value for
the salary column, the function is non-deterministic and might return the
employee ID for a different row in subsequent executions.
 The function ignores the NULL value in the salary column when determining
the rows with the maximum values.

The following example returns an ARRAY containing the IDs of the employees with
the three highest salaries:

SELECT MAX_BY(employee_id, salary, 3) from employees;


+--------------------------------+
Department of Computer Engineering
| MAX_BY(EMPLOYEE_ID, SALARY, 3) |
|--------------------------------|
|[ |
| 900, |
| 2010, |
| 1001 |
|] |
+--------------------------------+

As shown in the example, the values in the ARRAY are sorted by their
corresponding values in the salary column. So, MAX_BY returns the IDs of
employees sorted by their salary in descending order.

If more than one of these rows contain the same value in the salary column, the order
of the returned values for that salary is non-deterministic.

5.6 MEDIAN

Determines the median of a set of values.

Syntax
Aggregate function

MEDIAN( <expr> )

Window function

MEDIAN( <expr> ) OVER ( [ PARTITION BY <expr2> ] )

Argument
expr
Department of Computer Engineering
The expression must evaluate to a numeric data type
(INTEGER, FLOAT , DECIMAL, or equivalent).

Returns
Returns a FLOAT or DECIMAL (fixed-point) number, depending upon the input.

Usage Notes
 If the number of non-NULL values is an odd number greater than or equal to
1, this returns the median (“center”) value of the non-NULL values.
 If the number of non-NULL values is an even number, this returns a value
equal to the average of the two center values. For example, if the values are
1, 3, 5, and 20, then this returns 4 (the average of 3 and 5).
 If all values are NULL, this returns NULL.
 If the number of non-NULL values is 0, this returns NULL.
 DISTINCT is not supported for this function.
 When used as a window function:
 This function does not support:
 ORDER BY sub-clause in the OVER() clause.
 Window frames.

Examples
This shows how to use the function.

Create an empty table.

CREATE OR REPLACE TABLE aggr(k int, v decimal(10,2));

Get the MEDIAN value for column v. The function returns NULL because there are
no rows.

SELECT MEDIAN (v) FROM aggr;


+------------+
| MEDIAN (V) |
|------------|
| NULL |
+------------+

Insert some rows:


Department of Computer Engineering
INSERT INTO aggr VALUES(1, 10), (1,20), (1, 21);
INSERT INTO aggr VALUES(2, 10), (2, 20), (2, 25), (2, 30);
INSERT INTO aggr VALUES(3, NULL);

Get the MEDIAN value for each group. Note that because the number of values in
group k = 2 is an even number, the returned value for that group is the mid-point
between the two middle numbers.

SELECT k, MEDIAN(v) FROM aggr GROUP BY k ORDER BY k;


+---+-----------+
| K | MEDIAN(V) |
|---+-----------|
| 1 | 20.00000 |
| 2 | 22.50000 |
| 3 | NULL |
+---+-----------+

5.7 MIN

Returns the minimum value for the records within expr. NULL values are ignored
unless all the records are NULL, in which case a NULL value is returned.

Syntax
Aggregate function

MIN( <expr> )

Window function

MIN( <expr> ) [ OVER ( [ PARTITION BY <expr1> ] [ ORDER BY <expr2>


[ <window_frame> ] ] ) ]

Returns
Department of Computer Engineering
The data type of the returned value is the same as the data type of the input values.

Usage Notes
 For compatibility with other systems, you can specify the DISTINCT
keyword as an argument for the function, but it does not have any effect.
 If the function is called as a window function, the window can include an
optional window_frame. The window_frame (either cumulative or sliding) specifies
the subset of rows within the window for which the summed values are
returned. If no window_frame is specified, the default is the following cumulative
window frame (in accordance with the ANSI standard for window functions):

Collation Details
 The comparisons follow the collation based on the input arguments’ collations
and precedences.
 The collation of the result is the same as the collation of the input.

Examples
The following examples demonstrate how to use the MIN function.

Create a table and data:

CREATE OR REPLACE TABLE sample_table(k CHAR(4), d CHAR(4));

INSERT INTO sample_table VALUES


('1', '1'), ('1', '5'), ('1', '3'),
('2', '2'), ('2', NULL),
('3', NULL),
(NULL, '7'), (NULL, '1');

Display the data:

SELECT k, d
FROM sample_table
ORDER BY k, d;
+------+------+
|K |D |
|------+------|
|1 |1 |
Department of Computer Engineering
|1 |3 |
|1 |5 |
|2 |2 |
| 2 | NULL |
| 3 | NULL |
| NULL | 1 |
| NULL | 7 |
+------+------+

Use the MIN function to retrieve the smallest value in the column named d:

SELECT MIN(d) FROM sample_table;


+--------+
| MIN(D) |
|--------|
|1 |
+--------+

Combine the GROUP BY clause with the MIN function to retrieve the smallest
values in each group (where each group is based on the value of column k):

SELECT k, MIN(d)
FROM sample_table
GROUP BY k
ORDER BY k;
+------+--------+
| K | MIN(D) |
|------+--------|
|1 |1 |
|2 |2 |
| 3 | NULL |
| NULL | 1 |
+------+--------+

Use a PARTITION BY clause to break the data into groups based on the value of k.
This is similar to, but not identical to, using GROUP BY. In particular, GROUP BY
produces one output row per group, while PARTITION BY produces one output
row per input row.

SELECT k, d, MIN(d) OVER (PARTITION BY k)


FROM sample_table
Department of Computer Engineering
ORDER BY k, d;
+------+------+------------------------------+
| K | D | MIN(D) OVER (PARTITION BY K) |
|------+------+------------------------------|
|1 |1 |1 |
|1 |3 |1 |
|1 |5 |1 |
|2 |2 |2 |
| 2 | NULL | 2 |
| 3 | NULL | NULL |
| NULL | 1 | 1 |
| NULL | 7 | 1 |
+------+------+------------------------------+

Use a windowing ORDER BY clause to create a sliding window two rows wide, and
output the lowest value within that window. (Remember that ORDER BY in the
windowing clause is separate from ORDER BY at the statement level.) This example
uses a single partition, so there is no PARTITION BY clause in the OVER() clause.

SELECT k, d, MIN(d) OVER (ORDER BY k, d ROWS BETWEEN 1


PRECEDING AND CURRENT ROW)
FROM sample_table
ORDER BY k, d;
+------+------+----------------------------------------------------------------------+
| K | D | MIN(D) OVER (ORDER BY K, D ROWS BETWEEN 1
PRECEDING AND CURRENT ROW) |
|------+------+----------------------------------------------------------------------|
|1 |1 |1 |
|1 |3 |1 |
|1 |5 |3 |
|2 |2 |2 |
| 2 | NULL | 2 |
| 3 | NULL | NULL |
| NULL | 1 | 1 |
| NULL | 7 | 1 |
+------+------+-------------------------------------------------

5.8 MIN_BY
Department of Computer Engineering
Finds the row(s) containing the minimum value for a column and returns the value
of another column in that row.

For example, if a table contains the


columns employee_id and salary, MIN_BY(employee_id, salary) returns the value of
the employee_id column for the row that has the lowest value in the salary column.

If multiple rows contain the specified minimum value, the function is non-
deterministic.

To return values for multiple rows, specify the


optional maximum_number_of_values_to_return argument. With this additional
argument:

 The function returns an ARRAY containing the values of a column for the
rows with the lowest values of a specified column.
 The values in the ARRAY are sorted by their corresponding values in the
column containing the minimum values.
 If multiple rows contain these lowest values, the function is non-deterministic.

For example, MIN_BY(employee_id, salary, 5) returns an ARRAY of values of


the employee_id column for the five rows containing the lowest values in
the salary column. The IDs in the ARRAY are sorted by the corresponding values in
the salary column.

Syntax
MIN_BY( <col_to_return>, <col_containing_mininum> [ ,
<maximum_number_of_values_to_return> ] )

Arguments
Required:

col_to_return

Column containing the value to return.

col_containing_mininum

Column containing the minimum value.


Department of Computer Engineering
Optional:

maximum_number_of_values_to_return

Constant integer specifying the maximum number of values to return. You


must specify a positive number. The maximum number that you can specify
is 1000.

Returns
 If maximum_number_of_values_to_return is not specified, the function returns a
value of the same type as col_to_return.
 If maximum_number_of_values_to_return is specified, the function returns an
ARRAY containing values of the same type as col_to_return. The values in
the ARRAY are sorted by their corresponding col_containing_mininum values.

For example, MIN_BY(employee_id, salary, 5) returns the IDs of the employees


with the lowest five salaries, sorted by salary (in ascending order).

Usage Notes
 The function ignores NULL values in col_containing_mininum.
 If all values in col_containing_mininum are NULL, the function returns NULL
(regardless of whether the
optional maximum_number_of_values_to_return argument is specified).

Examples
The following examples demonstrate how to use the MIN_BY function.

To run these examples, execute the following statements to set up the table and data
for the examples:

CREATE OR REPLACE TABLE employees(employee_id NUMBER,


department_id NUMBER, salary NUMBER);

INSERT INTO employees VALUES


(1001, 10, 10000),
(1020, 10, 9000),
(1030, 10, 8000),
(900, 20, 15000),
Department of Computer Engineering
(2000, 20, NULL),
(2010, 20, 15000),
(2020, 20, 8000);

Execute the following statement to view the contents of this table:

SELECT * FROM employees;


+-------------+---------------+--------+
| EMPLOYEE_ID | DEPARTMENT_ID | SALARY |
|-------------+---------------+--------|
| 1001 | 10 | 10000 |
| 1020 | 10 | 9000 |
| 1030 | 10 | 8000 |
| 900 | 20 | 15000 |
| 2000 | 20 | NULL |
| 2010 | 20 | 15000 |
| 2020 | 20 | 8000 |
+-------------+---------------+--------+

The following example returns the ID of the employee with the lowest salary:

SELECT MIN_BY(employee_id, salary) FROM employees;


+-----------------------------+
| MIN_BY(EMPLOYEE_ID, SALARY) |
|-----------------------------|
| 1030 |
+-----------------------------+

Note the following:

 Because more than one row contains the minimum value for
the salary column, the function is non-deterministic and might return the
employee ID for a different row in subsequent executions.
 The function ignores the NULL value in the salary column when determining
the rows with the minimum values.

The following example returns an ARRAY containing the IDs of the employees with
the three lowest salaries:

SELECT MIN_BY(employee_id, salary, 3) FROM employees;

+--------------------------------+
Department of Computer Engineering
| MIN_BY(EMPLOYEE_ID, SALARY, 3) |
|--------------------------------|
|[ |
| 1030, |
| 2020, |
| 1020 |
|] |
+--------------------------------+

As shown in the example, the values in the ARRAY are sorted by their
corresponding values in the salary column. So, MIN_BY returns the IDs of
employees sorted by their salary in ascending order.

If more than one of these rows contain the same value in the salary column, the order
of the returned values for that salary is non-deterministic.

5.9 SUM

Returns the sum of non-NULL records for expr. You can use the DISTINCT keyword
to compute the sum of unique non-null values. If all records inside a group are
NULL, the function returns NULL.

Syntax
Aggregate function

SUM( [ DISTINCT ] <expr1> )

Window function

SUM( [ DISTINCT ] <expr1> ) OVER (


[ PARTITION BY <expr2> ]
[ ORDER BY <expr3> [ ASC | DESC ] [ <window_frame>
]]
)

For details about window_frame syntax, see Window Frame Syntax and Usage.

Arguments
expr1
Department of Computer Engineering
This is an expression that evaluates to a numeric data type (INTEGER,
FLOAT, DECIMAL, etc.).

expr2

This is the optional expression to partition by.

expr3

This is the optional expression to order by within each partition. (This does
not control the order of the entire query output.)

Usage Notes
 Numeric values are summed into an equivalent or larger data type.
 When passed a VARCHAR expression, this function implicitly casts the input
to floating point values. If the cast cannot be performed, an error is returned.

 When this function is called as a window function (i.e. with an OVER clause):
 If the OVER clause contains an ORDER BY subclause, then:
 A window frame is required. If no window frame is specified
explicitly, then the ORDER BY implies a cumulative window
frame:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

For information about window frames, including syntax and


examples, see Window Frame Syntax and Usage.

For information about implied window frames, see


also Window Frame Usage Notes.

 Using the keyword DISTINCT inside the window function is


prohibited and results in a compile-time error.

Examples
CREATE OR REPLACE TABLE sum_example(k INT, d DECIMAL(10,5),
s1 VARCHAR(10), s2 VARCHAR(10));

INSERT INTO sum_example VALUES


(1, 1.1, '1.1','one'), (1, 10, '10','ten'),
Department of Computer Engineering
(2, 2.2, '2.2','two'), (2, null, null,'null'),
(3, null, null, 'null'),
(null, 9, '9.9','nine');

SELECT * FROM sum_example;

+------+----------+------+------+
| K| D | S1 | S2 |
|------+----------+------+------|
| 1 | 1.10000 | 1.1 | one |
| 1 | 10.00000 | 10.0 | ten |
| 2 | 2.20000 | 2.2 | two |
| 2 | NULL | NULL | null |
| 3 | NULL | NULL | null |
| NULL | 9.00000 | 9.9 | nine |
+------+----------+------+------+

SELECT SUM(d), SUM(s1) FROM sum_example;

+----------+---------+
| SUM(D) | SUM(S1) |
|----------+---------|
| 22.30000 | 23.2 |
+----------+---------+

select k, SUM(d), SUM(s1) FROM sum_example GROUP BY k;

+------+----------+---------+
| K | SUM(D) | SUM(S1) |
|------+----------+---------|
| 1 | 11.10000 | 11.1 |
| 2 | 2.20000 | 2.2 |
| 3 | NULL | NULL |
| NULL | 9.00000 | 9.9 |
+------+----------+---------+

SELECT SUM(s2) FROM sum_example;

100038 (22018): Numeric value 'one' is not recognized


Department of Computer Engineering
The script below shows the use of this function (and some other window functions)
in a windowing context:

CREATE OR REPLACE TABLE example_cumulative (p INT, o INT, i INT);

INSERT INTO example_cumulative VALUES


( 0, 1, 10), (0, 2, 20), (0, 3, 30),
(100, 1, 10),(100, 2, 30),(100, 2, 5),(100, 3, 11),(100, 3, 120),
(200, 1, 10000),(200, 1, 200),(200, 1, 808080),(200, 2, 33333),(200, 3, null),
(200, 3, 4),
(300, 1, null), (300, 1, null);
SELECT
p, o, i,
COUNT(i) OVER (PARTITION BY p ORDER BY o ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW) count_i_Rows_Pre,
SUM(i) OVER (PARTITION BY p ORDER BY o ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW) sum_i_Rows_Pre,
AVG(i) OVER (PARTITION BY p ORDER BY o ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW) avg_i_Rows_Pre,
MIN(i) OVER (PARTITION BY p ORDER BY o ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW) min_i_Rows_Pre,
MAX(i) OVER (PARTITION BY p ORDER BY o ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW) max_i_Rows_Pre
FROM example_cumulative
ORDER BY p,o;
+-----+---+--------+------------------+----------------+----------------+----------------+----
------------+
| P | O | I | COUNT_I_ROWS_PRE | SUM_I_ROWS_PRE |
AVG_I_ROWS_PRE | MIN_I_ROWS_PRE | MAX_I_ROWS_PRE |
|-----+---+--------+------------------+----------------+----------------+----------------+-----
-----------|
| 0 | 1 | 10 | 1| 10 | 10.000 | 10 | 10 |
| 0 | 2 | 20 | 2| 30 | 15.000 | 10 | 20 |
| 0 | 3 | 30 | 3| 60 | 20.000 | 10 | 30 |
| 100 | 1 | 10 | 1| 10 | 10.000 | 10 | 10 |
| 100 | 2 | 30 | 2| 40 | 20.000 | 10 | 30 |
| 100 | 2 | 5 | 3| 45 | 15.000 | 5| 30 |
| 100 | 3 | 11 | 4| 56 | 14.000 | 5| 30 |
| 100 | 3 | 120 | 5| 176 | 35.200 | 5| 120 |
| 200 | 1 | 10000 | 1| 10000 | 10000.000 | 10000 |
10000 |
Department of Computer Engineering
| 200 | 1 | 200 | 2| 10200 | 5100.000 | 200 | 10000 |
| 200 | 1 | 808080 | 3| 818280 | 272760.000 | 200 |
808080 |
| 200 | 2 | 33333 | 4| 851613 | 212903.250 | 200 |
808080 |
| 200 | 3 | NULL | 4| 851613 | 212903.250 | 200 |
808080 |
| 200 | 3 | 4 | 5| 851617 | 170323.400 | 4| 808080 |
| 300 | 1 | NULL | 0| NULL | NULL | NULL |
NULL |
| 300 | 1 | NULL | 0| NULL | NULL | NULL |
NULL |
+-----+---+--------+------------------+----------------+----------------+-

You might also like