Consider a table employees with 1 million rows. It has a column department_id. Which query will run faster if there is an index on department_id?
Assume department_id has many repeated values.
SELECT * FROM employees WHERE department_id = 5;
Think about how indexes help find data without scanning the whole table.
An index on department_id allows the database to quickly locate rows matching the condition without scanning all rows. This speeds up the query significantly.
Which index type is best suited for a column that stores unique user IDs and is often used in equality searches?
Think about which index type supports equality searches well.
B-tree indexes are the default and best for equality and range queries. Hash indexes only support equality but are less commonly used. GIN is for full-text or array data. BRIN is for large tables with correlated data.
Which SQL command correctly creates a composite index on columns last_name and first_name in the customers table?
Composite indexes list columns separated by commas inside parentheses.
Option D correctly creates a composite index on last_name and first_name. Option D reverses the order, which changes index usage. Option D creates two separate indexes, not a composite one. Option D uses invalid syntax.
What is a common downside of having many indexes on a table when performing frequent INSERT operations?
Think about what happens to indexes when new data is added.
Each index must be updated when a new row is inserted, which adds overhead and slows down INSERT operations.
You created an index on the email column of the users table. However, the query below does not use the index:
SELECT * FROM users WHERE LOWER(email) = 'test@example.com';
Why does this happen?
Think about how functions on columns affect index usage.
Applying a function like LOWER() on a column prevents the use of a normal index. To fix this, create an index on the expression LOWER(email).