SQL Query: Selecting City Names Not Starting or Ending with Vowels

Dec 01, 2025 · Programming · 11 views · 7.8

Keywords: SQL | regular expression | query optimization

Abstract: This article delves into how to query city names from the STATION table in SQL, requiring names that either do not start with vowels (aeiou) or do not end with vowels, with duplicates removed. It primarily references the MySQL solution using regular expressions, including RLIKE and REGEXP, while supplementing with methods for other SQL dialects like MS SQL and Oracle, and explains the core logic of regex and common errors.

Introduction

In database querying, regular expressions are a powerful tool for string matching. This article focuses on a common problem: selecting city names from the STATION table where names either do not start with vowels (aeiou) or do not end with vowels, with results deduplicated. The user's initial query may fail due to regex syntax issues; we analyze this based on the best answer (Answer 1) and extract key insights.

Core Solution: Regular Expression Method in MySQL

Answer 1 provides two effective MySQL queries. The first uses RLIKE: SELECT DISTINCT city FROM station WHERE city RLIKE '^[^aeiouAEIOU].*|.*[^AEIOUaeiou]$';. Here, the regex '^[^aeiouAEIOU].*|.*[^AEIOUaeiou]$' means: match strings not starting with vowels (^[^aeiouAEIOU].*) or not ending with vowels (.*[^AEIOUaeiou]$). The symbol ^ denotes start of string, $ end, [^...] negated character set, and | logical OR.

The second approach uses REGEXP with NOT logic: SELECT DISTINCT city FROM station WHERE LOWER(city) NOT REGEXP '^[aeiou]' AND LOWER(city) NOT REGEXP '[aeiou]$';. This is more intuitive: first convert city names to lowercase to avoid case sensitivity, then use NOT REGEXP to exclude names starting or ending with vowels. Note that the original problem requires "either not start or not end," but this query uses AND, which might be incorrect; in practice, based on Answer 1 context, OR should be used for correct matching, as seen in Answer 3.

Detailed Regex Analysis and Common Errors

The user's original query select distinct city from station where city regexp '^[^aeiou].*[^aeiou]$'; fails because it requires strings to both not start and not end with vowels, conflicting with the "either-or" logic. Correct method uses the | operator to combine conditions. Also, case sensitivity matters; Answer 1 handles this by including both cases aeiouAEIOU or using LOWER() function.

Supplementally, Answer 3 offers a concise regex: SELECT DISTINCT city FROM station WHERE city REGEXP '^[^aeiouAEIOU]|[^aeiouAEIOU]$';, directly using | to separate conditions, avoiding redundant .* for efficiency.

Supplementary Solutions for Other SQL Dialects

Answer 2 provides a solution for MS SQL using LIKE operator: SELECT DISTINCT CITY FROM STATION WHERE CITY NOT LIKE '[AEIOU]%' OR CITY NOT LIKE '%[aeiou]';. Here, NOT LIKE with wildcard % achieves similar functionality. For Oracle, Answer 6 suggests regexp_like function: SELECT DISTINCT city FROM station WHERE regexp_like (city, '^[^aeiouAEIOU].*') OR regexp_like (city, '.*[^aeiouAEIOU]$');. These methods show syntax differences across databases but maintain core logic.

Answers 7 and 8 provide detailed condition lists based on LIKE, but code is verbose; Answer 5 uses left() and right() functions with REGEXP for MySQL-specific scenarios. These variants highlight query flexibility and readability.

Best Practices and Considerations

In practice, using regex is recommended for code conciseness and performance, but ensure correct logic: use OR operator for "either-or" conditions. Handle case sensitivity via functions like LOWER() or character set extensions. Use DISTINCT to deduplicate results. For different databases, choose appropriate regex functions (e.g., RLIKE/REGEXP for MySQL, regexp_like for Oracle).

Also, note escaping special characters: in content, code examples are escaped, e.g., text within <code> tags to avoid misinterpretation.

Conclusion

Through this analysis, we master the core method of using SQL regex to query city names not starting or ending with vowels. The MySQL solution from Answer 1 is efficient and standard, while other answers provide cross-database supplements. Key points include proper regex syntax, case handling, and operator choice. This aids developers in optimizing queries for similar string-matching tasks.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.