9

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION.

My answer/tried code is:

select city from station where REGEXP_LIKE(city,'[^aeiou]+');

But it doesn't seem to be correct.

Kindly help me with this.

Goran Kutlaca
  • 2,014
  • 1
  • 12
  • 18
Chaitanya Chawla
  • 91
  • 1
  • 1
  • 3
  • 8
    `[^aeiou]+` - This means "everything that is not a, e, i, o, u, repeated 1 or more times" ([demo](https://regexr.com/4998m)). Likely you meant `^[aeiou].+`, which means "a vowel at the beginning of the string, followed by whatever character repeated 1 or more times" ([demo](https://regexr.com/4998p)). – BackSlash Feb 28 '19 at 07:15
  • @BackSlash, your Regex is good but Chaitanya will have to add parameter `'i'` for his query. It won't accept city name starting with Capital letters. `i` means case insensitive. `select city from station where REGEXP_LIKE(city,'^[aeiou].+','i');` – Deep Feb 28 '19 at 15:19

18 Answers18

17

use SUBSTR

select t.city from station t where lower(SUBSTR(city,1,1)) in ('a','e','i','o','u')
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
  • 3
    Please add some description on your answer. – Sagar Chauhan Feb 28 '19 at 07:34
  • 2
    @SagarChauhan i think it is enough just to say about substring – Zaynul Abadin Tuhin Feb 28 '19 at 07:41
  • @ZaynulAbadinTuhin 1. In Oracle there is no SUBSTRING function, it is SUBSTR. 2. If There is just an Alphabet from A, E, I ,O or U it will accept. Where it should not. It should be more than 1 Character. – Deep Feb 28 '19 at 15:38
  • @Deep thanks edited hope you will remove or downvote – Zaynul Abadin Tuhin Feb 28 '19 at 15:39
  • Sure, I will remove but your query fails my second condition. Let's just use an character E, it will also be selected. So it has to be at least two characters. – Deep Feb 28 '19 at 15:42
  • @Deep you may be not noticed that 1st i cut the 1st chracter then i converted in lower case so there is no chance it will be A or E it will convert into a,e , so you can upvote me now my answer :) – Zaynul Abadin Tuhin Feb 28 '19 at 15:47
  • You did not get my point, let's assume that you are getting (A or a) or (I or i) as data in that field, your query will select that data too. Where it should not. For eg.`with station as (SELECT 'Ahmedabad' city from dual UNION SELECT 'Baroda' city from dual UNION SELECT 'E' city from dual) select t.city from station t where lower(SUBSTR(city,1,1)) in ('a','e','i','o','u')`. This will select `E` also. Where using Regex what BackSlash answered in Comment to section will work. – Deep Feb 28 '19 at 16:27
9

As BackSlash have already commented, you've written the wrong REGEXP_LIKE pattern and you should change it to '^[aeiou].+', or you can even ommit .+ from your pattern, as you're only interested in the first letter of your string (containing more than 1 character):

select city from station where REGEXP_LIKE(city,'^[aeiou]');

Example with test data

Beware that would only return stations that start with lowercase vowels! If you also want to include uppercase vowels than add them to your pattern:

select city from station where REGEXP_LIKE(city,'^[aeiouAEIOU]');

or specify inside REGEXP_LIKE call that inputted pattern is case-insensitive with an 'i' flag, like this:

select city from station where REGEXP_LIKE(city,'^[aeiou]', 'i');

Example with test data

Kudos to MT0 for helpful comment!

I hope we helped!

Goran Kutlaca
  • 2,014
  • 1
  • 12
  • 18
  • 1
    You don't need `.+` at the end of the regular expression; it is sufficient to just match the first character without matching the rest of the string. You also don't need to include both cases if you use the `i` case-insensitive flag like this: `WHERE REGEXP_LIKE( city, '^[aeiou]', 1, 1, 'i' )`. – MT0 Feb 28 '19 at 09:53
  • @MT0 thank you for for your input! I've upgraded my answer with your suggestions – Goran Kutlaca Feb 28 '19 at 10:25
8

Another way to get output

select distinct CITY from STATION where left(city,1) in ('a', 'e', 'i', 'o', 'u')
Chaitanya
  • 81
  • 1
  • 1
6

All 3 works on MySQL

  1. By using regular expression
SELECT DISTINCT(CITY)
FROM STATION 
WHERE CITY REGEXP '^[aeiou]';
  1. By using OR
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%");
  1. By using SUBSTR(string, start, length).
    SUBSTR is 1 based indexing.
    Therefore, SUBSTR(CITY, 1, 1) means extracting substring at position 1 of length 1 from CITY column.
SELECT DISTINCT(CITY)
FROM STATION 
WHERE SUBSTR(CITY, 1, 1) IN ('A', 'E', 'I', 'O', 'U');
darknova
  • 61
  • 1
  • 3
3

On MSSQL server:

SELECT DISTINCT(city) FROM station WHERE city LIKE '[a,e,i,o,u]%' ;

On MySQL:

SELECT DISTINCT(city) FROM station WHERE SUBSTR(city,1,1) IN ('a','e','i','o','u');
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Dev K.M
  • 41
  • 1
  • Please do not answer in just capital letters. It comes across as that you are shouting and is actually much harder to read. – Dragonthoughts Feb 05 '21 at 08:15
1

Try with MySQL solution:

select distinct CITY from STATION where substr(CITY,1,1) in ('a','e','i','o','u');

Here "distinct" will solve the problem of duplicate value and "substring" function extract substring from string . Substring also contain start & length . For more details follow the link :- https://www.w3schools.com/sql/func_mysql_substr.asp

1

This can be solved by using substring and lower functions:

SELECT CITY FROM STATION WHERE LOWER(SUBSTR(CITY,1,1)) in ('a','e','i','o','u');

substring will find the first character of string and lower function make this first character lower. Once we find the lower first character then we can find that it is vowel or not by using IN operator.

Ali Hasan
  • 512
  • 1
  • 4
  • 18
1

I will share 2 answers in particular for PostgreSQL. The first query returns the cities that start with a vowel using ILIKE:

SELECT city 
FROM table  
WHERE city ILIKE 'A%' 
OR city ILIKE 'E%' 
OR city ILIKE 'I%' 
OR city ILIKE 'O%' 
OR city ILIKE 'U%';

The ILIKE operator is used to perform a case-insensitive match so searching capital vowels would automatically small letters as well.

However, this query is too long and redundant in my opinion. Is it possible to rewrite this query without so many conditions? Yes!

Let's use SIMILAR TO:

SELECT city
FROM patients
WHERE city SIMILAR TO '[aeiouAEIOU]%'
ORDER BY city ASC;

This will return all rows from the table where the city column starts with a vowel (i.e., 'a', 'E', 'i', 'O', or 'U'). The % symbol is used as a wildcard to match any number of characters.

Note that SIMILAR TO and LIKE are not the same in PostgreSQL.

SIMILAR TO is used to match a string against a pattern that can include regular expressions. For example, you can use SIMILAR TO to find all strings that start with a vowel by using the pattern '[AEIOU]%'.

LIKE is used to match a string against a pattern that does not include regular expressions. It is similar to the = operator, except that it allows you to use the % and _ wildcard characters to match any number of or a single character, respectively. For example, you can use LIKE to find all strings that start with a vowel by using the pattern 'A%'.

Gulbala Salamov
  • 198
  • 3
  • 9
0

That worked for me

SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[aeiou]'
0
SELECT DISTINCT CITY FROM STATION WHERE SUBSTR(CITY,1,1) IN ('A','E','I','O','U')
Nava Bogatee
  • 1,465
  • 13
  • 15
0
select distinct city from station where regexp_like(city, ‘^[aeiou]’, ‘i’)

Also, for ending with a vowel '[aeiou]$' and for starting and ending with a vowel '^[aeiou]$' $- for ending with, ^ for starting with, i- for case insensitivity, will select cities ending with uppercase as well.

  • This does not answer the question - if looks for cities ending with a vowel not starting with one. Plus points though for the case insensitive param. – Ian Kenney Jun 05 '21 at 23:19
  • @IanKenney You are right. Maybe I was trying to reply someone in the comments who was asking for A query in City Names ending with a vowel. For sure, if we want to query for city names starting with a vowel ^ is used like:- select distinct city from station where regexp_like(city, '^[aeiou]', 'i') – vijay nara Jun 10 '21 at 19:42
0

For Mysql, this worked for me -

  select DISTINCT(CITY) from STATION 
  where CITY REGEXP '^[aeiou].*';
Jatin
  • 91
  • 5
0

This below solution is for MySQL DATABASE

 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%';
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Jul 06 '21 at 07:29
0
select distinct city from station where city ~*'^(a|e|i|o|u)[a-z]$';

This is for Postgres

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
0
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%";
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0
with cte as (
select CITY ,case when lower(substr(CITY,1,1)) in ('a','e','i','o','u') then 1 else 0 end as Rule2
    from STATION
)
select distinct CITY  from cte where Rule2=1;
Patrick
  • 1,189
  • 5
  • 11
  • 19
  • 1
    Could you explain further why using a CTE could help the OP, and therefore why using a CTE at all ? – Frankich Dec 20 '21 at 10:34
-1
SELECT distinct(city)
FROM station
WHERE city LIKE '[a,e,i,o,u]%'
M-Chen-3
  • 2,036
  • 5
  • 13
  • 34
ot.avr
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. Please explain what your code does and how it works; never post code on its own. – M-Chen-3 Dec 05 '20 at 17:02
-1

We can use a regexp combined with REGEXP_LIKE

MYSQL

SELECT DISTINCT CITY 
FROM STATION
WHERE REGEXP_LIKE(city,'^[AEIOU]');
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Sillians
  • 177
  • 2
  • 9