k-Nearest Neighbors
DISTANCE · TOP-K · VOTEEncode rows, compute Euclidean distance, rank five neighbors and vote with GROUP BY.
Select Run pure SQL.
PURE SQL MACHINE LEARNING · NO PYTHON
Machine learning begins with representation, distance, probability, optimization and aggregation. SQL already has those building blocks. This lab opens the black box by rebuilding six model families from relational operations.
SIX LIVE FAMILIES
Press Run SQL to execute each complete model pipeline on the server. Results and the exact SQL are returned; source rows stay inside the engine.
Encode rows, compute Euclidean distance, rank five neighbors and vote with GROUP BY.
Select Run pure SQL.
Estimate priors and likelihoods with aggregates, apply Laplace smoothing and sum log probabilities.
Select Run pure SQL.
Fit slope, intercept and R² from SQL aggregate statistics and predict in the same statement.
Select Run pure SQL.
Use joins for point-centroid distance, a window rank for assignment and GROUP BY for new centroids.
Select Run pure SQL.
Self-join items within baskets and derive support, confidence and lift from grouped counts.
Select Run pure SQL.
Learn mean and standard deviation, score every observation and rank unusual rows.
Select Run pure SQL.
EMBEDDED EXCEL WORKSHEET
The 2022 workbook teaches the same dataset through probability and distance. Tabs below preserve that learning order; download the Excel file to inspect and modify the formulas.
| No. | Outlook | Temperature | Humidity | Windy | Play |
|---|---|---|---|---|---|
| 1 | sunny | hot | high | false | no |
| 2 | sunny | hot | high | true | no |
| 3 | overcast | hot | high | false | yes |
| 4 | rainy | mild | high | false | yes |
| 5 | rainy | cool | normal | false | yes |
| 6 | rainy | cool | normal | true | no |
| 7 | overcast | cool | normal | true | yes |
| 8 | sunny | mild | high | false | no |
| 9 | sunny | mild | normal | false | yes |
| 10 | rainy | mild | normal | false | yes |
| 11 | sunny | mild | normal | true | yes |
| 12 | overcast | mild | high | true | yes |
| 13 | overcast | hot | normal | false | yes |
| 14 | rainy | mild | high | true | no |
| Class | Prior | P(rainy|class) | P(cool|class) | P(high|class) | P(windy=true|class) | Original product |
|---|---|---|---|---|---|---|
| yes | 9/14 | 3/9 | 2/9 | 3/9 | 3/9 | 0.00823 |
| no | 5/14 | 2/5 | 1/5 | 4/5 | 3/5 | 0.03840 |
Visible multiplication makes conditional evidence easy to follow.
Adds class prior, Laplace smoothing and log scores so unseen values and underflow are handled explicitly.
| Neighbor | Distance | Class |
|---|---|---|
| 1 | 0.0361 | no |
| 8 | 0.1562 | no |
| 9 | 0.1838 | yes |
| 2 | 1.0026 | no |
| 11 | 1.0108 | yes |
Vote: no = 3, yes = 2 → prediction = no
HOW SQL BECOMES ML
Pure SQL does not mean every algorithm is equally elegant or scalable. It means the complete transformation from training rows to decision is expressed and executed inside the data engine.
RELATED LESSONS
Continue to Declarative ML for the wider ecosystem, MADlib for mature in-database algorithms, and DuckDB × ClickHouse for execution-boundary comparison.
NEXT PURE SQL LAB
Continue with seven SQL-only recommender methods: popularity, graph co-occurrence, collaborative filtering, content, time decay and hybrid ranking.