Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    SQLSQL

    First questions to the data

    • What are SQL and databases?
    • SELECT: choosing columns
    • WHERE: filtering rows
    • ORDER BY and LIMIT: sort and take the top

    Summarizing and grouping

    • Aggregates: COUNT, SUM, AVG
    • GROUP BY and HAVING
    • CASE, COALESCE and text functions

    Joining tables

    • Keys and relationships
    • INNER JOIN
    • LEFT JOIN: leave nobody out
    • Three-table joins and self joins
    • Subqueries and CTEs

    Building tables and changing data

    • CREATE TABLE and constraints
    • INSERT, UPDATE and DELETE
    • Transactions: all or nothing
    • SQL injection: the most dangerous mistake

    Professional level

    • Indexes: why is my query slow?
    • Window functions: rankings and totals without losing rows
    • Designing tables: normalization
    • Project: analytics for a learning platform
    SQL

    Professional level · Lesson 17 of 20

    Indexes: why is my query slow?

    How the database searches, EXPLAIN, and when an index hurts.

    11 minutes1 games

    Without an index, to find Cairo students the database reads every row one by one (a full table scan). With 8 rows it doesn't matter; with 10 million the query takes seconds.

    An index is like the index at the back of a book: a sorted list of values, each pointing to its rows. The database jumps straight there instead of turning every page.

    • index on city (B-tree)
      • A … C
        • Alexandria → rows 2, 5
        • Cairo → rows 1, 3, 6
      • G … M
        • Giza → rows 4, 8
        • Mansoura → row 7

    Most indexes are B-trees: values are sorted, so a lookup takes few steps even on a huge table.

    EXPLAIN QUERY PLAN (in PostgreSQL: EXPLAIN) tells you what the database plans to do. Compare the plan before and after the index:

    SQL
    EXPLAIN QUERY PLAN
    SELECT name FROM students WHERE city = 'Cairo';
    
    CREATE INDEX idx_students_city ON students (city);
    
    EXPLAIN QUERY PLAN
    SELECT name FROM students WHERE city = 'Cairo';

    SCAN means it read the whole table; SEARCH … USING INDEX means it used the index. In PostgreSQL you'll see Seq Scan versus Index Scan.

    Important

    An index isn't free: every INSERT, UPDATE and DELETE must update the indexes too, and each takes space. Index columns you search by often (WHERE, JOIN, ORDER BY), not everything. Primary keys and UNIQUE create indexes on their own.

    Composite indexes and column order

    An index on (city, xp) is sorted by city first, then by XP within each city, like a phone book (surname, then first name). It helps WHERE city = 'Cairo' and WHERE city = 'Cairo' AND xp > 100, but not WHERE xp > 100 alone, just as a phone book can't help you search by first name only.

    SQL
    CREATE INDEX idx_city_xp ON students (city, xp);
    
    EXPLAIN QUERY PLAN
    SELECT name FROM students WHERE city = 'Cairo' AND xp > 100;
    
    EXPLAIN QUERY PLAN
    SELECT name FROM students WHERE xp > 100;

    Quick question

    You have an index on (city, xp). Which query will not benefit from it?

    Things that stop an index from being used: a function on the column (WHERE LOWER(email) = …), a LIKE '%abc' starting with %, or comparing different types. If you need the function, use an expression index: CREATE INDEX ON users (LOWER(email)).

    Interview question

    Why not index every column? How do you know a query needs an index?

    Learn more from

    • Use The Index, Luke: Anatomy of an indexuse-the-index-luke.com
    • Use The Index, Luke: Concatenated indexesuse-the-index-luke.com
    • PostgreSQL docs: Using EXPLAINpostgresql.org
    • SQLite: EXPLAIN QUERY PLANsqlite.org
    • SQLite: Query planner overviewsqlite.org

    Played 0 of 1 games

    Sign in to save your progress and earn XP.

    PreviousSQL injection: the most dangerous mistakeNextWindow functions: rankings and totals without losing rows
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.