Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard with multiple values?

Tags:

sql

I get these Challenge on HackerRank

Write a query to print the list of CITY that start with vowels (a, e, i, o, u) in lexicographical order. Do not print duplicates.

My solution:

Select DISTINCT(City)
From Station
Where City like 'A%' 
   or City like 'E%' 
   or City like 'I%' 
   or City like 'O%' 
   or City like 'U%'
Order by City;

Other solution:

select distinct(city) from station
where upper(substr(city, 1,1)) in ('A','E','I','O','U');

This is very clever, but I want to know, if are there any other ways to solve this?

Any kind of DB is OK.

like image 584
Trung Avatar asked Nov 02 '25 12:11

Trung


1 Answers

Regular expressions in MySQL / MariaDB:

select distinct city from station where city regexp '^[aeiouAEIOU]' order by city
like image 182
Grzegorz Adam Kowalski Avatar answered Nov 04 '25 05:11

Grzegorz Adam Kowalski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!