27

Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

enter image description here

I write the below query, but it's not working for me. Any suggestion?

select distinct city
from station
where city regexp '^[^aeiou].*[^aeiou]$'; 
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Shanti
  • 397
  • 1
  • 3
  • 5
  • 6
    Which DB is this ? Most DB do not support regex. Use functions that your DB supports. – Ari Singh Mar 12 '18 at 06:00
  • 2
    In what way does it not work? – klutt Mar 12 '18 at 07:10
  • 1
    Which [DBMS](https://en.wikipedia.org/wiki/DBMS) are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using `postgresql`, `oracle`, `sql-server`, `db2`, ... –  Mar 12 '18 at 08:56
  • 1
    @AriSingh: actually most DBMS **do** support regular expressions. But as there is no real SQL standard (at least for real POSIX regex) the syntax is very different for each DBMS product –  Mar 12 '18 at 08:57
  • "either/or" sounds like it is allowed to *both* start and end on a vowel? – Hans Kesting Mar 12 '18 at 09:55
  • 1
    Based on use of `VARCHAR2` data type database is Oracle. – Piro Mar 12 '18 at 21:44

50 Answers50

16

Assuming you are using MySQL, Here is what you are looking for

SELECT DISTINCT city FROM station WHERE city RLIKE '^[^aeiouAEIOU].*|.*[^AEIOUaeiou]$';

Footnote : RLIKE and DISTINCT

Anand G
  • 3,130
  • 1
  • 22
  • 28
12

This SQL Query will helpful for you. If you are using MS SQL then follow this line of code given below:

SELECT DISTINCT CITY 
FROM STATION  
WHERE CITY NOT LIKE '[AEIOU]%' OR CITY NOT LIKE '%[aeiou]';

If you are using MySQL then follow this line of code given below:

SELECT DISTINCT CITY 
FROM STATION 
WHERE (CITY NOT IN (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'))
OR 
(CITY NOT IN (SELECT CITY FROM STATION WHERE CITY LIKE 'A%' OR CITY LIKE 'E%' OR CITY LIKE 'I%' OR CITY LIKE 'O%' OR CITY LIKE 'U%'));
Vignesh VS
  • 921
  • 1
  • 14
  • 30
7
SELECT DISTINCT city
FROM   station
WHERE  city REGEXP '^[^aeiouAEIOU]|[^aeiouAEIOU]$'
Community
  • 1
  • 1
ebuzz168
  • 1,134
  • 2
  • 17
  • 39
5
 SELECT DISTINCT city FROM station WHERE city RLIKE '^[^aeiouAEIOU].* 
 [^aeiouAEIOU]$';

You have to place caret character inside square brackets which means not any vowels. Here is explanation

rob111
  • 1,249
  • 1
  • 13
  • 18
4

Try the following:

SELECT city 
FROM station 
WHERE left(city,1) not regexp 'a|e|i|o|u' or right(city,1) not regexp 'a|e|i|o|u' 
GROUP BY city
JSelser
  • 3,510
  • 1
  • 19
  • 40
Samrat Rai
  • 41
  • 2
4

This SQL Query will helpful for you if you are using Oracle:

SELECT DISTINCT city
FROM station
WHERE regexp_like (city, '^[^aeiouAEIOU].*') 
OR regexp_like (city, '.*[^aeiouAEIOU]$');

Another answer if you are using Oracle:

SELECT DISTINCT (CITY) 
FROM STATION 
WHERE NOT regexp_like(lower(CITY),'^[aeiou].*[aeiou]$');
Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
  • Regarding '^[aeiou].*[aeiou]$', how to understand the comma and star here? I saw this is a popular answer, but now sure how to understand the .* – emily_relax Feb 09 '21 at 05:05
  • 1. there is no comma, kindly check. 2. `.*` means string should start from either of (aeiou) and can have zero or more iteration of that letter. – Abhishake Gupta Feb 17 '21 at 14:49
4

Try this:

select DISTINCT city from STATION 
where  (CITY NOT LIKE 'a%'
        AND CITY  NOT LIKE 'e%'
        AND CITY NOT LIKE 'i%' 
        AND CITY NOT LIKE 'o%'
        AND CITY NOT LIKE 'u%')
OR (CITY NOT LIKE '%a' AND
    CITY  NOT LIKE '%e' AND
    CITY NOT LIKE '%i' AND 
    CITY NOT LIKE '%o' AND 
    CITY NOT LIKE '%u')
vijay
  • 75
  • 1
  • 9
4

You could try this for MySQL:

SELECT DISTINCT(CITY) 
FROM STATION 
WHERE CITY NOT REGEXP '^[aeiou]' 
    AND CITY NOT REGEXP '[aeiou]$';

This query will bring you the desired output for the asked question.

Furkan Ekinci
  • 2,472
  • 3
  • 29
  • 39
  • `DISTINCT` is not a function, it's a _set quantifier_. Simply skip those extra parentheses and write `SELECT DISTINCT CITY FROM...`, to make code clearer. – jarlh Dec 28 '21 at 13:08
3

Oracle solution:

SELECT DISTINCT CITY FROM STATION WHERE 
NOT REGEXP_LIKE(UPPER(CITY), '^[AEIOU]') OR 
NOT REGEXP_LIKE(UPPER(CITY), '[AEIOU]$'); 
nglauber
  • 18,674
  • 6
  • 70
  • 75
3
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[^aeiou]|[^aeiou]$';

this is what you are looking for bro.

Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
Raj Singh
  • 408
  • 1
  • 7
  • 17
3

This is a required solution for MS SQL SERVER,

select distinct city from station where city not like '[aeiouAEIOU]%' and city not like '%[aeiouAEIOU]'
shyamzzp
  • 115
  • 7
karthik8fd
  • 51
  • 3
3

Try this:

SELECT DISTINCT CITY FROM STATION WHERE CITY NOT RLIKE '^[aeiouAEIOU]' 
AND
CITY NOT RLIKE '[aeiouAEIOU]$'
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
2

A solution to your question should be the following query:

select DISTINCT CITY from STATION where CITY NOT LIKE '[a,e,i,o,u]%' OR
CITY NOT LIKE '%[a,e,i,o,u]'
B--rian
  • 5,578
  • 10
  • 38
  • 89
Nandhini
  • 31
  • 2
2

For MySQL Select DISTINCT CITY from STATION where CITY REGEXP '^[^aeiou]|[^aeiou]$';

1

I worked with this

SELECT DISTINCT CITY FROM STATION
WHERE CITY NOT LIKE 'a%' 
AND CITY NOT LIKE 'e%'
AND CITY NOT LIKE 'i%'
AND CITY NOT LIKE 'o%'
AND CITY NOT LIKE 'u%'
AND CITY NOT LIKE '%a' 
AND CITY NOT LIKE '%e'
AND CITY NOT LIKE '%i'
AND CITY NOT LIKE '%o'
AND CITY NOT LIKE '%u'
Kashif Faraz Shamsi
  • 513
  • 1
  • 7
  • 21
  • In the question, It asks OR not AND ".. either do not start with vowels OR do not end with vowels." – M. John Mar 10 '22 at 21:30
1
select distinct CITY
from STATION
where SUBSTRING(CITY, Length(CITY), 1) NOT in ('a','e','i','o','u','A','E','I','O','U') 
order by CITY
Nolequen
  • 3,032
  • 6
  • 36
  • 55
1
Select Distinct
       City
From
       Station
Where
       Lower(Left(City,1)) not in ('a','e','i','o','u')
       or Lower(Right(City,1)) not in ('a','e','i','o','u')
Nolequen
  • 3,032
  • 6
  • 36
  • 55
1
SELECT DISTINCT CITY 
FROM STATION 
WHERE 
left(city,1) NOT IN ('a','e','i','o','u') OR RIGHT(city,1) 

NOT IN ('a','e','i','o','u')
Enea Dume
  • 3,014
  • 3
  • 21
  • 36
1

This works for me

SELECT DISTINCT CITY FROM STATION WHERE NOT CITY RLIKE '^[AEIOUaeiou]' AND NOT CITY RLIKE '.*[AEIOUaeiou]$' 
vimuth
  • 5,064
  • 33
  • 79
  • 116
1
mySQL query:
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[^aeiou]' OR CITY REGEXP '[^aeiou]$' 
Zoe
  • 27,060
  • 21
  • 118
  • 148
1

Right answer this ,

SELECT DISTINCT CITY FROM STATION
WHERE NOT CITY RLIKE '^[AEIOUaeiou]' AND NOT CITY RLIKE '.*[AEIOUaeiou]$';
JIJOMON K.A
  • 1,290
  • 3
  • 12
  • 29
1
select distinct city 
from station
where city not RLIKE '^[aeiou]' and city not RLike '[aeiou]$'
1

MYSQL:

Select distinct city from station where city not regexp '^[aeiou].*[aeiou]$'

EXPLANATION : Using regular expression and NOT condition. So basically it is searches for the city which start with vowels '^[aeiou]' concatenated by 'n' values in between by '.' operator and second condition of ending with vowels '[aeiou]$'. And at the end since NOT condition is applied ,so only the ones whic do not meet the criteria are filtered out and displayed.

Sumeet Kumar
  • 123
  • 6
  • 2
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 15 '20 at 09:53
1
SELECT DISTINCT CITY 
FROM STATION 
WHERE 
  SUBSTRING(CITY,1,1) NOT IN ('A','E','I','O','U') 
  OR 
  SUBSTRING(CITY,1,1) NOT IN ('a','e','i','o','u')

This code works for MS SQL SERVER.

Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
Rohan Aher
  • 39
  • 5
1

I use this and it works, its ORACLE SQL:

select distinct city from station where regexp_like(city, '^[^aeiou].*|.*[^aeiou]$', 'i');
Jorge Ruiz
  • 11
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '22 at 01:04
0

Correction to the above statement using MS SQL SERVER is shown below:

SELECT DISTINCT CITY FROM STATION WHERE CITY NOT LIKE '[AEIOU]%' OR CITY NOT LIKE '[aeiou]**%**';
XORG_99
  • 180
  • 4
  • 15
  • To filter out both the upper case beginning vowel and the lower case beginning vowel, shouldn't it be an `AND` instead of an `OR`? Or better yet, just combine the cases, e.g. `WHERE CITY NOT LIKE '[AaEeIiOoUu]%' OR CITY NOT LIKE '%[AaEeIiOoUu]'` – ergohack Nov 01 '18 at 23:22
0

mysql query: SELECT DISTINCT city FROM station WHERE city NOT REGEXP '^[aeiouAEIOU].*[aeiouAEIOU]$';

0
select distinct city
from station 
where not lower(substr(city,1,1)) in ('a','e','i','o','u')
      or not lower(substr(city,-1,1)) in ('a','e','i','o','u');
A. S. K.
  • 2,504
  • 13
  • 22
0

The following query should do what you want. It worked for me successfully!

SELECT DISTINCT CITY 
FROM STATION
WHERE CITY NOT RLIKE '^[aeiouAEIOU]' OR CITY NOT RLIKE '[aeiouAEIOU]$'
luator
  • 4,769
  • 3
  • 30
  • 51
0

This worked for me using MYSQL. I also grouped the results by city, which is not needed...just prettier:

select distinct CITY
from STATION
where CITY NOT RLIKE '^[aeiouAEIOU]'
  OR CITY NOT RLIKE '[AEIOUaeiou]$'
GROUP BY CITY;
Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
0

This will work the way you want:

SELECT city 
FROM station 
WHERE left(city,1) not regexp 'a|e|i|o|u' or right(city,1) not regexp 'a|e|i|o|u' 
GROUP BY city
brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • Welcome to SO. You should add some explanation and also format your code (in this case with triple backticks and sql syntax hint). – m02ph3u5 Jul 28 '19 at 15:13
0
SELECT distinct city FROM station 
WHERE City not LIKE '[aeiou]%' and CITY NOT LIKE '%[aeiou]'
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

Assuming that you saw this question on hacker rank and given your screenshot of the table STATION schema also tells the same thing. Here is my answer and it worked for me in MYSQL.

select DISTINCT(city) from STATION where CITY REGEXP '^[^aeiou]' or CITY REGEXP '.*[^aeiou]$';

LohitPant
  • 1
  • 5
0

How about this one:

SELECT CITY from STATION WHERE LOWER(CITY) NOT REGEXP '^[aeiou].*[aeiou]$' GROUP BY CITY
  • Welcome to stackoverflow! When providing answers, please add context explaining what is happening in your code. Answers that are only code are not considered complete. – brae Oct 17 '19 at 20:36
0

MySQL RLIKE operator performs a pattern match of a string expression against a pattern. The pattern is supplied as an argument.

SELECT DISTINCT city FROM station 
WHERE city NOT RLIKE '^[aeiouAEIOU].*[aeiouAEIOU]$';

Ref: https://www.w3resource.com/mysql/string-functions/mysql-rlike-function.php https://dev.mysql.com/doc/refman/8.0/en/regexp.html#operator_regexp

0

As the requirement is mentioned that the start and end should not be Vowel, here we can use a Pattern using RLIKE:

select distinct city from station where (left(city,1) not in ('a','e','i','o','u') and right(city,1) not in ('a','e','i','o','u'));

0

mysql

Answer for "dont start with vowels either dont end with vowels" is SELECT DISTINCT CITY FROM STATION WHERE NOT CITY RLIKE '^[AEIOUaeiou]' or NOT CITY RLIKE '.*[AEIOUaeiou]$' ;

Answer for "dont start with vowels and dont end with vowels" is SELECT DISTINCT CITY FROM STATION WHERE NOT CITY RLIKE '^[AEIOUaeiou]' And NOT CITY RLIKE '.*[AEIOUaeiou]$' ;

  • 1
    Welcome to SO! The first answer is identical to the accepted answer: https://stackoverflow.com/posts/49229048/revisions, and is enough to answer the question. The second answer doesn't answer the question of either don't start with vowels or don't end with vowels.Your answer only either repeats a previous answer or doesn't add extra useful information. – MBorg Jan 06 '20 at 08:14
0

for oracle try this:

SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'^.*[^aeiouAEIOU]$') 
UNION 
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'^[^aeiouAEIOU].*$');
James Z
  • 12,209
  • 10
  • 24
  • 44
0

SELECT DISTINCT CITY FROM STATION WHERE CITY NOT like '%a' and CITY NOT like '%i' and CITY NOT like '%e' and CITY NOT like '%o' and CITY NOT like '%u';

0

For Oracle : select DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'^[^aeiou]|[^aeiou]$','i');

