Bird
Raised Fist0
MySQLquery~20 mins

Creating indexes in MySQL - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Effect of a UNIQUE index on query results

Consider a table users with columns id (primary key), email, and name. A UNIQUE index is created on the email column.

What will happen if you try to insert two rows with the same email value?

MySQL
CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255), name VARCHAR(255));
CREATE UNIQUE INDEX idx_email ON users(email);

INSERT INTO users VALUES (1, 'alice@example.com', 'Alice');
INSERT INTO users VALUES (2, 'alice@example.com', 'Alicia');
AThe second insert will fail with a duplicate key error.
BThe second insert will be ignored silently.
CThe second insert will overwrite the first row.
DBoth inserts will succeed, allowing duplicate emails.
Attempts:
2 left
💡 Hint

Think about what a UNIQUE index enforces on the column values.

❓ query_result
intermediate
2:00remaining
Query performance with and without an index

Given a table orders with columns order_id, customer_id, and order_date, you run this query:

SELECT * FROM orders WHERE customer_id = 12345;

Which index will most improve the speed of this query?

ACREATE INDEX idx_order_date ON orders(order_date);
BCREATE UNIQUE INDEX idx_order_id ON orders(order_id);
CCREATE INDEX idx_customer ON orders(customer_id);
DCREATE FULLTEXT INDEX idx_customer ON orders(customer_id);
Attempts:
2 left
💡 Hint

Which column is used in the WHERE clause?

📝 Syntax
advanced
2:00remaining
Correct syntax to create a composite index

Which of the following is the correct syntax to create a composite index on columns last_name and first_name in MySQL?

ACREATE INDEX idx_name ON users(last_name first_name);
BCREATE INDEX idx_name ON users(last_name, first_name);
CCREATE INDEX idx_name ON users(last_name AND first_name);
DCREATE INDEX idx_name ON users(last_name + first_name);
Attempts:
2 left
💡 Hint

Think about how multiple columns are listed in an index.

❓ optimization
advanced
2:00remaining
Choosing the best index for a query with multiple conditions

Given a table products with columns category, price, and stock, you run this query:

SELECT * FROM products WHERE category = 'Books' AND price < 20;

Which index will most improve this query's performance?

ACREATE INDEX idx_category_price ON products(category, price);
BCREATE INDEX idx_price_category ON products(price, category);
CCREATE INDEX idx_category ON products(category);
DCREATE INDEX idx_price ON products(price);
Attempts:
2 left
💡 Hint

Consider the order of columns in a composite index and how the query filters.

🔧 Debug
expert
2:00remaining
Why does this index creation fail?

Consider this SQL statement:

CREATE INDEX idx_description ON products(description(10));

It fails with an error. What is the most likely reason?

APrefix length can only be used with UNIQUE indexes.
BThe index name idx_description is already used by another index.
CMySQL requires a prefix length for indexing TEXT or BLOB columns, but the syntax is incorrect here.
DThe column description is not a string type and cannot have a prefix length.
Attempts:
2 left
💡 Hint

Think about indexing long text columns and how prefix lengths work.

Practice

(1/5)
1. What is the main purpose of creating an index on a column in a MySQL table?
easy
A. To speed up data retrieval when searching or filtering
B. To store duplicate values in the column
C. To increase the size of the database
D. To automatically backup the table data

Solution

  1. Step 1: Understand what an index does

    An index acts like a shortcut to find data faster in a table.
  2. Step 2: Identify the main benefit

    Indexes speed up searches and filtering by avoiding full table scans.
  3. Final Answer:

    To speed up data retrieval when searching or filtering -> Option A
  4. Quick Check:

    Index = faster search [OK]
Hint: Indexes speed up searches by creating shortcuts [OK]
Common Mistakes:
  • Thinking indexes store duplicate values
  • Believing indexes increase database size significantly
  • Confusing indexes with backups
2. Which of the following is the correct syntax to create a simple index on the column email in a table named users?
easy
A. INDEX CREATE email_index ON users (email);
B. CREATE INDEX email_index ON users (email);
C. CREATE INDEX users.email_index (email);
D. CREATE INDEX ON users email_index (email);

Solution

  1. Step 1: Recall MySQL index creation syntax

    The correct syntax is: CREATE INDEX index_name ON table_name (column_name);
  2. Step 2: Match the syntax with options

    CREATE INDEX email_index ON users (email); matches the correct syntax exactly.
  3. Final Answer:

    CREATE INDEX email_index ON users (email); -> Option B
  4. Quick Check:

    CREATE INDEX index_name ON table (column) [OK]
Hint: Syntax: CREATE INDEX name ON table (column) [OK]
Common Mistakes:
  • Swapping order of table and index name
  • Omitting ON keyword
  • Using wrong keyword order
3. Given the table products with columns id, name, and category, what will be the effect of this query?
CREATE UNIQUE INDEX idx_name ON products (name);
medium
A. It allows duplicate product names in the table
B. It creates a backup of the products table
C. It deletes rows with duplicate names
D. It prevents duplicate product names and speeds up searches by name

Solution

  1. Step 1: Understand UNIQUE index effect

    A UNIQUE index prevents duplicate values in the indexed column.
  2. Step 2: Identify additional benefit

    Indexes also speed up searches on that column.
  3. Final Answer:

    It prevents duplicate product names and speeds up searches by name -> Option D
  4. Quick Check:

    UNIQUE index = no duplicates + faster search [OK]
Hint: UNIQUE index blocks duplicates and speeds search [OK]
Common Mistakes:
  • Thinking UNIQUE index allows duplicates
  • Confusing index with backup or delete
  • Ignoring speed improvement
4. You try to create an index with this command:
CREATE INDEX idx_age ON employees age;

What is the error in this statement?
medium
A. Table name is missing
B. Index name cannot start with 'idx_'
C. Missing parentheses around the column name
D. Cannot create index on numeric columns

Solution

  1. Step 1: Check syntax for CREATE INDEX

    The column list must be inside parentheses after the table name.
  2. Step 2: Identify the error

    The statement lacks parentheses around age, causing syntax error.
  3. Final Answer:

    Missing parentheses around the column name -> Option C
  4. Quick Check:

    Columns must be in parentheses [OK]
Hint: Always put columns in parentheses after table name [OK]
Common Mistakes:
  • Forgetting parentheses around columns
  • Thinking index names have restrictions
  • Assuming numeric columns can't be indexed
5. You have a large orders table with columns order_id, customer_id, and order_date. You often query orders by customer_id and order_date. Which index creation will best improve query speed without allowing duplicate entries for the same customer on the same date?
hard
A. CREATE UNIQUE INDEX idx_customer_date ON orders (customer_id, order_date);
B. CREATE INDEX idx_customer ON orders (customer_id);
C. CREATE INDEX idx_order_date ON orders (order_date);
D. CREATE UNIQUE INDEX idx_order_id ON orders (order_id);

Solution

  1. Step 1: Analyze query needs

    Queries filter by both customer_id and order_date, so a composite index on both is best.
  2. Step 2: Consider uniqueness requirement

    To prevent duplicate orders for the same customer on the same date, use a UNIQUE index on both columns.
  3. Final Answer:

    CREATE UNIQUE INDEX idx_customer_date ON orders (customer_id, order_date); -> Option A
  4. Quick Check:

    Composite UNIQUE index matches query and uniqueness [OK]
Hint: Use composite UNIQUE index for multi-column uniqueness and speed [OK]
Common Mistakes:
  • Creating single-column indexes only
  • Ignoring uniqueness requirement
  • Indexing unrelated columns
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard