SQL Indexes

Make queries faster.

✍️ 3 exercises + code challenge in this lesson

An index is a lookup structure that helps the database find rows quickly, like the index at the back of a book.

  • CREATE INDEX idx_city ON Customers (City);
  • CREATE UNIQUE INDEX also enforces uniqueness.
  • DROP INDEX idx_city;

Index columns you often use in WHERE, JOIN and ORDER BY. But every index slows down inserts and updates slightly, so do not index everything.

Key Takeaways: SQL Indexes

Mastering SQL Indexes helps you write clean and reliable SQL code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

SQL Indexes Code Example

SQL
CREATE INDEX idx_customers_city ON Customers (City);

EXPLAIN QUERY PLAN
SELECT * FROM Customers WHERE City = 'Lahore';

SELECT name, tbl_name FROM sqlite_master WHERE type = 'index';
Free sign-in required · Google login available

SQL Indexes Practice Exercises

Check what you learned in “SQL Indexes”.

/ 3 solved
Fill in the blank

Create an index on the City column.

 idx_city ON Customers (City);
✓ Correct! Not quite. Try again.
Multiple choice

What is the downside of having many indexes?

✓ Correct! Not quite. Try again.
Code challenge

Create an index idx_price on Products(Price), then list index names from sqlite_master where type = 'index' and tbl_name = 'Products'.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services