SQL
92 bites tagged SQL — interview questions with model answers, and 60-second explainers.
Design a data warehouse model for tracking feature adoption
This tests your grasp of data warehousing star schemas for efficient behavioral analysis. A strong answer proposes a central `events` fact table linked to `users`, `features`, and `time` dimension tables.
Explain cohort analysis for user retention and write a pseudo-query
Tests your ability to use precise metrics. A good answer defines a cohort, explains why it isolates variables better than aggregate data, outlines the calculation, and provides a clear pseudo-query.
How would you optimize a slow, expensive data warehouse?
Tests your diagnostic approach to performance issues. A good answer first analyzes query patterns, then applies partitioning by date, clustering by high-cardinality keys, and materialized views for aggregations.
Generate a monthly cohort retention table from user events
This tests your ability to translate a business metric into a multi-step SQL query. A great answer defines cohorts by first activity, maps subsequent activity to period indices, counts distinct users, and pivots the result.
How do you ensure accurate counts with duplicate analytics events?
Tests your grasp of data integrity under at-least-once delivery. Explain why COUNT(*) is inflated, then propose deduplication using a unique event ID. Mention trade-offs of stateful processing. A red flag is ignoring the cost or the need for a unique ID.
Explain a star schema and its advantages for analytics
Tests your grasp of data modeling trade-offs for OLAP vs. OLTP. A good answer defines fact/dimension tables, then explains how denormalization and fewer joins improve query speed for analytics. A red flag is confusing it with a snowflake schema.
How would you diagnose a slow dashboard query?
This tests your systematic approach to performance tuning. A great answer investigates query optimization, strategic indexing/partitioning, and the data model (like star schema).
Describe star and snowflake schemas and their trade-offs.
Tests your grasp of data warehouse design trade-offs. Define star (denormalized dimensions) and snowflake (normalized dimensions) schemas. Contrast them: star is faster for queries but uses more space; snowflake is space-efficient but requires more joins.
Alembic: Version Control for Your Database Schema
Alembic is like Git for your database schema, providing versioned, reversible changes. Use it with SQLAlchemy to evolve your database structure alongside your code. The footgun is that autogeneration can miss changes; always review generated scripts.
Preventing SQL Injection: Never Trust User Input
To prevent SQL injection, treat SQL as a template and user input as data that can only fill placeholders, never changing the query's structure. Use this for any database query in your Node.js app that uses external data.
Point-in-Time Correctness: Avoiding Data Leakage in ML
A point-in-time correct join is a time-traveling lookup for ML features, grabbing the most recent values known *at the time of an event*. It's vital when building training data from feature tables that update at different rates to prevent data leakage.
Optimizer Hints: Backseat Driving Your Database
An optimizer hint lets you override the database's query plan, like telling a GPS which street to take. Use it as a last resort when you know more than the optimizer, but beware: hints can become performance traps when data or schemas change.
CockroachDB: A SQL Database That Survives Disasters
CockroachDB is a distributed SQL database designed to be unkillable. Use it for global apps needing strong consistency and high availability, like financial ledgers or identity systems. The footgun: ignoring network latency between nodes can kill performance.
Database Cursors: Row-by-Row Result Processing
A database cursor is an iterator for a query's results, letting you process a large dataset one row at a time. It's for batch jobs on huge record sets that would otherwise crash your app.
SQL Query Builders: Write SQL Without Writing SQL
An SQL query builder is a translator for your database, converting visual clicks or chained code methods into raw SQL. It's used to write safer, database-agnostic code or to let non-technical users build queries. The footgun is generating inefficient queries.
The Object-Relational Impedance Mismatch
The Object-Relational Impedance Mismatch is the friction between how SQL databases see data (tables, rows) and how OO code sees it (objects, inheritance). It's the core problem ORMs solve. The footgun is thinking an ORM makes the database disappear.
JDBC: Java's Universal Translator for Databases
JDBC is Java's universal adapter for databases, letting your app speak SQL to any database via a standard API. It's used for connecting, querying, and managing transactions. The biggest footgun is building SQL strings directly; always use PreparedStatements.
SQL Injection: When User Input Becomes a Command
SQL injection tricks a database into running unintended commands by sneaking them into user input. It's a common attack on websites where user data is directly stitched into SQL queries. The footgun is trusting input; always use prepared statements instead.
How Database Indexes Rot and How to Fix Them
Your database indexes rot over time, making queries slower. Frequent writes cause fragmentation (disordered pages) and low page density (half-empty pages), forcing more disk I/O.
Query Rewriting: Your Database's Unseen Optimizer
A database's query rewriter is like a smart GPS, finding a faster route (execution plan) to the same destination (your query result). It happens automatically to speed up joins and filters. The footgun: your handwritten query isn't what actually runs.
Sort-Merge Join: The 'Line Up and Walk' Join
A sort-merge join is like merging two sorted lines of people. It's efficient when tables are already sorted on the join key or memory is tight. The footgun: if data isn't pre-sorted, the initial sort can make it slower than other join methods.
Hash Join: Faster Database Joins with Hash Tables
A hash join speeds up database joins by building an in-memory lookup table (a hash table) for the smaller table, then streaming the larger one past it to find matches. It's ideal for large, unsorted equijoins.
Nested Loop Join: The Brute-Force Database Join
A nested loop join is the brute-force way to match two tables. For each row in the first table, it scans every row in the second. It's simple and effective for small tables but its performance degrades quadratically on large datasets.
Composite Indexes: One Index for Multiple Columns
A composite index is like a phone book sorted by last name, then first name. It's for queries filtering on multiple columns, like finding a specific person. The footgun is creating separate indexes, which is far less efficient than a single composite one.
Get SQL bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.