🗄️ SQL for beginners · Class 12 · Lesson 11 of 18 · Free
SQL Indexes & Performance – Make Queries Find Rows Fast
⏱ About 35 minutes · ▶ run real SQL here — nothing to install · no sign-in
In this lesson you will
- Create an index with CREATE INDEX and remove it with DROP INDEX
- Read EXPLAIN QUERY PLAN and tell a SCAN from a SEARCH
- Choose good columns and column order for an index, and know its cost
A railway enquiry counter has 50,000 tickets for today. You give your PNR number. If the clerk checked every ticket one by one, you would wait a long time. Databases have the same problem, and the fix is the same one your textbook uses at the back: an index.
What is an index in SQL?
An index is an extra, sorted list of one column's values. Each value points to its row. It works like the index at the back of a book: look up the word, jump to the page.
EXPLAIN QUERY PLAN asks SQLite how it will run a query, without running it. Read the detail column. SCAN students means it reads every row. SEARCH … USING INDEX means it jumps straight to the matching rows.
The other columns (id, parent, notused) are SQLite's own bookkeeping. You can ignore them. Try it: change city to section in both places and run again.
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
┌────┬────────┬─────────┬───────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼───────────────┤ │ 2 │ 0 │ 216 │ SCAN students │ └────┴────────┴─────────┴───────────────┘ (1 row) Index idx_students_city created. ┌────┬────────┬─────────┬────────────────────────────────────────────────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼────────────────────────────────────────────────────────┤ │ 3 │ 0 │ 62 │ SEARCH students USING INDEX idx_students_city (city=?) │ └────┴────────┴─────────┴────────────────────────────────────────────────────────┘ (1 row)
How does an index make a SQL query faster?
With 8 students, a scan is instant. Real tables are bigger. The setup here makes 50,000 train tickets with a recursive CTE, like the ones in Lesson 3.
Without an index, finding one PNR means checking up to 50,000 rows. The index keeps PNRs sorted, so SQLite can halve the search again and again, like looking up a word in a dictionary. About 16 halvings are enough for 50,000 rows.
Try it: change 4612343 to another PNR that ends the same way, like 4500007, and run again.
📋 Tables in this example: tickets — tap to see how they are made
CREATE TABLE tickets (
pnr INTEGER NOT NULL,
passenger TEXT NOT NULL,
train TEXT NOT NULL
);
WITH RECURSIVE n(x) AS (
SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 50000
)
INSERT INTO tickets
SELECT 4500000 + x * 7,
'Passenger ' || x,
CASE x % 3 WHEN 0 THEN 'Pune Express' WHEN 1 THEN 'Nagpur Mail' ELSE 'Delhi Superfast' END
FROM n;┌─────────┐ │ tickets │ ├─────────┤ │ 50000 │ └─────────┘ (1 row) ┌────┬────────┬─────────┬──────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼──────────────┤ │ 2 │ 0 │ 216 │ SCAN tickets │ └────┴────────┴─────────┴──────────────┘ (1 row) Index idx_tickets_pnr created. ┌────┬────────┬─────────┬────────────────────────────────────────────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼────────────────────────────────────────────────────┤ │ 3 │ 0 │ 61 │ SEARCH tickets USING INDEX idx_tickets_pnr (pnr=?) │ └────┴────────┴─────────┴────────────────────────────────────────────────────┘ (1 row) ┌─────────┬─────────────────┬─────────────────┐ │ pnr │ passenger │ train │ ├─────────┼─────────────────┼─────────────────┤ │ 4612343 │ Passenger 16049 │ Delhi Superfast │ └─────────┴─────────────────┴─────────────────┘ (1 row)
💡 We do not print timings: they change from computer to computer. The plan (SCAN or SEARCH) is the same everywhere.
Which SQL queries can use an index?
An index helps when the query compares the plain column, like city = 'Pune' or marks > 90. It also helps ORDER BY on that column, because the index is already sorted.
It cannot help when you wrap the column in a function, like lower(city). The index stores 'Pune', not 'pune', so SQLite must read every row and work out lower() for each one.
Try it: change the last query to WHERE city > 'M' and run again. Ranges can use the index too.
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1); CREATE INDEX idx_students_city ON students(city);
┌────┬────────┬─────────┬────────────────────────────────────────────────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼────────────────────────────────────────────────────────┤ │ 3 │ 0 │ 62 │ SEARCH students USING INDEX idx_students_city (city=?) │ └────┴────────┴─────────┴────────────────────────────────────────────────────────┘ (1 row) ┌────┬────────┬─────────┬─────────────────────────────────────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼─────────────────────────────────────────────┤ │ 4 │ 0 │ 224 │ SCAN students USING INDEX idx_students_city │ └────┴────────┴─────────┴─────────────────────────────────────────────┘ (1 row) ┌────┬────────┬─────────┬───────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼───────────────┤ │ 2 │ 0 │ 216 │ SCAN students │ └────┴────────┴─────────┴───────────────┘ (1 row)
💡 The index idx_students_city is made in this example's setup.
What is a composite index and does column order matter?
A composite index covers two or more columns. It is sorted by the first column, then by the second inside each first value. Think of a phone book sorted by surname, then first name.
The marks table already has one: its primary key (student_id, subject_id) from Lesson 5 makes an automatic index. It helps when you search by student_id. It cannot help a search by subject_id alone, just as a phone book cannot find everyone called 'Riya' quickly.
So put the column you filter by most (often with =) first. Try it: swap the columns to (marks, subject_id) in CREATE INDEX and run again.
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
┌────┬────────┬─────────┬──────────────────────────────────────────────────────────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼──────────────────────────────────────────────────────────────────┤ │ 3 │ 0 │ 62 │ SEARCH marks USING INDEX sqlite_autoindex_marks_1 (student_id=?) │ └────┴────────┴─────────┴──────────────────────────────────────────────────────────────────┘ (1 row) ┌────┬────────┬─────────┬────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼────────────┤ │ 2 │ 0 │ 216 │ SCAN marks │ └────┴────────┴─────────┴────────────┘ (1 row) Index idx_marks_subject created. ┌────┬────────┬─────────┬───────────────────────────────────────────────────────────────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼───────────────────────────────────────────────────────────────────────┤ │ 3 │ 0 │ 51 │ SEARCH marks USING INDEX idx_marks_subject (subject_id=? AND marks>?) │ └────┴────────┴─────────┴───────────────────────────────────────────────────────────────────────┘ (1 row)
Do SQL indexes have a cost?
Yes. Every index is a second copy of some data, so it takes space. Every INSERT, UPDATE and DELETE must also update each index. A table with ten indexes is slower to change.
So index columns you search, join or sort on often, like ids and PNRs. Skip columns you rarely filter on, and tiny tables.
A UNIQUE index also blocks duplicates, like the UNIQUE constraint in Lesson 5. Indexes named sqlite_autoindex_… were made by SQLite for PRIMARY KEY and UNIQUE. Try it: remove the DROP INDEX line and run again.
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
Index idx_teachers_name created. Index idx_fees_month created. Index idx_fees_month dropped. ┌─────────────────────────────┬────────────┐ │ name │ table_name │ ├─────────────────────────────┼────────────┤ │ idx_teachers_name │ teachers │ │ sqlite_autoindex_marks_1 │ marks │ │ sqlite_autoindex_subjects_1 │ subjects │ └─────────────────────────────┴────────────┘ (3 rows)
🤔 Predict the output
Think like SQL: read the code and choose what it prints. You get the reason straight away.
Question 1. How many indexes does the database have now?
📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE INDEX idx_students_city ON students(city); SELECT COUNT(*) FROM sqlite_master WHERE type = 'index';
Question 2. There is an index on city. What does this return?
📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1); CREATE INDEX idx_students_city ON students(city);
SELECT COUNT(*) FROM students WHERE city = 'pune';
Question 3. Which indexes on students are left?
📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE INDEX idx_a ON students(class); CREATE INDEX idx_b ON students(section); DROP INDEX idx_a; SELECT name FROM sqlite_master WHERE type = 'index' AND tbl_name = 'students';
🧩 Your challenge
The fee office often runs: SELECT * FROM fees WHERE student_id = 2 AND month = '2026-07'. Check its plan, add a composite index called idx_fees_student_month that helps, and check the plan again. Expected: the first plan says SCAN fees, the second says SEARCH fees USING INDEX idx_fees_student_month (student_id=? AND month=?).
┌────┬────────┬─────────┬───────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼───────────┤ │ 2 │ 0 │ 216 │ SCAN fees │ └────┴────────┴─────────┴───────────┘ (1 row) Index idx_fees_student_month created. ┌────┬────────┬─────────┬───────────────────────────────────────────────────────────────────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼───────────────────────────────────────────────────────────────────────────┤ │ 3 │ 0 │ 62 │ SEARCH fees USING INDEX idx_fees_student_month (student_id=? AND month=?) │ └────┴────────┴─────────┴───────────────────────────────────────────────────────────────────────────┘ (1 row)
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
⚠️ Common mistakes (and how to fix them)
❌ Wrong
EXPLAIN QUERY PLAN SELECT name FROM students WHERE lower(city) = 'pune';
Runs, but gives the wrong result: ┌────┬────────┬─────────┬───────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼───────────────┤ │ 2 │ 0 │ 216 │ SCAN students │ └────┴────────┴─────────┴───────────────┘ (1 row)
✅ Right
EXPLAIN QUERY PLAN SELECT name FROM students WHERE city = 'Pune';
A function around the column hides it from the index, so SQLite scans every row. Compare the plain column (store data in one consistent case).
❌ Wrong
CREATE INDEX idx_marks ON marks(marks, subject_id); EXPLAIN QUERY PLAN SELECT * FROM marks WHERE subject_id = 1;
Runs, but gives the wrong result: Index idx_marks created. ┌────┬────────┬─────────┬────────────┐ │ id │ parent │ notused │ detail │ ├────┼────────┼─────────┼────────────┤ │ 2 │ 0 │ 216 │ SCAN marks │ └────┴────────┴─────────┴────────────┘ (1 row)
✅ Right
CREATE INDEX idx_marks ON marks(subject_id, marks); EXPLAIN QUERY PLAN SELECT * FROM marks WHERE subject_id = 1;
A composite index helps only when the query uses its FIRST column. Put the column you filter on first.
❌ Wrong
CREATE UNIQUE INDEX idx_students_city ON students(city);
Error in statement 1 (CREATE): UNIQUE constraint failed: students.city
✅ Right
CREATE INDEX idx_students_city ON students(city);
Many students live in Pune, so city is not unique and SQLite refuses. Use UNIQUE only for values that must never repeat, like a PNR or roll number.
📌 Remember
- An index is a sorted list of a column's values that points to rows.
- EXPLAIN QUERY PLAN shows SCAN (read every row) or SEARCH … USING INDEX (jump to rows).
- Indexes help =, ranges, ORDER BY and JOINs on the plain column, not on lower(column).
- In a composite index, the first column decides which queries it can help.
- Indexes speed up reading but cost space and slow down every write: add them where queries need them.
Finished the quiz and the challenge?
Questions students ask
Does a primary key automatically create an index?
Yes. PRIMARY KEY and UNIQUE columns get an index automatically. In SQLite, an INTEGER PRIMARY KEY is the row id itself, so it needs no extra index at all.
Should I add an index to every column?
No. Each index slows down INSERT, UPDATE and DELETE and uses space. Add indexes for columns you search, join or sort on often, and check the plan to see if they are used.
What is the difference between EXPLAIN in MySQL and SQLite?
MySQL and PostgreSQL use EXPLAIN SELECT …, and show tables with more columns, like estimated rows. SQLite's readable version is EXPLAIN QUERY PLAN. All of them answer the same question: scan or index?
Why is my query still slow with an index?
Check the plan first. The query may wrap the column in a function, use a composite index without its first column, or return most of the table anyway. Then a scan can be the right choice.