Premium
SQL Snippets.

Curated list of production-ready SQL scripts and coding solutions.

SQL

Aggregate Data and Filter Groups with GROUP BY and HAVING

Learn to summarize database records using SQL GROUP BY for counts, sums, and averages, then filter these aggregated results using the HAVING clause, ideal for reporting.

View Snippet →
SQL

Perform Conditional Updates on Multiple Rows

Learn to update multiple database rows with different values based on specific conditions by utilizing the powerful SQL CASE statement within an UPDATE query.

View Snippet →
SQL

Perform Atomic Upsert Operations with MySQL's `INSERT ... ON DUPLICATE KEY UPDATE`

Master MySQL's `INSERT ... ON DUPLICATE KEY UPDATE` to atomically insert a new row or update an existing one if a unique key constraint is violated, ensuring data integrity.

View Snippet →
SQL

Calculate Running Totals Per Group using SQL Window Functions

Utilize SQL window functions with `SUM() OVER (PARTITION BY ... ORDER BY ...)` to efficiently compute cumulative sums or running totals for data within distinct categories or groups.

View Snippet →
SQL

Transform Rows to Columns (Pivot Data) with SQL `CASE` Statements

Learn to dynamically pivot data in SQL, converting unique row values into distinct columns using conditional aggregation with `SUM()` and `CASE` statements for clearer reporting.

View Snippet →
SQL

Construct and Query JSON Data in MySQL

Master MySQL's JSON functions like `JSON_OBJECT`, `JSON_ARRAYAGG`, and `JSON_EXTRACT` to dynamically create, manipulate, and query JSON data directly within your database tables.

View Snippet →
SQL

Calculate Running Total (Cumulative Sum)

Learn to calculate cumulative sums or running totals for financial transactions or sequential data using SQL window functions, providing invaluable insights into progressive data trends.

View Snippet →
SQL

Find Nth Highest Value Using DENSE_RANK()

Discover how to efficiently retrieve the Nth highest value, such as the Nth highest salary, from a dataset using the DENSE_RANK() window function in SQL, perfect for ranking-based queries.

View Snippet →
SQL

Delete Duplicate Rows Keeping One (General SQL)

Learn a general SQL method to efficiently remove duplicate records from a table, retaining only one instance of each duplicate set based on a specified ordering criterion.

View Snippet →
SQL

Extract Values from PostgreSQL JSONB Columns

Learn to efficiently query and extract specific values from JSONB columns in PostgreSQL using operators like `->>` and `@>`, enabling advanced filtering and data retrieval.

View Snippet →
SQL

Optimize Existence Checks with EXISTS

Enhance your SQL query performance by using the `EXISTS` operator instead of `IN` for subqueries, particularly effective for checking the mere existence of related records.

View Snippet →
SQL

Efficient Pagination with ROW_NUMBER() in SQL

Learn to implement robust and efficient pagination in SQL using window functions like ROW_NUMBER(). This method ensures consistent ordering and avoids OFFSET/LIMIT pitfalls.

View Snippet →