🌍 SQL & Databases · flashcards

SQL & Databases Querying Data with SQL Flashcards

51 question-and-answer cards covering Querying Data with SQL as it is examined in SQL & Databases. 24 of them are printed below, taken from across the deck — no signup, no paywall on the preview.

51Cards in deck
24Free preview
28Syllabus topics
~221Chars per answer
FreePrice

24 sample cards from the Querying Data with SQL deck

Sampled from the end of the deck, so these are different cards from the ones shown on the syllabus page.

  1. When joining three or more tables, how are the joins evaluated, and does textual order matter for INNER joins?

    Joins are logically evaluated left to right, each JOIN combining the accumulated result with the next table. For INNER joins the optimizer may reorder them freely (they are associative/commutative), so textual order doesn't change results. For OUTER joins, order matters.

  2. Why does join order matter when mixing INNER and OUTER joins?

    OUTER joins are not freely reorderable: an OUTER join preserves unmatched rows that a later INNER join (or WHERE on the null-able side) can silently discard, effectively turning the outer join into an inner one. Order and placement of predicates must be chosen carefully.

  3. What does the USING clause do in a join, and how does it differ from ON?

    USING (col) joins on columns with the same name in both tables (equivalent to ON A.col = B.col) and collapses the joined column into a single output column (not qualified by table). ON is more general and keeps both columns separately.

  4. What is a NATURAL JOIN, and why is it considered risky?

    A NATURAL JOIN automatically joins on ALL columns sharing the same name in both tables, with no explicit condition. It is risky because adding a same-named column later silently changes the join semantics, producing unexpected results; explicit ON/USING is safer.

  5. List the five standard SQL aggregate functions and what each computes.

    COUNT (number of rows/non-null values), SUM (total of values), AVG (arithmetic mean), MIN (minimum), MAX (maximum). They collapse a set of rows into a single scalar value per group.

  6. How do aggregate functions (other than COUNT(*)) treat NULL values?

    SUM, AVG, MIN, MAX, and COUNT(column) ignore NULLs — they operate only on non-null values. COUNT(*) counts all rows including those with NULLs. This means AVG divides by the count of non-null values, not total rows.

  7. What is the difference between COUNT(*), COUNT(column), and COUNT(DISTINCT column)?

    COUNT(*) counts all rows. COUNT(column) counts rows where column IS NOT NULL. COUNT(DISTINCT column) counts distinct non-null values of the column.

  8. What does the GROUP BY clause do, and how does it relate to aggregate functions?

    GROUP BY partitions rows into groups sharing the same values of the listed columns; aggregate functions then compute one summary value per group. The result has one row per distinct group.

  9. What is the fundamental rule about non-aggregated columns in the SELECT list when using GROUP BY?

    Every column in SELECT that is not inside an aggregate function must appear in the GROUP BY clause (functional-dependency exceptions aside). Otherwise the value would be ambiguous. Violating this is a standard SQL error (though MySQL historically allowed it).

  10. How does GROUP BY treat NULL values in the grouping columns?

    All NULLs in a grouping column are treated as a single group (NULLs are considered equal for grouping purposes), producing one group containing all rows with NULL in that column.

  11. What does the HAVING clause do, and how does it differ from WHERE?

    HAVING filters groups after aggregation (it can reference aggregate functions like SUM > 100), whereas WHERE filters individual rows before grouping and cannot reference aggregates. WHERE runs first, then GROUP BY, then HAVING.

  12. Can HAVING be used without GROUP BY? What happens?

    Yes. Without GROUP BY, the entire result set is treated as one group, so HAVING filters that single aggregate group — the query returns rows only if the whole-table aggregate condition is met.

  13. For performance, why is it preferable to filter with WHERE rather than HAVING when possible?

    WHERE eliminates rows before grouping/aggregation, reducing the number of rows the aggregation must process. HAVING filters after aggregation. Non-aggregate conditions should go in WHERE so fewer rows are grouped, improving efficiency.

  14. What do GROUPING SETS let you compute in a single query?

    GROUPING SETS specify multiple grouping combinations in one query, producing the union of several GROUP BY results. E.g. GROUP BY GROUPING SETS ((a,b),(a),()) yields groupings by (a,b), by a alone, and a grand total.

  15. What does ROLLUP generate, and for GROUP BY ROLLUP(a, b) which grouping sets are produced?

    ROLLUP produces hierarchical subtotals from most detailed to grand total. ROLLUP(a, b) generates the grouping sets (a,b), (a), and () — i.e. subtotals per (a,b), subtotals per a, and a grand total. For $n$ columns it yields $n+1$ sets.

  16. What does CUBE generate, and how many grouping sets does CUBE(a, b) produce?

    CUBE produces all possible combinations (the power set) of the grouping columns. CUBE(a, b) generates (a,b), (a), (b), and () — $2^{2}=4$ grouping sets. In general CUBE of $n$ columns yields $2^{n}$ grouping sets.

  17. What is the GROUPING() function used for with ROLLUP/CUBE?

    GROUPING(col) returns 1 if the column was aggregated away (rolled up) for that row and 0 otherwise. It distinguishes a genuine NULL value in the data from a NULL that represents a subtotal/grand-total row.

  18. What is a scalar subquery, and where can it be used?

    A scalar subquery returns exactly one row and one column (a single value). It can be used anywhere a scalar expression is allowed — in SELECT, WHERE, or as a value in comparisons, e.g. WHERE salary > (SELECT AVG(salary) FROM employees).

  19. What is a row subquery, and give an example comparison using one.

    A row subquery returns a single row with multiple columns, compared as a tuple. Example: WHERE (dept_id, salary) = (SELECT dept_id, MAX(salary) FROM ...). The comparison matches the ordered set of column values.

  20. What happens if a scalar subquery unexpectedly returns more than one row?

    It raises a runtime error (e.g. 'more than one row returned by a subquery used as an expression'). A scalar subquery must return at most one row; returning zero rows yields NULL.

  21. What is a correlated subquery, and how does its execution differ from an uncorrelated one?

    A correlated subquery references columns from the outer query, so it is (conceptually) re-evaluated once per outer row. An uncorrelated subquery is independent and can be evaluated once. Correlation makes it depend on the current outer row's values.

  22. Write a correlated subquery that selects employees earning more than their own department's average salary.

    SELECT e.* FROM employees e WHERE e.salary > (SELECT AVG(e2.salary) FROM employees e2 WHERE e2.dept_id = e.dept_id). The inner query references e.dept_id from the outer row, making it correlated.

  23. What does the EXISTS predicate do, and what does its subquery's SELECT list matter?

    EXISTS (subquery) returns TRUE if the subquery produces at least one row, else FALSE. It tests for existence only, so the SELECT list is irrelevant — conventionally SELECT 1 is used. It typically short-circuits on the first matching row.

  24. What does NOT EXISTS test, and why is it often preferred over NOT IN when NULLs are possible?

    NOT EXISTS returns TRUE if the subquery returns no rows (an anti-join). It is preferred over NOT IN because NOT IN with a NULL in the value list returns UNKNOWN for all comparisons, yielding no rows; NOT EXISTS handles NULLs correctly and gives the expected results.

What this deck covers

The Querying Data with SQL deck follows the SQL & Databases Querying Data with SQL syllabus — 6 chapters and 28 topics — so questions land on material that is genuinely examinable rather than trivia around it. That works out to roughly 8.5 cards per chapter.

Answers are written to be recallable, not just readable — averaging about 221 characters, which is long enough to carry the reasoning and short enough to say out loud.

A deck like this earns its keep on the second and third pass. Read the syllabus first so you know the shape of the subject, then use the cards to find the specific facts that have not stuck.

Querying Data with SQL flashcards FAQ

How many Querying Data with SQL flashcards are in this SQL & Databases deck?

51 cards. This page previews 24 of them, sampled evenly across the deck so you can judge the difficulty before installing anything.

Are these SQL & Databases flashcards free?

Yes. The preview here is free to read with no signup, and the full 51-card deck is free inside the Examius app.

What do the Querying Data with SQL cards cover?

They follow the SQL & Databases Querying Data with SQL syllabus — 6 chapters and 28 topics — so the questions track what is actually examinable.

How should I use these flashcards?

Read the syllabus first so you know the shape of the subject, then drill the deck. Examius schedules each card with spaced repetition, so cards you keep missing come back sooner and ones you know drift further apart.