Other Snowflake SQL Fxns

0.0(0)
studied byStudied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/117

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 6:18 AM on 1/22/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

118 Terms

1
New cards

How do you calculate percent of total in SQL without losing row detail? You want to know, for example, if a single row is emergency and also (on a row level) what the % emergency is across the dataset

Use SUM(value) OVER () to compute the total across all rows and divide each row by it (e.g., sales / SUM(sales) OVER () gives percent of total). SUM(emergency) OVER () / COUNT(*) OVER () AS pct_emergency_claims
Technically, you could also use AVG(emergency) over ()

for example, this would fail:
SELECT

claim_id,

emergency,

AVG(emergency) * 100 AS pct_emergency_claims

FROM claims;

because claim_id, emergency need to be in group by

But this would succeed:
SELECT

claim_id,

emergency,

AVG(emergency) over () * 100 AS pct_emergency_claims

FROM claims;

2
New cards

How do you calculate a running total in Snowflake SQL?

Use SUM(value) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) to accumulate values row by row without collapsing rows.

3
New cards

How do you rank rows within a group in SQL?

Use RANK() or DENSE_RANK() OVER (PARTITION BY group_col ORDER BY metric DESC) since ranking requires comparing rows without grouping them away.

4
New cards

How do you compare a row to its group average in SQL?

Use AVG(value) OVER (PARTITION BY group_col) to attach the group average to each row for direct comparison.

5
New cards

How do you get the max or min of a group while keeping all rows? On every row I want the min/max of that group column

Use MAX(value) OVER (PARTITION BY group_col) to show the group’s extreme value on every row.

6
New cards

Why can’t GROUP BY replace OVER() in certain cases?

GROUP BY collapses rows and removes row-level context

7
New cards

Calculate the average of numeric values across rows

AVG(expr) | AVG(expr) OVER ( [PARTITION BY expr] [ORDER BY expr] [ROWS|RANGE frame] ) | Example: AVG(salary) OVER (PARTITION BY dept ORDER BY hire_date)

8
New cards

Count rows or non-null values

COUNT() | COUNT(expr) | COUNT(expr) OVER (PARTITION BY expr) | Example: COUNT() OVER (PARTITION BY dept)

9
New cards

Return the total sum of numeric values

SUM(expr) | SUM(expr) OVER (PARTITION BY expr ORDER BY expr) | Example: SUM(revenue) OVER (PARTITION BY region)

10
New cards

Return the minimum value from a set

MIN(expr) | MIN(expr) OVER (PARTITION BY expr ORDER BY expr) | Example: MIN(price) OVER (PARTITION BY category)

11
New cards

Return the maximum value from a set

MAX(expr) | MAX(expr) OVER (PARTITION BY expr ORDER BY expr) | Example: MAX(score) OVER (PARTITION BY class)

12
New cards

Assign a unique sequential row number within a partition

ROW_NUMBER() OVER (PARTITION BY expr ORDER BY expr) | Example: ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC)

13
New cards

Rank rows with gaps when ties occur

RANK() OVER (PARTITION BY expr ORDER BY expr) | Example: RANK() OVER (PARTITION BY dept ORDER BY salary DESC)

14
New cards

Rank rows without gaps when ties occur

DENSE_RANK() OVER (PARTITION BY expr ORDER BY expr) | Example: DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC)

15
New cards

Return the first value in an ordered window

FIRST_VALUE(expr) OVER (PARTITION BY expr ORDER BY expr frame) | Example: FIRST_VALUE(salary) OVER (PARTITION BY dept ORDER BY hire_date)

16
New cards

Return the last value in an ordered window

