- Position: Senior Software Engineer
- Interview Time: Nov 2024
- Company Type: Ride-hailing
- Company Name: Uber & Grab
1. What is the LIKE operator in SQL?
Question: What is the LIKE operator, and when should it be used?
Answer:
The LIKE operator in SQL is used to search for a specified pattern in a column. It is often used with wildcard characters like %
(for zero or more characters) and _
(for a single character).
%
represents zero or more characters._
represents a single character.
Example:
SELECT * FROM users WHERE name LIKE ‘John%’; This query will return all rows where thename
column starts with “John”. The LIKE operator is useful for simple pattern matching but can be inefficient for large datasets.
Question: What are the limitations of using LIKE for pattern matching?
Answer:
- Performance: LIKE can be slow, especially with large datasets, because it scans each row to check if the column matches the pattern.
- Limited Search Types: LIKE only supports basic pattern matching (e.g., starting, ending, or containing a string), making it less flexible than full-text search solutions.
2. What is Full-Text Search in SQL?
Question: What is Full-Text Search, and how does it work?
Answer:
Full-Text Search (FTS) is a feature that allows you to perform more advanced search queries on text columns. It is specifically optimized for searching large blocks of text and can handle complex queries like searching for multiple words, phrases, or individual word stems.
Full-Text Search is typically implemented using an inverted index, which makes searching more efficient than using LIKE. It supports features such as:
- Searching for exact or partial matches.
- Ranking results by relevance.
- Searching for word variations or stemming.
Question: When should you use Full-Text Search?
Answer:
You should use Full-Text Search when:
- You need to perform searches on large text data, like product descriptions, blog posts, or articles.
- You require advanced search capabilities, such as phrase searching, word stemming, or proximity searching.
- Performance is critical, especially for large datasets.
3. Comparing LIKE and Full-Text Search
Question: How does LIKE compare with Full-Text Search in terms of performance?
Answer:
- LIKE performs a linear scan of the data, which means it can be slow on large datasets, especially when searching for patterns at the beginning or middle of text.
- Full-Text Search uses an inverted index and other optimizations, making it much faster and more efficient when dealing with large amounts of textual data.
Question: Which method is more suitable for searching large datasets?
Answer:
For large datasets, Full-Text Search is the better choice due to its efficiency. It is specifically designed for searching large text blocks and offers advanced features that LIKE does not, such as ranking search results by relevance.
4. Syntax Differences Between LIKE and Full-Text Search
Question: What is the syntax for LIKE vs Full-Text Search?
Answer:
- LIKE Syntax: SELECT * FROM table WHERE column LIKE ‘pattern’;
- Full-Text Search Syntax (in MySQL, for example): SELECT * FROM table WHERE MATCH(column) AGAINST(‘search text’); In MySQL, the MATCH function is used to perform a Full-Text Search, and the AGAINST keyword specifies the text you want to search for.
5. Advanced Matching Techniques
Question: Can LIKE support more advanced search techniques?
Answer:
No, LIKE is limited to simple pattern matching using the %
and _
wildcard characters. It cannot handle more advanced search scenarios such as searching for word stems, phrases, or ranking results by relevance.
Question: What advanced features does Full-Text Search offer that LIKE does not?
Answer:
- Word Stemming: Full-Text Search can search for different variations of words (e.g., searching for “run” will also match “running”, “runner”).
- Phrase Search: Full-Text Search allows you to search for exact phrases, whereas LIKE only matches individual words.
- Relevance Ranking: Full-Text Search can rank search results based on how relevant they are to the search query, something LIKE cannot do.
6. When to Use LIKE vs Full-Text Search?
Question: When should I use LIKE over Full-Text Search?
Answer:
You should use LIKE when:
- The dataset is small or the query is simple.
- You are performing exact matches or matches with simple patterns.
- You do not need advanced search features like stemming or ranking.
Question: When is Full-Text Search the preferred option?
Answer:
Full-Text Search should be used when:
- You need to perform searches on large text datasets.
- Advanced search features like phrase matching, stemming, and relevance ranking are required.
- Performance is a concern, and you need more efficient searching.
7. Performance Tuning with Full-Text Search
Question: How can you improve performance with Full-Text Search?
Answer:
To optimize performance in Full-Text Search:
- Indexing: Ensure that the column being searched is indexed for full-text search.
- Query Optimization: Use
MATCH
andAGAINST
effectively to narrow down the search scope. - Stopwords: Configure stopwords (common words like “and”, “the”, “of”) to improve query performance and relevance.
Question: Can LIKE be optimized for better performance?
Answer:
LIKE queries can be optimized by:
- Using indexes on the column being searched, but this is only effective when the pattern does not start with
%
. - Avoiding leading wildcards (e.g.,
LIKE '%pattern%'
), as this makes the query slow.
8. Handling Special Characters in LIKE and Full-Text Search
Question: How do you handle special characters when using LIKE?
Answer:
When using LIKE, special characters like %
or _
need to be escaped if you want to match them literally. In SQL, you can escape these characters by using a backslash (\
), for example: SELECT * FROM table WHERE column LIKE ‘100%’;
Question: How does Full-Text Search handle special characters?
Answer:
Full-Text Search typically ignores special characters, but some systems (e.g., MySQL) may allow you to specify certain characters to be treated as delimiters or part of the search term.
9. Full-Text Search vs. LIKE in Different Database Systems
Question: How does the implementation of Full-Text Search differ across databases?
Answer:
The implementation of Full-Text Search varies across databases:
- MySQL: Uses the
MATCH
andAGAINST
syntax for Full-Text Search and has support for indexing text columns. - PostgreSQL: Uses the
tsvector
andtsquery
for Full-Text Search and is highly configurable with different dictionaries and text search configurations. - SQL Server: Uses Full-Text Indexing to enable searches on text-based columns, providing advanced features like word proximity searches.
10. Case Sensitivity in LIKE and Full-Text Search
Question: Is LIKE case-sensitive?
Answer:
By default, LIKE is case-insensitive in many SQL implementations like MySQL, but it depends on the collation settings. For case-sensitive searches, you can use the BINARY
keyword in MySQL: SELECT * FROM table WHERE BINARY column LIKE ‘Pattern’;
Question: Is Full-Text Search case-sensitive?
Answer:
In most databases, Full-Text Search is case-insensitive. However, it may depend on the specific database and configuration. For instance, MySQL Full-Text Search is case-insensitive by default, while PostgreSQL can be case-sensitive depending on its configuration.
For more detailed discussions on SQL and database performance, visit my blog.
LIKE vs Full-Text Search: SQL Performance and Use Cases