SQL chapters

Chapter 2 of 17

Create a repeatable SQLite exercise

Try this in our SQLite Compiler →

The idea

An exercise should create its own tables and rows so it can run in a fresh database. DROP TABLE IF EXISTS is useful only for disposable practice data, never an instruction to erase production tables. Run the complete script, not just its last query.

Worked example

DROP TABLE IF EXISTS students;
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT NOT NULL, score INTEGER);
INSERT INTO students VALUES (1, 'Asha', 80), (2, 'Ravi', 60);
SELECT name, score FROM students ORDER BY id;

What to check

The rows are Asha/80 and Ravi/60. Explicit ORDER BY makes their presentation deterministic.

Practice task

Add a third student and rerun the full script. Verify that there are exactly three rows, not duplicates from previous inserts.

Check your understanding

Explain the result in your own words before changing the example. Change one thing at a time, compare the result with your prediction, and keep the version that demonstrates the concept clearly.

Try it yourself

Output
Run your project to see output here.
Ctrl/Cmd+Enter runs · Ctrl/Cmd+S downloads the project · Local auto-save is always on