Dev Helpers

Developer journal

SQL

Query techniques for reporting, reshaping records and working with JSON in SQLite.

← All categories

SQL

Compare two literal lists

Set operations compare rows under SQL's rules for duplicates. Check whether the result represents membership or the number of occurrences.

Count jobs per active day

Grouping timestamps by day produces counts only for days present in the data. A continuous chart may need a calendar series as well.

Read the previous duration

LAG retrieves a prior row's value within an ordered window. The first row lacks a predecessor and usually needs its own display rule.

Match one character

A single-character wildcard expresses a different constraint from a variable-length wildcard. Decide whether punctuation in a search pattern is literal.

Alias a result column

An alias defines the name visible to query consumers. It can decouple an API field from a table's internal naming.

Count all jobs

COUNT(*) counts rows, while counting a nullable expression ignores null values. Choose the measure that matches the report.

Accumulate an incoming count

An upsert can accumulate a new count into an existing key. The uniqueness constraint determines which row receives the update.

Construct an array

A JSON array preserves element order and type. Inspect the distinction between a nested JSON value and a string containing JSON text.

Find gaps in identifiers

A gap between identifiers may reflect deletion or allocation history. Locate the gap before assigning it a business meaning.

Compare to a neighbouring job

Neighbour comparisons require a stable ordering. An ID tie or timestamp tie can change which record counts as the neighbour.

Validate an incoming JSON string

Invalid JSON can interrupt a query that expects structured input. Validate incoming text before passing it into extraction functions.

Use a default field value

A column default applies when an insert omits that column. Supplying an explicit NULL is a separate operation.

Sort numeric text as numbers

Text sorting places character sequences in lexical order. Cast numeric text deliberately when the ordering represents a quantity.

Expose a reusable view

A view gives a reusable query a stable name. Its output still reflects changes in the underlying tables.

Slice a JSON array into named pages

Integer array indexes can be divided into page buckets. Sort the elements within each aggregate to preserve the source sequence.

Count down to zero

A recursive query needs both an initial row and a terminating condition. Walk through the boundary to see whether zero is included.

Add a numeric adjustment

Arithmetic in a select list produces a derived value without changing the stored column. Give the result a name with clear units.

Check database integrity

An integrity check examines SQLite's internal structures. It answers a different question from whether the application's business rules hold.

Replace the middle coordinate

Replacing a JSON array element requires an existing path. A missing coordinate calls for a separate insert or set policy.

Select job names

Selecting named columns makes the result shape explicit. A later schema addition then leaves the consumer's expected fields intact.

Convert an empty field to null

An empty string and NULL communicate different conditions. NULLIF can normalize an empty import field where the schema expects a missing value.

Convert a date to Unix seconds

Unix seconds provide a compact comparison key for timestamps. The conversion still depends on a clear timezone convention for incoming text.

Lowercase a label

Lowercasing a label can simplify a display or comparison key. SQLite's built-in casing rules need particular care for non-ASCII text.

Filter on one status

A status filter is a useful baseline for a report. Explicitly decide how unknown or null statuses should behave.

Return unique queues

DISTINCT removes duplicate combinations across the entire selection. Adding another selected column can change what counts as unique.

Compare to the overall average

A comparison with the overall average uses two levels of aggregation or a scalar subquery. Define which records belong in the baseline.