Shubham Parmar
  • 147
  • 1
  • 13
0

Simple MySql query can be like :

SELECT DISTINCT CITY FROM STATION WHERE CITY NOT REGEXP '^[aeiou].*[aeiou]$';
Ruchir Dixit
  • 105
  • 1
  • 9
0

My SQL Query:

SELECT DISTINCT CITY FROM STATION 
WHERE(CITY NOT LIKE 'u%' AND 
CITY NOT LIKE 'a%' AND 
CITY NOT LIKE 'e%' AND 
CITY NOT LIKE 'i%' AND 
CITY NOT LIKE 'o%') OR
( CITY NOT LIKE '%u' AND 
CITY NOT LIKE '%a' AND 
CITY NOT LIKE '%e' AND 
CITY NOT LIKE '%i' AND 
CITY NOT LIKE '%o');
0

for MySQL

SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP "^[^aeiouAEIOU].*[^aeiouAEIOU]$"
Shashank
  • 294
  • 3
  • 13
0

This one is for MySQL, just tried now, and its works.

SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT RLIKE '^[aeiou]'
OR CITY NOT REGEXP '[aeiou]$'
0

SELECT DISTINCT CITY FROM STATION WHERE lower(right(CITY,1)) not in('a','e','i','o','u');

For City not ending with vowel