LAST_VALUE(expr) OVER (PARTITION BY expr ORDER BY expr ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

17
New cards

Access a value from a prior row

LAG(expr

18
New cards

Access a value from a following row

LEAD(expr

19
New cards

Divide rows into buckets

NTILE(n) OVER (ORDER BY expr)

20
New cards

Compute cumulative distribution

CUME_DIST() OVER (PARTITION BY expr ORDER BY expr)

21
New cards

Compute relative rank as a percentile

PERCENT_RANK() OVER (PARTITION BY expr ORDER BY expr)

22
New cards

Return the absolute value of a number

ABS(expr)

23
New cards

Round a number to a specified scale

ROUND(expr

24
New cards

Round a number up

CEIL(expr) | CEILING(expr)

25
New cards

Round a number down

FLOOR(expr)

26
New cards

Return the remainder of division

MOD(expr1

27
New cards

Raise a number to a power

POWER(expr

28
New cards

Return the square root of a number

SQRT(expr)

29
New cards

Return a random number between 0 and 1

RANDOM()

30
New cards

Return the exponential value

e^x

31
New cards

Return the natural logarithm

LN(expr)

32
New cards

Return logarithm with base 10

LOG10(expr)

33
New cards

Return logarithm with specified base

LOG(base

34
New cards

Convert text to uppercase

UPPER(expr)

35
New cards

Convert text to lowercase

LOWER(expr)

36
New cards

Capitalize the first letter of each word

INITCAP(expr)

37
New cards

Remove leading and trailing characters

TRIM(expr) | TRIM(char FROM expr)

38
New cards

Remove leading characters

LPAD(expr

39
New cards

Remove trailing characters

RPAD(expr

40
New cards

Return string length

LENGTH(expr)

41
New cards

Extract part of a string

SUBSTR(expr

42
New cards

Replace text within a string

REPLACE(expr

43
New cards

Concatenate strings

CONCAT(expr1

44
New cards

Split string into array

SPLIT(expr

45
New cards

Search for substring position

POSITION(substring IN expr)

46
New cards

Return the current date

CURRENT_DATE()

47
New cards

Return the current timestamp

CURRENT_TIMESTAMP()

48
New cards

Add time to a date or timestamp

DATEADD(unit

49
New cards

Calculate difference between dates

DATEDIFF(unit

50
New cards

Truncate a date or timestamp

DATETRUNC(unit

51
New cards

Extract date part from date or timestamp

EXTRACT(part FROM date_expr)

52
New cards

Convert value to date

TO_DATE(expr

53
New cards

Convert value to timestamp

TO_TIMESTAMP(expr

54
New cards

Return last day of a period

LAST_DAY(date_expr

55
New cards

Return next specified weekday

NEXT_DAY(date_expr

56
New cards

Return first non-null value

COALESCE(expr1

57
New cards

Return null if expressions are equal

NULLIF(expr1

58
New cards

Conditional logic evaluation

CASE WHEN condition THEN result ELSE result END

59
New cards

Test for null value

expr IS NULL

60
New cards

Test for non-null value

expr IS NOT NULL

61
New cards

Return value based on condition

IFF(condition

62
New cards

Extract value from JSON object

GET(expr

63
New cards

Flatten arrays or objects into rows

FLATTEN(input => expr)

64
New cards

Check if JSON key exists

OBJECT_HAS_KEY(expr

65
New cards

Return JSON object size

OBJECT_SIZE(expr)

66
New cards

Return array size

ARRAY_SIZE(expr)

67
New cards

Convert a value to another data type explicitly

CAST(expr AS datatype)

68
New cards

Convert value to numeric type

TO_NUMBER(expr

69
New cards

Convert value to varchar

TO_VARCHAR(expr)

70
New cards

Convert value to boolean

TO_BOOLEAN(expr)

71
New cards

Convert value to binary

TO_BINARY(expr

72
New cards

Convert value to floating-point number

TO_DOUBLE(expr)

73
New cards

Safely convert value without error

TRY_CAST(expr AS datatype)

74
New cards

Safely convert to number returning null on failure

TRY_TO_NUMBER(expr)

75
New cards

Safely convert to date returning null on failure

TRY_TO_DATE(expr)

76
New cards

Safely convert to timestamp returning null on failure

TRY_TO_TIMESTAMP(expr)

77
New cards

Check if value is within a range

expr BETWEEN low AND high

78
New cards

Check if value matches any in a list

expr IN (value1

79
New cards

Check if value does not match list

expr NOT IN (value1

80
New cards

Check if condition is true for all rows

ALL(subquery)

81
New cards

Check if condition is true for any row

ANY(subquery)

82
New cards

Evaluate logical AND condition

expr AND expr

83
New cards

Evaluate logical OR condition

expr OR expr

84
New cards

Negate a boolean condition

NOT expr

85
New cards

Match text using SQL pattern matching

expr LIKE pattern

86
New cards

Match text using regex

REGEXP_LIKE(expr

87
New cards

Extract substring using regex

REGEXP_SUBSTR(expr

88
New cards

Replace text using regex

REGEXP_REPLACE(expr

89
New cards

Count regex matches

REGEXP_COUNT(expr

90
New cards

Return position of regex match

REGEXP_INSTR(expr

91
New cards

Create an array from values

ARRAY_CONSTRUCT(expr1

92
New cards

Append value to array

ARRAY_APPEND(array

93
New cards

Remove value from array

ARRAY_REMOVE(array

94
New cards

Check if array contains value

ARRAY_CONTAINS(value

95
New cards

Access array element by index

array[index]

96
New cards

Flatten array elements into rows

FLATTEN(input => array)

97
New cards

Create object from key-value pairs

OBJECT_CONSTRUCT(key

98
New cards

Retrieve value by key from object

object:key

99
New cards

Insert or update key-value pair

OBJECT_INSERT(object

100
New cards

Delete key from object

OBJECT_DELETE(object