Learn/SQL

SQL fundamentals

Output prediction and multiple choice questions covering the SQL mechanics that trip people up.

227 questions17 topics
SELECT Basics16 questions
Select one columnEasy
Arithmetic expression in SELECTEasy
Column order in SELECTEasy
What SELECT * returnsEasy
LIMIT rows returnedEasy
Selecting a constant valueEasy
Column alias with ASEasy
SELECT * vs named columnsEasy
Alias without AS keywordEasy
Selecting a numeric literalEasy
DISTINCT on multiple columnsMedium
LIMIT with OFFSETMedium
Where column aliases can be usedMedium
DISTINCT removes duplicatesMedium
Alias in WHERE clauseMedium
SELECT without FROMHard
Filtering14 questions
WHERE with !=Easy
WHERE with equalityEasy
IN operatorEasy
WHERE with ANDEasy
WHERE with OREasy
BETWEEN is inclusive on both endsMedium
AND/OR operator precedenceMedium
LIKE with % wildcardMedium
NOT IN operatorMedium
LIKE with _ wildcardMedium
NOT BETWEEN behaviorMedium
NOT IN with NULL in the listHard
IS NULL vs = NULLHard
IS NOT NULL filterHard
Sorting15 questions
ORDER BY multiple columnsEasy
Alphabetical sortEasy
ORDER BY combined with WHEREEasy
ORDER BY descendingEasy
ORDER BY ascendingEasy
Multi-column sort with different directionsEasy
NULLS FIRST / NULLS LASTMedium
NULL values in ORDER BYMedium
ORDER BY expressionMedium
ORDER BY column positionMedium
Top-N pattern with ORDER BY and LIMITMedium
ORDER BY string functionMedium
Danger of positional ORDER BYMedium
Bottom-N patternMedium
Result order without ORDER BYHard
Aggregation13 questions
AVG of a columnEasy
SUM of a columnEasy
COUNT(*) counts all rowsEasy
Aggregate with WHEREEasy
MIN and MAXEasy
COUNT(column) ignores NULLsEasy
MIN on a text columnEasy
COUNT with WHEREEasy
COUNT(DISTINCT column)Medium
SUM when all values are NULLMedium
AVG and NULL rowsMedium
Mixing aggregate and non-aggregate columnsHard
Valid fix for non-grouped columnHard
Grouping12 questions
Basic GROUP BYEasy
GROUP BY multiple columnsEasy
COUNT per groupEasy
GROUP BY with ORDER BYEasy
Distinct groups with multi-column GROUP BYEasy
Sort grouped results by aggregate valueEasy
WHERE filters before groupingMedium
Average per groupMedium
Non-grouped column in SELECTMedium
Which SELECT is valid with GROUP BY dept?Medium
HAVING filters after groupingMedium
GROUP BY vs DISTINCTHard
Filtering Groups9 questions
HAVING with COUNTEasy
Basic HAVING clauseEasy
WHERE vs HAVINGEasy
WHERE and HAVING togetherMedium
HAVING with MINMedium
HAVING with aliasMedium
HAVING with MAXMedium
Full GROUP BY pipelineHard
HAVING without GROUP BYHard
Joins19 questions
LEFT JOIN keeps all left rowsEasy
INNER JOIN basicsEasy
RIGHT JOIN keeps all right rowsEasy
What INNER JOIN excludesEasy
ON vs WHERE in a LEFT JOINMedium
Finding unmatched rows with LEFT JOINMedium
CROSS JOIN produces a cartesian productMedium
Joining three tablesMedium
Non-equi joinMedium
Joining on multiple conditionsMedium
Order of chained JOINsMedium
Fan-out from single-column join on composite keyMedium
FULL OUTER JOINHard
NULL in a join keyHard
LEFT JOIN converted to INNER by WHEREHard
Fan-out from duplicate rows before joiningHard
JOIN vs subquery — when to use eachHard
FULL OUTER JOIN vs LEFT JOINHard
Detecting unexpected fan-outHard
Self Joins8 questions
Employee-manager self joinEasy
What is a self join?Easy
Self join with LEFT JOIN for hierarchyEasy
Comparing rows within the same tableMedium
Two-level hierarchyMedium
Finding pairs in the same groupMedium
Why aliases are required in self joinsHard
Chained self joins and missing rowsHard
Subqueries15 questions
Scalar subquery in WHEREEasy
Subquery with INEasy
Subquery with NOT INEasy
Derived table must be aliasedEasy
Scalar subquery in SELECTMedium
Correlated subqueryMedium
EXISTS subqueryMedium
Scalar subquery in HAVINGMedium
Scalar subquery must return one rowMedium
EXISTS vs IN - semantic differenceMedium
HAVING vs WHERE with subqueriesMedium
Subquery in FROM (derived table)Hard
NOT IN subquery with NULLsHard
Correlated subquery performanceHard
NOT IN vs NOT EXISTSHard
CTEs13 questions
Basic CTE with WITHEasy
Multiple CTEsEasy
CTE vs subqueryEasy
CTE referencing a prior CTEEasy
CTE used in a JOINMedium
CTE scopeMedium
CTE referenced multiple timesMedium
Recursive CTE conceptHard
Recursive CTE generating a sequenceHard
CTEs and the optimizerHard
CTE vs temporary tableHard
Recursive CTE - org chart traversalHard
Forcing CTE materializationHard
CASE Expressions13 questions
Simple CASE (equality form)Easy
Basic CASE WHENEasy
Multiple WHEN branchesEasy
CASE as a replacement for nested IFsEasy
Conditional aggregation with CASEMedium
CASE with no ELSEMedium
CASE in ORDER BYMedium
COUNT with conditional CASEMedium
CASE WHEN NULL - equality vs IS NULLMedium
Pivoting rows to columns with CASEMedium
CASE in GROUP BYMedium
When to use CASE pivot vs PIVOT keywordMedium
CASE and NULLHard
Set Operations10 questions
INTERSECT returns common rowsEasy
UNION ALL keeps duplicatesEasy
EXCEPT removes rows found in the second setEasy
UNION removes duplicatesEasy
EXCEPT with no remaining rowsEasy
INTERSECT vs inner joinEasy
Column count in UNIONMedium
ORDER BY with UNIONMedium
UNION vs JOINHard
Choosing UNION or JOIN for a real scenarioHard
Window Functions22 questions
PARTITION BYEasy
ROW_NUMBER basicsEasy
RANK vs ROW_NUMBER on tiesMedium
Running total with ORDER BY in OVERMedium
DENSE_RANK has no gapsMedium
SUM as a window functionMedium
NTILE divides rows into bucketsMedium
Window function vs GROUP BYMedium
LAG with offset > 1Medium
Multiple window functions in one queryMedium
NTILE with uneven bucket sizesMedium
Named windows with the WINDOW clauseMedium
Can you mix window functions and GROUP BY?Medium
LAG to access the previous rowHard
Top-N per group with window functionsHard
PERCENT_RANKHard
ROWS BETWEEN frame clauseHard
NULLs in window ORDER BYHard
FIRST_VALUE and LAST_VALUEHard
Top-N per group: why a subquery is neededHard
Why LAST_VALUE surprises developersHard
ROWS vs RANGE frame modesHard
Date Filtering13 questions
Filter by exact dateEasy
Filter with date comparison operatorsEasy
BETWEEN inclusivity on date boundariesEasy
Equality on a date column with no matchEasy
Less-than date comparisonEasy
Filtering relative to todayMedium
Filtering timestamps within a dayMedium
Filter by year or month using EXTRACTMedium
CURRENT_DATE vs NOW() for date filteringMedium
EXTRACT for day-of-week filteringMedium
Filtering NULL datesHard
Index-friendly date filteringHard
IS NOT NULL on a date columnHard
Date Functions12 questions
EXTRACT a date partEasy
Difference between two datesEasy
Date arithmetic with intervalsEasy
Getting the current date and timeEasy
EXTRACT month and dayEasy
NOW() vs CURRENT_TIMESTAMPEasy
Filtering by date rangeMedium
Grouping by monthMedium
DATE_TRUNC truncates to a precisionMedium
Calculating age or elapsed timeHard
TIMESTAMP vs TIMESTAMPTZHard
Inclusive vs exclusive upper bound in date rangesHard
String Functions15 questions
String concatenationEasy
SUBSTRING extractionEasy
LENGTH of a stringEasy
UPPER and LOWEREasy
REPLACE all occurrencesEasy
TRIM removes whitespaceEasy
LENGTH with spacesEasy
UPPER on mixed caseEasy
LTRIM vs RTRIMEasy
REPLACE with no matchEasy
POSITION finds a substringMedium
LEFT and RIGHTMedium
Concatenation with NULLMedium
POSITION when substring is absentMedium
Case-insensitive LIKE searchHard
Distinct8 questions
DISTINCT on multiple columnsEasy
DISTINCT on one columnEasy
COUNT(DISTINCT column)Medium
DISTINCT treats NULLs as equalMedium
DISTINCT vs GROUP BY for unique valuesMedium
DISTINCT applies to all selected columnsHard
DISTINCT inside an aggregateHard
DISTINCT cannot use aggregatesHard