Skip to content

Repository files navigation

SQL Snippets

A curated collection of helpful SQL queries and functions, maintained by Count.

All contributions are very welcome - let's get useful SQL snippets out of Slack channels and Notion pages, and collect them as a community resource.

SnippetDatabases

Analytics

Cohort analysisBigQuery
Cumulative distribution functionsBigQueryPostgresMySQLSQL Server
Compound growth ratesBigQuerySQL Server
Generate a binomial distributionBigQuery
Generate a uniform distributionBigQuery
Histogram binsBigQuerySnowflakePostgres
MedianBigQuery
Median (UDF)BigQuery
Outlier detection: MAD methodBigQuery
Outlier detection: Standard Deviation methodBigQuery
Outlier detection: Z-score methodBigQuery
Random BetweenBigQuery
Random samplingBigQuerySnowflake
RankingBigQuerySnowflakePostgres
TF-IDFBigQuery

Arrays

Arrays to columnsBigQuerySnowflake
Filtering arraysBigQuerySnowflake
Generating arraysBigQuerySnowflake
Get last array elementBigQueryPostgresSnowflake
Reverse arrayPostgres
Transforming arraysBigQuerySnowflake
Least non-null value in array (UDF)BigQuery
Greatest/Least value in arrayBigQuery
Sort Array (UDF)Snowflake

Charts

Bar chartBigQuerySnowflake
Horizontal bar chartBigQuerySnowflake
Time seriesBigQuerySnowflake
100% stacked bar chartBigQuerySnowflake
Heat mapBigQuerySnowflake
Stacked bar and line chartBigQuerySnowflake

Database metadata

Check if column exists in tableSQL Server
Search for text in Stored ProceduresSQL Server

Dates and times

Date/Time Formatting One PagerPostgres
Converting from stringsBigQuery
Converting to stringsBigQuery
Local Timezone to UTCBigQuery
Converting epoch/unix to timestampPostgres
Generate timeseriesPostgres
Last day of the monthBigQuery
Last X daysPostgres

Geography

Calculating the distance between two pointsBigQuerySnowflake

JSON

Parsing JSON stringsBigQueryPostgresSnowflake

Regular & String expressions

Parsing URLsBigQuerySnowflakePostgres
Validating emailsBigQueryPostgres
Extract repeating wordsBigQuery
Split on Uppercase charactersBigQuery
Replace multple conditionsBigQuery
CONCAT with NULLsBigQuery
Capitalize initial letters (UDF)MySQL
Concatenating stringsSQL Server

Transformation

PivotBigQuerySnowflake
Unpivoting / meltingBigQuerySnowflake
Replace NULLsBigQueryPostgresSnowflake
Replace empty string with NULLBigQueryPostgresSnowflake
Counting NULLsBigQueryPostgresSnowflake
Splitting single columns into rowsPostgres
Latest Row (deduplicate)BigQuery

Window functions

First row of each groupBigQuerySQL Server
Moving averagesBigQuerySnowflakePostgres
Running totalsBigQuerySnowflake
Year-on-year changesBigQuerySnowflake
Creating equal-sized bucketsPostgres

Other resources

About

A curated collection of helpful SQL queries and functions, maintained by Count.

Resources

Contributing

Stars

211 stars

Watchers

7 watching

Forks

Contributors