Sitemap

SQL: Mastering Functions — Universal and Aggregate Unveiled

Conquer SQL: From Zero to Resume — Part 2 (, , , , , /, , , , , ,

3 min readJan 7, 2025

--

What functions we will learn in the below:

1. Universal functions: , , , , , /, 2. Aggregate functions: , , , ,

Contents

Press enter or click to view image in full size
Photo by Claudio Schwarz on Unsplash

What are Functions

A function is a predefined operation that maps inputs to outputs, enabling tasks such as numerical calculations and text manipulation to be efficiently performed.

The operation of a function consists of five components:

  1. Function name
  2. Input
  3. Parameters
  4. Processing logic
  5. Output

Functions can be broadly classified into two main categories based on their purpose. The key difference between universal functions and aggregate functions lies in the dimension they operate on:

  • Universal functions work horizontally.
  • Aggregate functions work vertically.

Characteristics:

Universal functions: Each observation produces a single output.

Aggregate functions: An entire column produces a single output.

👆

What are Universal functions?

What functions we will learn in the below:
1. CAST
2. COALESCE
3. ROUND
4. LENGTH
5. SUBSTR
6. LOWER/UPPER
7. STRFTIME

Universal functions can also be categorized based on the applied data types:

  1. Data type conversion
  2. Numerical calculations
  3. Text manipulation
  4. Date/time handling

Data type conversion

The function is used to convert the data type of a query result into a specified data type:

CAST(data AS data_type)

The function is used to replace NULL values (or missing values) with a specified constant.

COALESCE(NULL, replacement)

Numerical calculations

The function is used to adjust the number of decimal places in the query result.

ROUND(REAL, n_digits)

Text manipulation

The function counts the number of characters in a text, including spaces and punctuation marks.

LENGTH(TEXT)

The function extracts a specified substring from a text based on its position.

SUBSTR(TEXT, start, stop)

The and functions allow you to change the case of English text.

LOWER(TEXT)
UPPER(TEXT)

Date/time handling

Standard Date, Time, and DateTime Formats

  • ISO8601 is the standard format for date and time.
  • Dates
  • Time
  • Date & time

When date and time data are stored in the ISO8601 format in a database, it ensures high convenience in usage.

Get Mini Cactus’s stories in your inbox

Join Medium for free to get updates from this writer.

The function allows you to adjust the display format for dates, times, or date-time values.

STRFTIME(format, DATE/TIME/DATETIME)
-- format: '%d', '%j', '%m', '%w', '%W', '%Y'

Common date and date-time format parameters include:

  • : Two-digit day (01~31)
  • : Day of the year (001~366)
  • : Two-digit month (01~12)
  • : Day of the week (0~6)
  • : Week number of the year (00~53)
  • : Four-digit year (0000~9999)

👆

What are Aggregate functions?

Common Aggregate Functions:

  • : Calculates the average value of a variable.
  • : Counts the number of non-NULL values in a column.
  • : Counts the total number of observations in a table.
  • : Returns the maximum value of a variable.
  • : Returns the minimum value of a variable.
  • : Calculates the sum of a variable.

👆

--

Mini Cactus
Mini Cactus

Written by Mini Cactus

Master Data Analysis with SQL & Power BI Unlock insights with SQL, Power BI & analytics tips. Simplify data, make smarter decisions—follow for concise content!