0
SELECT DISTINCT CITY FROM STATION 
WHERE CITY REGEXP '^[^aeiou]' /*Checks City does not start with vowel*/
AND  CITY REGEXP '[^aeiou]$'; /*Checks City does not end with vowel*/
0

Simple and clean answer:

select distinct  city 
from station 
where city regexp '^[^aeiou].*[^aeiou]$'
Tamara Koliada
  • 1,200
  • 2
  • 14
  • 31
0

If you are using MS SQL:

SELECT DISTINCT CITY FROM STATION WHERE CITY NOT LIKE '[AEIOU]%' OR CITY NOT LIKE '%[aeiou]';

I noticed that most answers here uses "AND", but the question is to select cities that either starts with a vowel or ends with a vowel. So you should be using OR.

noobmaster007
  • 153
  • 2
  • 11
-1

In MySQL regular expressions are the same as in Python.

You can use regular expressions in this case as well.

  1. Select the cities which start and end with vowels.
  2. Next, put not in front, which will eliminate those cities.

The query will be:

select distinct(city) from station where not (city rlike '^[aeiouAEIOU]' and city rlike '[aeiouAEIOU]$');
Deepstop
  • 3,627
  • 2
  • 8
  • 21
-1
SELECT DISTINCT CITY 
FROM STATION 
WHERE SUBSTR(CITY,1,1) NOT IN ('A','E','I','O','U') OR
      SUBSTR(CITY,-1,1) NOT IN ('a','e','i','o','u');
TT.
  • 15,774
  • 6
  • 47
  • 88
  • 3
    Welcome to StackOverflow! Please [edit your question](https://stackoverflow.com/posts/58996949/edit) to include an explanation for your code. This question has more than *two dozen other answers*, many of which have good explanations. Without one on your answer, it's of comparatively low quality and will likely get downvoted or removed. Adding that explanation will help justify your answer's continued existence here. – Das_Geek Nov 22 '19 at 15:40