SQL queries and database design
Query and protect structured data for analytics.
Use parameterized statements and inspect query plans for performance.
Interview questions
Choose a question to reveal its answer.
01EasyCoreWhat constraints does a primary key enforce?
Answer
What constraints does a primary key enforce?
Uniquely identifies a table row and cannot be null.
Assign every test attempt a unique ID.
SELECT topic_slug, AVG(score) AS average_score
FROM test_attempts
WHERE user_id = $1
GROUP BY topic_slug
ORDER BY average_score;Bind the user ID as a parameter and verify ownership server-side.
02EasyCoreWhy use foreign keys?
Answer
Why use foreign keys?
Enforces a relationship to a referenced unique or primary key.
Ensure an answer references an existing attempt.
03EasyCoreHow does an INNER JOIN work?
Answer
How does an INNER JOIN work?
Returns rows satisfying the join condition in both inputs.
List attempts that have a matching user record.
04MediumHigh priorityWhen do you need a LEFT JOIN?
Answer
When do you need a LEFT JOIN?
Preserves all left rows and fills unmatched right columns with null.
List all topics even when no attempts exist.
05MediumHigh priorityHow does GROUP BY change query output?
Answer
How does GROUP BY change query output?
Groups rows to compute aggregates per key.
Calculate mean score for each topic.
06MediumWhat is the difference between WHERE and HAVING?
Answer
What is the difference between WHERE and HAVING?
Filters groups after aggregation.
Keep topics with more than five attempts.
07MediumWhen does an index improve query performance?
Answer
When does an index improve query performance?
Auxiliary data structure that can speed selected access patterns with write and storage costs.
Accelerate queries filtering by user ID.
08MediumWhy use a transaction for related writes?
Answer
Why use a transaction for related writes?
Groups operations into an atomic unit with defined isolation semantics.
Save an attempt and its answers together or neither.
09HardHow do window functions differ from GROUP BY?
Answer
How do window functions differ from GROUP BY?
Computes over related rows without collapsing them into one row per group.
Rank test attempts within each topic.
10HardHow does row-level security protect user records?
Answer
How does row-level security protect user records?
Database policies restrict which rows are visible or modifiable to a role.
Prevent a signed-in student from reading another student's history.
11MediumHigh priorityHow does a UNIQUE constraint differ from a primary key?
Answer
How does a UNIQUE constraint differ from a primary key?
Prevents duplicate combinations of constrained columns, with null behavior depending on database semantics.
Ensure one saved progress row per user and lesson.
12MediumHigh priorityWhy does column = NULL not work as expected?
Answer
Why does column = NULL not work as expected?
Represents missing or unknown information and participates in SQL three-valued logic.
Use IS NULL rather than equality to find missing values.
13MediumHigh priorityHow do you choose column order for a composite index?
Answer
How do you choose column order for a composite index?
Index over multiple columns whose ordering and query predicates affect usefulness.
Index user ID and creation time for recent attempts per user.
14MediumHigh priorityWhat problem do transaction isolation levels solve?
Answer
What problem do transaction isolation levels solve?
Defines which concurrent transaction interactions are permitted by a database.
Choose appropriate isolation when concurrent writes must preserve an invariant.
15MediumHigh priorityHow do you investigate a slow PostgreSQL query?
Answer
How do you investigate a slow PostgreSQL query?
Displays a database query plan; EXPLAIN ANALYZE executes the statement and reports observed execution statistics.
Investigate whether a query uses an index or scans the whole table.
Priority and difficulty are editorial study recommendations.
Technical reference: Documentation / paper ↗
Try a few practice questions
Explore the questions before creating a free account.
1. Which join preserves all rows of the left table even without matches?
2. Which SQL expression correctly checks a null column?
3. Which operation makes multiple related writes atomic?
PostgreSQL transactions and performance
Continue to the next set of interview questions.