Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions exercises/concept/intro-select/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS weather_readings;
CREATE TABLE weather_readings (
date TEXT NOT NULL,
location TEXT NOT NULL,
temperature REAL NOT NULL,
humidity INTEGER NOT NULL
);

.mode csv
.import ./data.csv weather_readings
6 changes: 6 additions & 0 deletions exercises/concept/intro-select/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"2025-10-22","Portland",53.1,72
"2025-10-22","Seattle",56.2,66
"2025-10-22","Boise",60.4,55
"2025-10-23","Portland",54.6,70
"2025-10-23","Seattle",57.8,68
"2025-10-23","Boise",62.0,58
60 changes: 60 additions & 0 deletions exercises/concept/intro-select/evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from pathlib import Path
import json
import sys


def gather_results(filename):
text = Path(filename).read_text()
list_start_indices = find_all_occurrences('[', text)
list_end_indices = find_all_occurrences(']', text)
return [
json.loads(text[begin:end+1])
for begin, end in zip(list_start_indices, list_end_indices)
]


def find_all_occurrences(char, string):
return [i for i, c, in enumerate(string) if c == char]


def format_diff(expected, actual):
return (
f"\tExpected:\n"
f"\t\t{expected}\n"
f"\tActual:\n"
f"\t\t{actual}"
)

def report_results(all_test_data, actual_results):
results = []
for test_data, actual in zip(all_test_data, actual_results):
result = {'description': test_data['description']}
if test_data['expected'] == actual:
result['status'] = 'pass'
else:
result['status'] = 'fail'
result['message'] = "Expected: " + json.dumps(test_data['expected'])
result['output'] = "Actual: " + json.dumps(actual)
results.append(result)
return make_result_report(results)


def make_result_report(results):
status = 'pass' if all(r['status'] == 'pass' for r in results) else 'fail'
return {
"version": 3,
"status": status,
"tests": results
}


def main():
test_data_filename, user_output_filename = sys.argv[1:3]
test_data = json.loads(Path(test_data_filename).read_text())
actual_results = gather_results(user_output_filename)
results = report_results(test_data, actual_results)
Path('results.json').write_text(json.dumps(results, indent=2))


if __name__ == '__main__':
main()
14 changes: 14 additions & 0 deletions exercises/concept/intro-select/intro-select.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SELECT * FROM weather_readings;

SELECT location, temperature FROM weather_readings;

-- This one will fail on purpose
SELECT 'Hello, world.' AS say_hi;

SELECT * FROM weather_readings WHERE location = 'Seattle';

SELECT * FROM weather_readings WHERE humidity BETWEEN 60 AND 70;

SELECT location FROM weather_readings;

SELECT DISTINCT location FROM weather_readings;
11 changes: 11 additions & 0 deletions exercises/concept/intro-select/intro-select_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Create database:
.read ./create_fixture.sql

.mode json

-- Run user solution
.output user_output.txt
.read ./intro-select.sql

-- Compare expected vs actual
.shell python evaluate.py test_data.json user_output.txt
64 changes: 64 additions & 0 deletions exercises/concept/intro-select/test_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"description": "ALL records => SELECT * FROM weather_readings",
"expected": [
{"date":"2025-10-22","location":"Portland","temperature":53.1,"humidity":72},
{"date":"2025-10-22","location":"Seattle","temperature":56.2,"humidity":66},
{"date":"2025-10-22","location":"Boise","temperature":60.4,"humidity":55},
{"date":"2025-10-23","location":"Portland","temperature":54.6,"humidity":70},
{"date":"2025-10-23","location":"Seattle","temperature":57.79999999999999,"humidity":68},
Copy link
Author

@blackk-foxx blackk-foxx Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, this comes out as 57.79999999999 instead of the original 57.8 in the input data (see data.csv). Any ideas from the SQLite gurus on how to make it come out as 57.8?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Floating point numbers are hard for computers.

SQLite promises to preserve the 15 most significant digits of a floating point value.
ref

Numbers like 57.8 (or 0.8) cannot actually be handled very well by computers using the common representations. The original only looks like 57.8 when input or rounded. Float comparisons generally go better when you compare that the two values are close enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can bypass this problem by picking nicer values, eg fractions which can be represented as a / (2^b).

sqlite> .mode json
sqlite> SELECT 57.8;
[{"57.8":57.79999999999999716}]
sqlite> SELECT 57.5;
[{"57.5":57.5}]

{"date":"2025-10-23","location":"Boise","temperature":62.0,"humidity":58}
]
},
{
"description": "Just location and temperature columns => SELECT location, temperature FROM weather_readings",
"expected": [
{"location":"Portland","temperature":53.1},
{"location":"Seattle","temperature":56.2},
{"location":"Boise","temperature":60.4},
{"location":"Portland","temperature":54.6},
{"location":"Seattle","temperature":57.79999999999999},
{"location":"Boise","temperature":62.0}
]
},
{
"description": "Without \"FROM\" => SELECT 'Hello, world.'",
"expected": [
{"'Hello, world.'":"Hello, world."}
]
},
{
"description": "All records from Seatle location => SELECT * FROM weather_readings WHERE location = 'Seattle'",
"expected": [
{"date":"2025-10-22","location":"Seattle","temperature":56.2,"humidity":66},
{"date":"2025-10-23","location":"Seattle","temperature":57.79999999999999,"humidity":68}
]
},
{
"description": "All records where humidity in range => SELECT * FROM weather_readings WHERE humidity BETWEEN 60 AND 70",
"expected": [
{"date":"2025-10-22","location":"Seattle","temperature":56.2,"humidity":66},
{"date":"2025-10-23","location":"Portland","temperature":54.6,"humidity":70},
{"date":"2025-10-23","location":"Seattle","temperature":57.79999999999999,"humidity":68}
]
},
{
"description": "Just location column => SELECT location FROM weather_readings",
"expected": [
{"location":"Portland"},
{"location":"Seattle"},
{"location":"Boise"},
{"location":"Portland"},
{"location":"Seattle"},
{"location":"Boise"}
]
},
{
"description": "Only unique locations => SELECT DISTINCT location FROM weather_readings",
"expected": [
{"location":"Portland"},
{"location":"Seattle"},
{"location":"Boise"}
]
}
]