https://github.com/count/sql-snippets
A curated collection of helpful SQL queries and functions, maintained by Count.
https://github.com/count/sql-snippets
Last synced: 5 months ago
JSON representation
A curated collection of helpful SQL queries and functions, maintained by Count.
- Host: GitHub
- URL: https://github.com/count/sql-snippets
- Owner: count
- License: mit
- Created: 2021-06-28T10:53:50.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-07T10:36:35.000Z (over 3 years ago)
- Last Synced: 2024-08-09T02:19:56.392Z (8 months ago)
- Homepage:
- Size: 346 KB
- Stars: 198
- Watchers: 8
- Forks: 24
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - count/sql-snippets - A curated collection of helpful SQL queries and functions, maintained by Count. (Others)
README
# SQL Snippets
A curated collection of helpful SQL queries and functions, maintained by [Count](https://count.co).All [contributions](./CONTRIBUTING.md) are very welcome - let's get useful SQL snippets out of Slack channels and Notion pages, and collect them as a community resource.
### [🙋♀️ Request a snippet](https://github.com/count/sql-snippets/issues/new?assignees=&labels=help+wanted&template=snippet-request.md&title=%5BSNIPPET+REQUEST%5D+)
### [⁉️ Report a bug](https://github.com/count/sql-snippets/issues/new?assignees=&labels=bug&template=bug_report.md&title=%5BBUG%5D+)
### [↣ Submit a pull request](https://github.com/count/sql-snippets/compare)| Snippet | Databases |
| ------- | --------- |
|Analytics
| |
| Cohort analysis | [](./bigquery/cohort-analysis.md) |
| Cumulative distribution functions | [](./bigquery/cdf.md) [](./postgres/cume_dist.md) [](./mysql/cdf.md) |
| Compound growth rates | [](./bigquery/compound-growth-rates.md) [](./mssql/compound-growth-rates.md) |
| Generate a binomial distribution | [](./bigquery/binomial-distribution.md) |
| Generate a uniform distribution | [](./bigquery/uniform-distribution.md) |
| Histogram bins | [](./bigquery/histogram-bins.md) [](./snowflake/histogram-bins.md) [](./postgres/histogram-bins.md) |
| Median | [](./bigquery/median.md) |
| Median (UDF) | [](./bigquery/median-udf.md) |
| Outlier detection: MAD method | [](./bigquery/outliers-mad.md) |
| Outlier detection: Standard Deviation method | [](./bigquery/outliers-stdev.md) |
| Outlier detection: Z-score method | [](./bigquery/outliers-z.md) |
| Random Between | [](./bigquery/rand_between.md) |
| Random sampling | [](./bigquery/random-sampling.md) [](./snowflake/random-sampling.md) |
| Ranking | [](./bigquery/rank.md) [](./snowflake/rank.md) [](./postgres/rank.md) |
| TF-IDF | [](./bigquery/tf-idf.md) |
|Arrays
| |
| Arrays to columns | [](./bigquery/array-to-columns.md) [](./snowflake/flatten-array.md) | |
| Filtering arrays | [](./bigquery/filtering-arrays.md) [](./snowflake/filtering-arrays.md) |
| Generating arrays | [](./bigquery/generating-arrays.md) [](./snowflake/generate-arrays.md) |
| Get last array element | [](./bigquery/get-last-array-element.md) [](./postgres/get-last-array-element.md) [](./snowflake/last-array-element.md) |
| Reverse array | [](./postgres/array-reverse.md) |
| Transforming arrays | [](./bigquery/transforming-arrays.md) [](./snowflake/transforming-arrays.md) |
| Least non-null value in array (UDF) | [](./bigquery/least-udf.md) |
| Greatest/Least value in array | [](./bigquery/least-array.md) |
| Sort Array (UDF) | [](./snowflake/udf-sort-array.md) |
|Charts
| |
| Bar chart | [](./bigquery/bar-chart.md) [](./snowflake/bar-chart.md) |
| Horizontal bar chart | [](./bigquery/horizontal-bar.md) [](./snowflake/horizontal-bar.md) |
| Time series | [](./bigquery/timeseries.md) [](./snowflake/timeseries.md) |
| 100% stacked bar chart | [](./bigquery/100-stacked-bar.md ) [](./snowflake/100-stacked-bar.md) |
| Heat map | [](./bigquery/heatmap.md) [](./snowflake/heatmap.md) |
| Stacked bar and line chart | [](./bigquery/stacked-bar-line.md) [](./snowflake/stacked-bar-line.md) |
|Database metadata
| |
| Check if column exists in table | [](./mssql/check-column-is-accessible.md) |
| Search for text in Stored Procedures | [](./mssql/search-stored-procedures.md) |
|Dates and times
| |
| Date/Time Formatting One Pager| [](./postgres/dt-formatting.md) |
| Converting from strings | [](./bigquery/convert-string-datetimes.md) |
| Converting to strings | [](./bigquery/convert-datetimes-string.md) |
| Local Timezone to UTC | [](./bigquery/localtz-to-utc.md) |
| Converting epoch/unix to timestamp | [](./postgres/convert-epoch-to-timestamp.md) |
| Generate timeseries | [](./postgres/generate-timeseries.md) |
| Last day of the month | [](./bigquery/last-day.md) |
| Last X days | [](./postgres/last-x-days.md) |
|Geography
| |
| Calculating the distance between two points | [](./bigquery/geographical-distance.md) [](./snowflake/geographical-distance.md) |
|JSON
| |
| Parsing JSON strings | [](./bigquery/json-strings.md) [](./postgres/json-strings.md) [](./snowflake/parse-json.md)|
|Regular & String expressions
| |
| Parsing URLs | [](./bigquery/regex-parse-url.md) [](./snowflake/parse-url.md) [](./postgres/regex-parse-url.md) |
| Validating emails | [](./bigquery/regex-email.md) [](./postgres/regex-email.md) |
| Extract repeating words | [](./bigquery/repeating-words.md) |
| Split on Uppercase characters | [](./bigquery/split-uppercase.md) |
| Replace multple conditions | [](./bigquery/replace-multiples.md) |
| CONCAT with NULLs | [](./bigquery/concat-nulls.md) |
| Capitalize initial letters (UDF) | [](./mysql/initcap-udf.md) |
| Concatenating strings | [](./mssql/concatenate-strings.md) |
|Transformation
| |
| Pivot | [](./bigquery/pivot.md) [](./snowflake/pivot.md) |
| Unpivoting / melting | [](./bigquery/unpivot-melt.md) [](./snowflake/unpivot-melt.md) |
| Replace NULLs | [](./bigquery/replace-null.md) [](./postgres/replace-null.md) [](./snowflake/replace-null.md) |
| Replace empty string with NULL | [](./bigquery/replace-empty-strings-null.md) [](./postgres/replace-empty-strings-null.md) [](./snowflake/replace-empty-strings-null.md) |
| Counting NULLs | [](./bigquery/count-nulls.md) [](./postgres/count-nulls.md) [](./snowflake/count-nulls.md) |
| Splitting single columns into rows | [](./postgres/split-column-to-rows.md) |
| Latest Row (deduplicate) | [](./bigquery/latest-row.md) |
|Window functions
| |
| First row of each group | [](./bigquery/first-row-of-group.md) [](./mssql/first-row-of-group.md) |
| Moving averages | [](./bigquery/moving-average.md) [](./snowflake/moving-average.md) [](./postgres/moving-average.md) |
| Running totals | [](./bigquery/running-total.md) [](./snowflake/running-total.md) |
| Year-on-year changes | [](./bigquery/yoy.md) [](./snowflake/yoy.md) |
| Creating equal-sized buckets | [](./postgres/ntile.md) |## Other resources
- [bigquery-utils](https://github.com/GoogleCloudPlatform/bigquery-utils) - a BigQuery-specific collection of scripts