Querying Relational Data
-
Selecting Specific Columns in SQL
A select list defines which values each result row exposes, often with aliases describing computed or renamed fields.
-
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.
-
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.
-
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.
-
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.
-
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.
-
Grouping Rows by One Column
Grouping collects rows that share a key so an aggregate can describe each group's observations.
-
WHERE and HAVING at Different Stages
WHERE restricts individual inputs before grouping. HAVING restricts groups after their aggregate conditions are computed.
-
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.
-
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.
-
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.
-
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.
-
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.
-
Finding Parents Without a Related Row
A NOT EXISTS predicate keeps a parent when a related-row query returns no rows meeting its criteria.
-
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.
-
Combining SQL Results with UNION and UNION ALL
UNION ALL preserves rows from both inputs, including repeats. UNION removes duplicate combined result rows.
-
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.
-
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.
-
Replacing Null with COALESCE
COALESCE selects the first non-null argument. Use it when the replacement reflects the output's declared semantics.
-
Adding Group Information Without Collapsing Rows
A window function can compute a value across related rows while retaining each detail row in its result.
-
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.
-
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.
-
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.
-
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.
-
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.
-
Calculating a Rate in SQL
A rate computation should select a suitable denominator, handle empty groups, and use arithmetic supporting the requested precision.
-
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.
-
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.
-
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.
-
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.