-4

Why is it not working?

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. our result cannot contain duplicates.

SELECT DISTINCT CITY FROM STATION 
WHERE CITY LIKE '%A' OR CITY LIKE '%E' OR LIKE '%I' LIKE '%O' OR LIKE '%U' 
ORDER BY CITY;

What would be the correct query?

6
  • 6
    "Not working" doesn't communicate anything useful. What should it be doing? What is it doing?
    – tadman
    Mar 8, 2018 at 18:26
  • 1
    What's the problem? Syntax error? Unexpected output?
    – chepner
    Mar 8, 2018 at 18:27
  • 2
    You have several 'or's missing, and a few cities too...
    – Nyerguds
    Mar 8, 2018 at 19:18
  • My short answer (MySQL query) here -> stackoverflow.com/a/75693303/7076660 . Mar 10 at 7:45
  • 1
    Similarly, @Payal, please be careful when reviewing edits. If you are dissatisfied with any existing answers, you can always add your own and accept it. May 28 at 3:30

17 Answers 17

6

You can try this also-

SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '[aeiouAEIOU]$';
1
  • For Oracle the query should be SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY, '[aeiouAEIOU]$'); Nov 22, 2022 at 4:00
2

You query has syntax error, for each "or", you need to put city like instead of or. In addition, remember that in most of the dbms, it is case sensitive. If you want to ignore both upper case and lower case, do it like this. I am using mysql syntax, different dbms has different functions for lcase

SELECT DISTINCT CITY FROM STATION
WHERE lcase(CITY) LIKE '%a'
OR lcase(CITY) LIKE '%e'
OR lcase(CITY) LIKE '%i'
OR lcase(CITY) LIKE '%o'
OR lcase(CITY) LIKE '%u'
ORDER BY CITY;
0
2

I solved that problem this way. Instead of checking the last character one by one, I thought it is easier to check if we use in operator.

select distinct city
from station
where substring(city,len(city),1) in ('a','e','u','o','i')
order by city;
1
  • Why do such a complicated way if the accepted answer uses another? Can you add some explanation about why your's could be better?
    – Nico Haase
    Dec 6, 2018 at 14:31
2

Using MySQL;

select distinct CITY 
from STATION 
where right(city,1) in ('a', 'e', 'i', 'o', 'u');
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

I solved it like this:

SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY,'[aeiou]$','i');

In this case, the 'i' at the end is called a "flag" which means the vowels can be upper or lower case.

1

select distinct CITY from STATION where CITY like '%[a,e,i,o,u]';

1
1

As much I can remember, this is one of the problems listed on HackerRank SQL Practice Questions.

There were 2 types of questions:-

  1. City names starting with vowels
  2. City names ending with vowels

Answers to both were as follows:

Starting with vowels:

SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[aeiouAEIOU]';

Ending with vowels:

SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '[aeiouAEIOU]$';
0

Try this in MS SQL server

select distinct(city) from station where city like '%[a,e,i,o,u]' order by city;
1
0
select CITY
from STATION
where right(lower(CITY),1) in ('a','e','i','o','u')
group by CITY;
0
0

Using Regular Expression in MySQL would be like this:

SELECT DISTINCT(CITY) FROM STATION WHERE REGEXP_LIKE(CITY, '[aiueo]$', 'i') ORDER BY CITY;
0

you can use substring to solve that

SELECT DISTINCT CITY FROM STATION WHERE SUBSTRING(CITY,-1,1) NOT IN ('a','e','i','o','u')
0

For Mysql, this worked for me -

select DISTINCT(CITY) from STATION 
where CITY REGEXP '[aeiou]$';
0
Select distinct city 
from Station 
where lower(SUBSTR(city,length(city),1))in ('a','e','i','o','u');
0

Simple but easy

select distinct(CITY) from Station where substr(City,LENGTH(CITY),Length(City)) in ('a','e','i','o','u')

0
SELECT DISTINCT(CITY)
FROM STATION
WHERE CITY LIKE '%[aeiou]';
-1

In MsSql Try this:

SELECT DISTINCT CITY FROM STATION WHERE RIGHT(CITY,1) IN  ('a', 'e', 'i', 'o', 'u');

Not the answer you're looking for? Browse other questions tagged or ask your own question.