Consider a table employees with columns id, name, and department_id. An index exists on department_id. What will be the output of the following query?
SELECT department_id, COUNT(*) AS total FROM employees GROUP BY department_id;
Assume the table has 3 departments with 5, 7, and 8 employees respectively.
SELECT department_id, COUNT(*) AS total FROM employees GROUP BY department_id;
Grouping counts employees per department. The index helps speed but does not change counts.
The query groups employees by department_id and counts them. The index on department_id speeds up grouping but does not affect the counts. The counts match the given numbers per department.
You have a table orders with columns order_id, customer_id, order_date, and status. Which index would best optimize this query?
SELECT * FROM orders WHERE customer_id = 123 AND status = 'shipped';
Think about which columns appear in the WHERE clause together.
The query filters by customer_id and status. An index on both columns in that order helps the database quickly find matching rows.
Which of the following is the correct syntax to create a composite index on columns last_name and first_name in MySQL?
Remember the order of clauses in the CREATE INDEX statement.
The correct syntax is CREATE INDEX index_name ON table_name (column1, column2);. Option D follows this exactly.
Given a table products with columns product_id, category_id, and price, which index will best optimize this query?
SELECT * FROM products WHERE category_id = 10 AND price BETWEEN 100 AND 200;
Consider the order of columns in the index for equality and range conditions.
For queries with equality on the first column and range on the second, the index should have the equality column first. So (category_id, price) is best.
A developer created an index on email column of the users table to speed up this query:
SELECT * FROM users WHERE email LIKE '%@example.com';
Why does the index not improve performance?
Think about how indexes work with LIKE patterns.
Indexes can be used efficiently only if the LIKE pattern does not start with a wildcard. Starting with '%' prevents the database from using the index to speed up the search.