0
0
SQLquery~10 mins

Covering index concept in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Covering index concept
Query issued
Check index for needed columns
Index has all columns?
NoFetch missing columns from table
Yes
Return data directly from index
Query result delivered
When a query runs, the database checks if an index contains all columns needed. If yes, it uses the index alone to answer, skipping the main table.
Execution Sample
SQL
CREATE INDEX idx_name ON employees(last_name, first_name);
SELECT last_name, first_name FROM employees WHERE last_name = 'Smith';
This query uses an index on last_name and first_name to get data without reading the full table.
Execution Table
StepActionIndex Columns CheckedTable AccessedResult
1Query starts-NoWaiting for plan
2Check index idx_namelast_name, first_nameNoIndex covers needed columns
3Use index to find last_name='Smith'last_name, first_nameNoFound matching rows in index
4Return last_name, first_name from indexlast_name, first_nameNoData returned without table access
5Query ends-NoResult delivered from index
💡 Index contains all requested columns, so no table access needed; query completes.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Index UsedNoYesYesYesYes
Table AccessedNoNoNoNoNo
Rows Found00MultipleMultipleMultiple
Key Moments - 2 Insights
Why does the query not access the main table?
Because the index contains all columns needed by the query, as shown in execution_table step 2 and 4, so the database can answer directly from the index.
What happens if the index does not have all requested columns?
The database must access the main table to get missing columns, which is slower. This is shown by the 'No' branch in the concept_flow diagram.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the database confirm the index covers all needed columns?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Index Columns Checked' and 'Result' columns in execution_table row for Step 2.
According to variable_tracker, does the table get accessed during query execution?
AYes, after Step 3
BYes, after Step 4
CNo, table is never accessed
DOnly at the start
💡 Hint
Look at 'Table Accessed' row across all steps in variable_tracker.
If the query requested an additional column not in the index, what would change in the execution_table?
AStep 3 would access the table to fetch missing columns
BStep 4 would return data only from index
CStep 2 would say index covers all columns
DQuery would end at Step 1
💡 Hint
Refer to concept_flow where missing columns cause table access after index check.
Concept Snapshot
Covering index means an index has all columns a query needs.
If yes, database reads only the index, skipping the table.
This speeds up queries by reducing data reads.
Create indexes with all needed columns for frequent queries.
If missing columns, table access slows query down.
Full Transcript
When a database query runs, it first checks if an index contains all the columns the query needs. If the index covers all columns, the database can return results directly from the index without reading the main table. This is called a covering index. For example, if an index has last_name and first_name, and the query requests only those columns, the database uses the index alone. This speeds up the query because reading the index is faster than reading the full table. If the index lacks some columns, the database must access the table to get missing data, which is slower. The execution table shows each step: starting the query, checking the index, using the index to find rows, returning data from the index, and finishing the query. The variable tracker shows that the table is never accessed because the index covers all needed columns. Understanding covering indexes helps write faster queries by designing indexes that include all columns a query needs.