In a typical Employees table, which SQL query returns all employees whose name starts with the letter M?

Difficulty: Easy

Correct Answer: SELECT * FROM Employees WHERE name LIKE 'M%';

Explanation:


Introduction / Context:
This question checks whether you understand how pattern matching works in SQL using the LIKE operator. Filtering rows based on the first letter of a column, such as retrieving all employees whose names begin with M, is a very common requirement in business applications. Interviewers use this type of problem to confirm that you not only know basic SELECT syntax but also understand wildcards and how they affect the matching pattern.


Given Data / Assumptions:
• We assume there is a table named Employees (or similar) with a character column called name. • The requirement is to return all employees where the first character of name is the letter M. • We assume a standard SQL LIKE pattern matching syntax using the percent wildcard %. • Case sensitivity may depend on collation, but the logical pattern remains the same.


Concept / Approach:
The LIKE operator is used with wildcard characters to search for patterns in text columns. The percent symbol % represents any sequence of zero or more characters. When we write 'M%', we mean any string where M is the first character, followed by zero or more characters of any kind. By contrast, '%M' would match any string ending with M, and '%M%' would match any string that contains M anywhere. Understanding these differences is critical to writing correct filter conditions for real world queries.


Step-by-Step Solution:
Step 1: Identify the requirement: the name must start with the letter M. Step 2: Choose the proper wildcard pattern. The starting letter is fixed, so we use M followed by a wildcard. Step 3: Recall that % stands for any sequence of characters, so the pattern should be 'M%'. Step 4: Combine this pattern with the LIKE operator inside a WHERE clause: WHERE name LIKE 'M%'. Step 5: Wrap the filter in a full SELECT statement against the Employees table to complete the query.


Verification / Alternative check:
A simple way to verify is to imagine sample values. The pattern 'M%' will match names like 'Mohan', 'Meera', 'M', and 'Mohit Kumar', because they all start with M. It will not match 'Amit', 'Rahim', or 'Kumar', because those values have different first letters. If you tried '%M', only values ending with M, such as 'Ram', would match. The pattern '%M%' would match any value that contains M anywhere, for example 'Amit' or 'Meera', which is not what the question asks. That confirms that the correct query is the one using LIKE 'M%'.


Why Other Options Are Wrong:
Option b uses LIKE '%M', which selects values ending with M rather than starting with M. Option c tests equality name = 'M', which only finds names that are exactly one letter long and equal to M. Option d uses LIKE '%M%', which returns any name containing M in any position, not only at the start. Option e uses BETWEEN, which compares whole strings lexicographically and does not guarantee that names start exactly with M.


Common Pitfalls:
A frequent mistake is to confuse 'M%' with '%M%' and unintentionally broaden the search to any name that contains M. Another pitfall is to forget that LIKE patterns are string specific; using = with 'M' does not implement pattern matching at all. It is also important to understand that the percent wildcard can represent zero characters, meaning that even the single letter value 'M' would match 'M%'. Finally, when working with case sensitive collations, you may need to consider using upper or lower functions consistently to avoid missing values that start with m in a different case.


Final Answer:
The correct choice is SELECT * FROM Employees WHERE name LIKE 'M%';, because it uses the LIKE operator with the pattern 'M%' to return all rows where the name begins with the letter M.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion