Sample questions
Question 1Easy
Which statement creates an index on the name column of employees?
INDEX employees(name);ADD INDEX name TO employees;CREATE INDEX idx_emp_name ON employees (name);ALTER INDEX employees ADD name;
Answer: C. CREATE INDEX idx_emp_name ON employees (name);
Explanation
The standard form is CREATE INDEX index_name ON table_name (column, ...).
Step-by-step solution
- The standard form is CREATE INDEX index_name ON table_name (column).
- Here the index name is idx_emp_name, on employees (name).
- That is option C.
Why the other options are wrong
- A. INDEX alone without CREATE is not valid SQL for creating an index.
- B. ADD INDEX ... TO is not standard SQL for creating an index.
- D. ALTER INDEX does not create a new index on a column in this form.
Tip: CREATE INDEX name ON table (column) is the standard form.
Question 2Easy
What is the main trade-off of adding an index to a table?
- None; indexes only ever improve performance
- Less storage used, but slower reads and joins
- Faster writes, but slower reads and more locking
- Faster reads, but slower writes and extra storage
Answer: D. Faster reads, but slower writes and extra storage
Explanation
Indexes speed up lookups, joins and sorting, but every INSERT, UPDATE and DELETE must also maintain them, and they take disk space.
Step-by-step solution
- An index speeds up reads by allowing lookups and sorts without a full scan.
- Every insert, update or delete must also update the index and it uses storage.
- So the trade-off is faster reads for slower writes and extra storage (option D).
Why the other options are wrong
- A. Indexes have costs in writes and storage, so they do not only improve performance.
- B. Indexes use more storage, and they usually speed up reads rather than slow them.
- C. This reverses the trade-off: indexes speed reads and slow writes.
Tip: Index every column you filter or join on often, but not every column.
Question 3Medium
Which constraints usually create an index automatically?
- CHECK and DEFAULT
- FOREIGN KEY in every database
- NOT NULL
- PRIMARY KEY and UNIQUE
Answer: D. PRIMARY KEY and UNIQUE
Explanation
Databases build an index to enforce uniqueness efficiently. Foreign keys are indexed automatically only in some systems (MySQL InnoDB does, PostgreSQL does not).
Step-by-step solution
- Uniqueness is enforced with an index.
- PRIMARY KEY and UNIQUE therefore get indexes.
- FOREIGN KEY indexing varies by database.
Why the other options are wrong
- A. These create no index.
- B. Only some databases (e.g. MySQL InnoDB) index them.
- C. NOT NULL needs no index.
Question 4Medium
There is a composite B-tree index on (last_name, first_name). Which WHERE clause can use it least effectively?
- WHERE first_name = 'Anu'
- WHERE last_name = 'Rao'
- WHERE last_name LIKE 'Ra%'
- WHERE last_name = 'Rao' AND first_name = 'Anu'
Answer: A. WHERE first_name = 'Anu'
Explanation
A composite index is sorted by its leftmost column first. Filtering only on the second column cannot seek into the index (the leftmost-prefix rule).
Step-by-step solution
- A composite B-tree index is sorted by last_name first, then first_name.
- A filter on first_name alone cannot seek into that sorted order (the leftmost-prefix rule).
- So WHERE first_name = 'Anu' uses the index least effectively (option A).
Why the other options are wrong
- B. Last_name is the leftmost column, so this can seek into the index.
- C. A prefix LIKE on the leftmost column can use the index range.
- D. Both columns match the index order, so the index is used fully.
Tip: A composite index helps only when the filter starts from its leftmost column.
Question 5Medium
In SQL Server or MySQL InnoDB, how many clustered indexes can a table have?
- One per column
- One
- Two
- Unlimited
Answer: B. One
Explanation
A clustered index defines the physical order in which rows are stored, so there can be only one. A table can have many non-clustered (secondary) indexes.
Step-by-step solution
- A clustered index sets the physical order of the table's rows.
- A table's rows can be stored in only one physical order.
- So a table can have one clustered index (option B).
Why the other options are wrong
- A. Clustered indexes are per table, not per column.
- C. Two physical orders cannot exist at once for one table.
- D. The number of clustered indexes is limited to one.
Tip: One clustered index per table; many non-clustered indexes are allowed.
Question 6Hard
A query uses WHERE LOWER(email) = 'a@x.com' and there is a plain index on email. What is the usual fix to make it use an index?
- Add ORDER BY email to the query
- Rebuild the existing email index
- Use HAVING instead of WHERE
- Create an index on LOWER(email)
Answer: D. Create an index on LOWER(email)
Explanation
Wrapping the column in a function means the plain index on email does not match. An expression (function-based) index on LOWER(email) does.
Step-by-step solution
- WHERE LOWER(email) wraps the column in a function.
- A plain index on email stores the raw values, so it does not match LOWER(email).
- An index on LOWER(email) matches the expression (option D).
Why the other options are wrong
- A. ORDER BY does not make a function on the column match an index.
- B. Rebuilding the plain index does not change the expression, so it still cannot match.
- C. HAVING without GROUP BY does not help; the filter is still on the function.
Tip: The indexed expression must match the expression in WHERE; index LOWER(email) for LOWER(email) lookups.
Question 7Hard
There is a B-tree index on customers(name). Which condition generally cannot use it for an index seek?
- name = 'Asha'
- name LIKE 'Ash%'
- name > 'M'
- name LIKE '%sha'
Answer: D. name LIKE '%sha'
Explanation
A B-tree is ordered from the first character. A pattern with a leading wildcard has no fixed prefix to search for, so the database falls back to scanning.
Step-by-step solution
- A B-tree index is sorted by the start of the value.
- Equality, prefix LIKE and range can seek.
- A leading wildcard has no fixed start, so it scans.
Why the other options are wrong
- A. Equality uses the index.
- B. Prefix LIKE 'Ash%' is a range seek.
- C. Range comparisons use the index.
Tip: Leading % kills the index.
Practise all 7 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →