Querying Relational Data

  1. Selecting Specific Columns in SQL

    A select list defines which values each result row exposes, often with aliases describing computed or renamed fields.

    Undergraduate
  2. Filtering Rows with WHERE

    A WHERE clause keeps rows whose condition evaluates to true. Good examples include cases on both sides and at the threshold.

    Undergraduate
  3. Ordering SQL Results with Two Fields

    An ORDER BY list uses its first key first and later keys to resolve ties. Supply a complete rule when consumers need stable order.

    Undergraduate
  4. Why LIMIT Needs a Meaningful Order

    A query limit bounds how many rows can be returned. The chosen order determines which subset a caller sees.

    Undergraduate
  5. Counting Rows and Counting Present Values

    Counting rows and counting values of a particular expression can give different totals because null values are excluded by a value count.

    Undergraduate
  6. An Empty Input and SQL SUM

    In PostgreSQL, SUM returns null when there are no non-null input values. A consumer can convert that result if its own domain treats absence as zero.

    Undergraduate
  7. Grouping Rows by One Column

    Grouping collects rows that share a key so an aggregate can describe each group's observations.

    Undergraduate
  8. WHERE and HAVING at Different Stages

    WHERE restricts individual inputs before grouping. HAVING restricts groups after their aggregate conditions are computed.

    Undergraduate
  9. Counting Children for Every Parent

    A report can keep parents without matching children by using an outer join and counting a present column identifying a matched child.

    Undergraduate
  10. An Inner Join as Matching Row Pairs

    An inner join returns every pair satisfying its join condition. The output multiplicity depends on how many matching rows exist.

    Undergraduate
  11. A Left Join Keeps Unmatched Left Rows

    A left join returns matching pairs plus a null-extended result for a left row with no right match.

    Undergraduate
  12. Right-Side Conditions in a Left Join

    A condition in ON affects which right rows are matched. A later WHERE condition can remove the already produced null-extended rows.

    Undergraduate
  13. Testing Whether a Related Row Exists

    EXISTS asks whether a subquery has at least one row. A Boolean existence test avoids duplicating outer results for multiple matches.

    Undergraduate
  14. Finding Parents Without a Related Row

    A NOT EXISTS predicate keeps a parent when a related-row query returns no rows meeting its criteria.

    Undergraduate
  15. Distinctness Depends on Selected Columns

    SELECT DISTINCT removes duplicate rows of the chosen result values. Changing which columns are selected can change which rows count as duplicates.

    Undergraduate
  16. Combining SQL Results with UNION and UNION ALL

    UNION ALL preserves rows from both inputs, including repeats. UNION removes duplicate combined result rows.

    Undergraduate
  17. Counting Distinct Values or Returning Groups

    A distinct count gives a number of different present values. Grouping can show each observed key and its own row count.

    Undergraduate
  18. Deriving a Category with SQL CASE

    A CASE expression chooses one result from branch conditions. Order and completeness determine how overlapping and unmatched rows are handled.

    Undergraduate
  19. Replacing Null with COALESCE

    COALESCE selects the first non-null argument. Use it when the replacement reflects the output's declared semantics.

    Undergraduate
  20. Adding Group Information Without Collapsing Rows

    A window function can compute a value across related rows while retaining each detail row in its result.

    Undergraduate
  21. Numbering Rows Within Groups

    A window row number ranks each row's position in its ordered partition. The ordering should determine ties when exact positions are important.

    Undergraduate
  22. A Running Total with an Ordered Window

    An ordered window can sum rows from a partition's start through the current row according to a specified frame.

    Undergraduate
  23. Naming an Intermediate Query with WITH

    A common table expression can name an intermediate result for later use in a query, making its role and granularity visible.

    Undergraduate
  24. Aggregate Before Joining When the Grain Requires It

    Aggregating one side before joining can align an input with the desired result grain and avoid repeated contributions.

    Undergraduate
  25. Filtering Text and Numeric Columns by Their Types

    Comparison uses a column's type and declared rules. A string reading number digits can produce a different order or match from a numeric field.

    Undergraduate
  26. Calculating a Rate in SQL

    A rate computation should select a suitable denominator, handle empty groups, and use arithmetic supporting the requested precision.

    Undergraduate
  27. Grouping by a Calendar Day in a Time Zone

    A daily report needs a calendar zone when stored observations are instants. The same instant can fall on different dates in different zones.

    Undergraduate
  28. Querying the Next Page After a Composite Cursor

    A query can continue beyond the last displayed pair of sort keys, under the same order and filter as earlier pages.

    Undergraduate
  29. Verifying a Data Migration with Queries

    Before and after counts and representative comparisons can show whether a migration preserved identities, relationships, and expected new constraints.

    Undergraduate
  30. Checking a Query Against a Known Small Data Set

    A small fixture with controlled duplicates and missing relationships can expose incorrect filters, ordering, or grouping.

    Undergraduate