SQL Functions and Aggregation

Reference for string, date, numeric, conditional, null-handling, and aggregate SQL functions.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

String Functions

Transform and search text values.

Concatenate strings

Join multiple strings into one value.

sqlANYstringconcat
sql
SELECT first_name || ' ' || last_name AS full_name
FROM users;

String concatenation syntax varies by dialect; CONCAT() is also widely available.

Change case

Convert strings to uppercase or lowercase.

sqlANYstringupperlower
sql
SELECT UPPER(email) AS email_upper, LOWER(email) AS email_lower
FROM users;

Useful for case-insensitive comparisons and normalized reporting.

Trim whitespace

Remove spaces from the left, right, or both sides.

sqlANYstringtrim
sql
SELECT TRIM(name) AS cleaned_name
FROM customers;

LTRIM and RTRIM are also available in many engines.

Extract substring

Return part of a string.

sqlANYstringsubstring
sql
SELECT SUBSTRING(phone FROM 1 FOR 3) AS area_code
FROM contacts;

Substring function syntax differs slightly across SQL dialects.

Replace text

Substitute one substring for another.

sqlANYstringreplace
sql
SELECT REPLACE(phone, '-', '') AS digits_only
FROM contacts;

Handy for lightweight normalization.

Date and Time Functions

Work with dates, timestamps, and intervals.

Get current date and timestamp

Return the current date and current timestamp.

sqlANYdatetimecurrent-datecurrent-timestamp
sql
SELECT CURRENT_DATE AS today, CURRENT_TIMESTAMP AS now_ts;

These values are useful for auditing and reporting queries.

Extract date parts

Return year, month, day, and other pieces from a date or timestamp.

sqlANYdateextract
sql
SELECT
  EXTRACT(YEAR FROM created_at) AS year_num,
  EXTRACT(MONTH FROM created_at) AS month_num
FROM orders;

Useful for grouping and reporting by calendar parts.

Add an interval

Shift a date or timestamp forward or backward.

sqlANYdateinterval
sql
SELECT created_at, created_at + INTERVAL '7 days' AS plus_seven_days
FROM orders;

Interval syntax varies slightly across engines.

Cast timestamp to date

Remove the time portion from a timestamp.

sqlANYdatecast
sql
SELECT CAST(created_at AS DATE) AS created_date
FROM orders;

Useful for daily rollups.

Conditional, NULL, and Conversion Functions

CASE, COALESCE, NULLIF, CAST, and type-safe expressions.

Use CASE expression

Return different values based on conditions.

sqlANYcaseconditional
sql
SELECT order_id,
       CASE
         WHEN amount >= 1000 THEN 'large'
         WHEN amount >= 100 THEN 'medium'
         ELSE 'small'
       END AS bucket
FROM orders;

CASE is one of the most useful SQL expressions for reporting and categorization.

Replace NULL with fallback value

Return the first non-NULL value in a list.

sqlANYcoalescenull-handling
sql
SELECT COALESCE(phone, mobile_phone, 'N/A') AS preferred_phone
FROM contacts;

COALESCE is portable and common across SQL engines.

Convert a value to NULL conditionally

Return NULL when two expressions are equal.

sqlANYnullifnull-handling
sql
SELECT revenue / NULLIF(order_count, 0) AS revenue_per_order
FROM daily_metrics;

NULLIF is often used to avoid divide-by-zero errors.

Convert data types

Cast an expression to a different type.

sqlANYcastconversion
sql
SELECT CAST(total AS DECIMAL(12,2)) AS total_decimal
FROM invoices;

Explicit casts make query intent clearer and avoid implicit conversion surprises.

Advanced Aggregation

Conditional aggregation and distinct counting.

Count distinct values

Count unique entries in a column.

sqlANYcount-distinctaggregation
sql
SELECT COUNT(DISTINCT customer_id) AS unique_customers
FROM orders;

Distinct counts are common in analytics and business reporting.

Conditional aggregation with CASE

Compute multiple categorized totals in one query.

sqlANYaggregationcasereporting
sql
SELECT
  COUNT(*) AS total_orders,
  SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid_orders,
  SUM(CASE WHEN status = 'refunded' THEN 1 ELSE 0 END) AS refunded_orders
FROM orders;

CASE inside aggregates is a staple reporting pattern.

Aggregate by date

Roll up metrics by calendar day.

sqlANYaggregationdatereporting
sql
SELECT CAST(created_at AS DATE) AS day, COUNT(*) AS total
FROM events
GROUP BY CAST(created_at AS DATE)
ORDER BY day;

Very common for dashboards and time-series summaries.

Recommended next

No recommendations yet.