Skip to content

SQL PRACTICE · SQLITE 3

SQL SELECT, WHERE and ORDER BY with a runnable dataset

Query a small student table, combine conditions and make the final row order deterministic.

Forsk Coding School learning resources · Updated 2026-10-26

What you will learn

  • Choose only the columns needed
  • Combine filters with AND
  • Specify a stable sort order, including ties

Before you start: A SQLite 3 environment. Run the shared setup script once in a fresh database before each independent example.

Load the same data before comparing results

The setup script below creates three related tables and inserts synthetic student records. These names and marks are teaching data, not claims about real Forsk students. Use a new empty database for the setup. Running it twice in the same database will correctly report that the tables already exist; start fresh rather than repeatedly inserting duplicate rows.

These exercises are tested with SQLite 3. They use a small SQL subset, but database products differ in details such as data types, foreign-key settings and date functions. Follow the stated dialect when reproducing an output. The shared setup enables foreign-key enforcement for the current SQLite connection.

Describe the result before writing the query

The question is: show the names and marks of Jaipur students who scored at least 80, highest mark first. SELECT names the output columns. FROM selects the source table. WHERE keeps rows that satisfy the condition. AND requires both the city and mark conditions to hold. This is a read operation; it does not change stored marks.

Text literals use single quotes. Column names such as city and score are identifiers, so they are not wrapped in single quotes. The comparison score >= 80 includes exactly 80. Writing > instead changes the boundary, even if the current dataset happens not to contain that value. Use small boundary examples to check the intended rule.

Never rely on the apparent storage order

Without ORDER BY, a query does not promise the order in which rows will appear. ORDER BY score DESC asks for the largest score first. Adding id ASC resolves ties between equal marks. A stable tie-breaker matters when you later paginate results or compare automated expected output.

SELECT * is convenient when first inspecting a table, but an application query is usually clearer when it requests explicit columns. That makes the output contract visible and avoids silently adding fields to a report when the schema grows. Before building joins, verify a one-table query and check that every returned row satisfies the original sentence.

Run the worked example

Show the shared dataset setup

Run this once in a fresh, empty SQLite database before running the example or exercise. Every name and mark is synthetic teaching data.

SQLite 3
PRAGMA foreign_keys = ON;
CREATE TABLE students (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    city TEXT,
    score INTEGER NOT NULL CHECK (score BETWEEN 0 AND 100),
    cohort TEXT NOT NULL
);
CREATE TABLE courses (id INTEGER PRIMARY KEY, title TEXT NOT NULL UNIQUE);
CREATE TABLE enrollments (
    student_id INTEGER NOT NULL REFERENCES students(id),
    course_id INTEGER NOT NULL REFERENCES courses(id),
    PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES
    (1, 'Asha', 'Jaipur', 82, 'Java'),
    (2, 'Aman', 'Ajmer', 66, 'Java'),
    (3, 'Jaya', 'Jaipur', 94, 'SQL'),
    (4, 'Kiran', NULL, 48, 'SQL'),
    (5, 'Neha', 'Kota', 82, 'SQL'),
    (6, 'Kabir', 'Jaipur', 40, 'Java');
INSERT INTO courses VALUES (10, 'Java'), (20, 'SQL'), (30, 'Python');
INSERT INTO enrollments VALUES (1, 10), (1, 20), (2, 10), (3, 20), (5, 20);

Download setup.sql

SQLite 3
SELECT name, score
FROM students
WHERE city = 'Jaipur' AND score >= 80
ORDER BY score DESC, id ASC;

Expected result

namescore
Jaya94
Asha82
  1. The city condition excludes Ajmer, Kota and the missing city.
  2. The score condition excludes Kabir even though his city is Jaipur.
  3. Jaya appears before Asha because 94 is greater than 82.

Common mistakes to check

  • A comparison with a text literal needs single quotes around the text.
  • Use AND when both requirements must hold.
  • Add ORDER BY whenever the row order matters to the answer.

Try it yourself

Return the names and marks of students scoring from 40 through 82 inclusive. Sort by score ascending and then id ascending.

Give me a hint

Use BETWEEN 40 AND 82, which includes both endpoints.

Compare with one working solution
SQLite 3
SELECT name, score FROM students
WHERE score BETWEEN 40 AND 82
ORDER BY score ASC, id ASC;

Expected result

namescore
Kabir40
Kiran48
Aman66
Asha82
Neha82

Quick check

Choose an answer, then check the explanation. Your answer stays in this page and is not saved as a student record.

Which clause guarantees a requested row order?

Keep learning

Explain the result in your own words, change one input and predict the new output. For additional structured assignments, explore the workbook sample before requesting the full pack.

Explore the practice workbook

Reference documentation

Use the official reference to check language details. The explanations, examples and exercises on this page are written for this learning resource.