Premium
SQL Snippets.

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

SQL

Perform Case-Insensitive Full-Text Search

Implement basic case-insensitive full-text search across multiple text columns in your database, leveraging the `ILIKE` operator (PostgreSQL) for flexible pattern matching.

View Snippet →
SQL

Query JSON Data Stored in a Column (PostgreSQL)

Extract and filter data from JSON columns in PostgreSQL, demonstrating how to access nested properties and array elements within JSON structures using SQL functions.

View Snippet →
SQL

Identify and Count Duplicate Records

Locate rows in a table that share identical values in one or more specified columns, useful for data cleaning and ensuring data integrity in your database.

View Snippet →
SQL

Traverse Hierarchical Data with Recursive CTE

Query tree-like or hierarchical data structures (e.g., categories, comments, organizational charts) using a Common Table Expression (CTE) to traverse parent-child relationships.

View Snippet →
SQL

Identify Records Without Corresponding Entries in Another Table

Efficiently find 'orphan' records in a primary table that lack matching entries in a related table using an SQL anti-join pattern. Essential for data integrity checks.

View Snippet →
SQL

Filter Aggregated Results Using HAVING Clause

Learn how to group data and apply conditions to the aggregated results using the SQL HAVING clause, essential for complex reporting and analytics.

View Snippet →
SQL

Calculate Cumulative Sum (Running Total) Using Window Functions

Discover how to compute a running total or cumulative sum over a set of rows in SQL, useful for financial analysis, sales trends, and progress tracking.

View Snippet →
SQL

Retrieve Latest Record for Each Group Using ROW_NUMBER()

Efficiently fetch the most recent or latest entry for each distinct category or group in your SQL database, crucial for versioning and history tracking.

View Snippet →
SQL

Perform Multi-Column Conditional Update with CASE

Learn to update multiple columns in a single SQL statement based on different conditions using the powerful CASE expression for flexible data manipulation.

View Snippet →
SQL

Rank Rows Within Groups Using SQL Window Functions

Apply SQL window functions (ROW_NUMBER(), RANK()) to assign ranks within groups. Essential for leaderboards, top N results, and various analytical reporting needs.

View Snippet →
SQL

Perform Atomic Insert or Update (Upsert) in SQL

Efficiently insert a new record or update an existing one atomically using SQL's UPSERT pattern. Prevents race conditions and ensures data integrity.

View Snippet →
SQL

Filter Records Using a Subquery in SQL

Master SQL subqueries for advanced data filtering. Select or exclude records based on results from another query, enabling complex conditional selections.

View Snippet →