Premium
SQL Snippets.

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

SQL

Perform UPSERT (INSERT or UPDATE) in SQL

Learn to implement UPSERT logic (insert new record or update existing) in SQL, critical for managing unique data entries without duplicates.

View Snippet →
SQL

Filter Records by Date Range in SQL

Filter database records by specific date and time ranges using SQL `WHERE` clauses, essential for reports and time-based data retrieval.

View Snippet →
SQL

Calculate Running Total (Cumulative Sum) in SQL

Learn how to compute a running total or cumulative sum over a set of rows in SQL using window functions, essential for financial reports and trend analysis.

View Snippet →
SQL

Find Nth Highest Value in SQL

Discover how to retrieve the Nth highest value from a column in SQL using DENSE_RANK() or a subquery, a fundamental skill for data analysis.

View Snippet →
SQL

Perform Upsert (Insert or Update on Conflict) in SQL

Learn to efficiently perform an 'upsert' operation in SQL, allowing you to insert a row if it doesn't exist or update it if a conflict occurs, crucial for data synchronization. (PostgreSQL syntax)

View Snippet →
SQL

Compare Current and Previous Rows with LAG/LEAD Window Functions

Utilize SQL's LAG and LEAD window functions to compare values between the current row and preceding or succeeding rows, invaluable for time-series analysis and change detection.

View Snippet →
SQL

Extract Data from JSON Columns in SQL

Learn how to query and extract specific values from JSON data stored in a database column using SQL's native JSON functions, crucial for modern applications (PostgreSQL syntax).

View Snippet →
SQL

Identify and Delete Duplicate Rows Efficiently

Discover SQL techniques to find duplicate records in your table using window functions and Common Table Expressions, and safely remove extra duplicates while preserving one unique entry.

View Snippet →
SQL

Perform Conditional Aggregation with CASE

Master SQL conditional aggregation using the CASE statement within aggregate functions (SUM, COUNT) to generate insightful reports from a single, efficient query.

View Snippet →
SQL

Retrieve Data for Specific Date Ranges

Learn essential SQL techniques for filtering records based on dynamic or fixed date ranges, such as the last 7 days, current month, or a specific period, using common database functions.

View Snippet →
SQL

Ranking Data within Partitions using Window Functions

Assign sequential ranks to rows within defined groups or partitions based on specific criteria using SQL window functions like ROW_NUMBER(), DENSE_RANK(), or RANK().

View Snippet →
SQL

Identify Records Lacking Related Data with LEFT JOIN

Discover records in one table that do not have corresponding entries in a related table by combining a LEFT JOIN with a WHERE clause checking for NULLs.

View Snippet →