Typorithm · medium

SAMPLE Clause

sample-clauseclickhousesampleapproximatesampling-key
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn SAMPLE Clause

0%

Querying

SAMPLE 0.1 reads about 10% of the data for fast approximate aggregates, but the table must declare a sampling key in its ORDER BY / SAMPLE BY.

SAMPLE n reads only a fraction of the data, trading exactness for a large speedup on big tables.

Because only part of the data is read, counts and sums must be scaled up by 1 / fraction to estimate the total.

SAMPLE only works when the table was created with a sampling key, declared via ORDER BY together with SAMPLE BY.

01

SAMPLE 0.1 reads ~10% of data.

02

Scale counts by 1 / fraction.

03

Needs a sampling key in ORDER BY / SAMPLE BY.

SELECT count() * 10 AS estimated_total
FROM events
SAMPLE 0.1;

Read the implementation

Connect each part of the code to the concept before you type.

1. Read 10%

SAMPLE 0.1 reads roughly 10% of the data for a fast approximate result.

SAMPLE 0.1

2. Scale up

Multiply by 1 / fraction to extrapolate the sampled count to the full table.

count() * 10

3. Sampled table

The table must declare a sampling key in ORDER BY / SAMPLE BY for SAMPLE to work.

FROM events

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

SAMPLE Clause: Lesson & Practice · Typorithm | ApplyDjinn