sqlbeginner
Handle NULLs with COALESCE
Use COALESCE to replace NULL values with a default in SQL queries.
sqlPress ⌘/Ctrl + Shift + C to copy
-- Replace NULL with a default value
SELECT
employee_id,
first_name,
COALESCE(phone, 'N/A') AS phone,
COALESCE(department, 'Unassigned') AS department
FROM employees;
-- Chain multiple fallbacks
SELECT
product_id,
COALESCE(sale_price, retail_price, wholesale_price, 0) AS effective_price
FROM products;Use Cases
- Replacing NULL values in reports
- Default values for missing data
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
sqladvanced
Recursive CTE for Hierarchical Data
Query hierarchical data like org charts, categories, and file trees using recursive CTEs.
Best for: Querying organizational hierarchies
#sql#cte
sqlintermediate
SQL Pivot and Crosstab Queries
Transform row data into columnar reports using CASE expressions, FILTER, and crosstab patterns.
Best for: Creating monthly revenue reports
#sql#pivot
sqlintermediate
SQL Upsert and Merge Patterns
Insert or update records atomically using ON CONFLICT, MERGE, and database-specific upsert syntax.
Best for: Syncing data from external sources
#sql#upsert
sqladvanced
SQL Index Strategy Patterns
Create effective indexes including composite, partial, covering, and expression-based indexes.
Best for: Optimizing slow database queries
#sql#indexes