Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a hash index in a database?
A hash index is a type of database index that uses a hash function to map search keys to specific locations, allowing fast data retrieval based on exact matches.
Click to reveal answer
beginner
How does a hash function help in hash indexing?
A hash function converts a search key into a fixed-size value (hash code) that points to the location where the data is stored, enabling quick access without scanning the entire dataset.
Click to reveal answer
beginner
What type of queries are hash indexes best suited for?
Hash indexes are best for queries that look for exact matches, such as finding a record with a specific key value, but they are not efficient for range queries.
Click to reveal answer
intermediate
What is a limitation of hash indexes compared to B-tree indexes?
Hash indexes do not support range queries or ordered traversal of data, while B-tree indexes allow both exact match and range queries efficiently.
Click to reveal answer
intermediate
What happens if two keys produce the same hash value in a hash index?
This is called a collision. Hash indexes handle collisions using methods like chaining (linking entries) or open addressing (finding another slot), ensuring data can still be retrieved correctly.
Click to reveal answer
What is the main advantage of using a hash index?
ASupports full-text search
BEfficient range queries
COrdered data traversal
DFast exact match lookups
✗ Incorrect
Hash indexes provide very fast lookups for exact key matches but do not support range queries or ordered traversal.
Which of the following is a common problem in hash indexes?
ASlow insertions
BData duplication
CCollisions
DLack of indexing
✗ Incorrect
Collisions occur when two keys hash to the same location; hash indexes use special methods to handle collisions.
Hash indexes are NOT suitable for which type of query?
AExact match queries
BRange queries
CSingle record retrieval
DEquality searches
✗ Incorrect
Hash indexes do not support range queries efficiently because they do not maintain order.
Which data structure is commonly used as an alternative to hash indexes for range queries?
AB-tree
BStack
CLinked list
DQueue
✗ Incorrect
B-tree indexes maintain sorted order and support efficient range queries.
What does a hash function do in a hash index?
AMaps keys to storage locations
BSorts the data
CCompresses data
DEncrypts data
✗ Incorrect
A hash function converts keys into hash codes that point to where data is stored for quick access.
Explain how a hash index works and when it is most useful.
Think about how a key is transformed and used to find data quickly.
You got /4 concepts.
Compare hash indexes with B-tree indexes in terms of query types they support.
Consider what kinds of searches each index type handles best.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of a hash index in a database?
easy
A. To store data in sorted order
B. To speed up range queries
C. To compress data for storage
D. To speed up exact key lookups
Solution
Step 1: Understand the function of hash indexes
Hash indexes convert keys into hash values to quickly find exact matches.
Step 2: Compare with other index types
Unlike B-tree indexes, hash indexes do not support range queries or sorting.
Final Answer:
To speed up exact key lookups -> Option D
Quick Check:
Hash index = exact key lookup speed [OK]
Hint: Hash indexes are for exact matches, not ranges [OK]
Common Mistakes:
Thinking hash indexes support range queries
Confusing hash indexes with sorted indexes
Assuming hash indexes compress data
2. Which of the following is the correct syntax to create a hash index on a column named user_id in SQL (assuming the database supports hash indexes)?
easy
A. CREATE INDEX idx_user ON users USING HASH (user_id);
B. CREATE HASH INDEX idx_user ON users (user_id);
C. CREATE INDEX idx_user ON users (user_id) HASH;
D. CREATE INDEX idx_user HASH ON users (user_id);
Solution
Step 1: Recall standard SQL syntax for hash indexes
The correct syntax uses CREATE INDEX with USING HASH to specify the index type.
Step 2: Analyze each option
CREATE INDEX idx_user ON users USING HASH (user_id); correctly places USING HASH after the index name and before the column list.
Final Answer:
CREATE INDEX idx_user ON users USING HASH (user_id); -> Option A
Quick Check:
Syntax for hash index = CREATE INDEX ... USING HASH ... [OK]
Hint: Use 'USING HASH' after index name to specify hash index [OK]
Common Mistakes:
Placing HASH keyword incorrectly
Omitting USING keyword
Using non-standard syntax unsupported by SQL
3. Consider the following SQL query on a table with a hash index on email column:
SELECT * FROM users WHERE email = 'alice@example.com';
What is the expected behavior of the database when using the hash index?
medium
A. It performs a range scan on the email column
B. It scans the entire table because hash indexes do not support equality
C. It performs a fast exact match lookup using the hash index
D. It returns an error because hash indexes cannot be used in WHERE clauses
Solution
Step 1: Understand hash index usage in equality queries
Hash indexes are designed to quickly find rows matching an exact key value.
Step 2: Analyze the query condition
The WHERE clause uses equality on the indexed column, so the hash index is used efficiently.
Final Answer:
It performs a fast exact match lookup using the hash index -> Option C
Quick Check:
Equality query + hash index = fast lookup [OK]
Hint: Hash indexes speed up exact equality queries [OK]
Common Mistakes:
Thinking hash indexes do full table scans
Confusing hash index with range scan
Assuming hash indexes cause errors in queries
4. A developer created a hash index on the phone_number column but notices that queries with LIKE '%1234' are slow. What is the most likely reason?
medium
A. The hash index is corrupted and needs rebuilding
B. Hash indexes do not support pattern matching or partial searches
C. The database does not support hash indexes on numeric columns
D. The query optimizer ignores all indexes for LIKE queries
Solution
Step 1: Understand hash index limitations
Hash indexes only support exact key lookups, not pattern matching or partial searches.
Step 2: Analyze the query pattern
The LIKE '%1234' pattern searches for suffix matches, which hash indexes cannot optimize.
Final Answer:
Hash indexes do not support pattern matching or partial searches -> Option B
Quick Check:
Hash index ≠ pattern matching support [OK]
Hint: Hash indexes only speed exact matches, not LIKE patterns [OK]
Common Mistakes:
Assuming hash indexes speed up all LIKE queries
Blaming index corruption without evidence
Thinking numeric columns can't have hash indexes
5. You want to optimize a database table for fast lookups on a customer_id column, but also need to efficiently query ranges of order_date. Which indexing strategy is best?
hard
A. Create a hash index on customer_id and a B-tree index on order_date
B. Create hash indexes on both customer_id and order_date
C. Create a B-tree index on customer_id and no index on order_date
D. Create no indexes and rely on full table scans
Solution
Step 1: Match index types to query needs
Hash indexes are best for exact key lookups like on customer_id.
Step 2: Use B-tree indexes for range queries
B-tree indexes efficiently support range queries, so use it on order_date.
Final Answer:
Create a hash index on customer_id and a B-tree index on order_date -> Option A
Quick Check:
Hash for exact, B-tree for range queries [OK]
Hint: Use hash for exact keys, B-tree for ranges [OK]