Could someone help me clean this up? (Beginner, SQL) [duplicate] - mysql

This question already has answers here:
Query the list of CITY names from STATION that do not start with vowels and do not end with vowels
(22 answers)
Closed 6 months ago.
I'm doing exercises (SQL beginner practice) on hackerrank.com, which I'm sure many of you are familiar with.
This is the question:
Query the list of CITY names from STATION that do not start with vowels and do not end with vowels. Your result cannot contain duplicates.
My answer was the following:
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'
I know this is a ridiculous way to go about it- can you guys offer suggestions on how to make this shorter? I'm just not knowledgable enough in SQL at the moment to make it more succinct. If you could show a couple ways to go about it, that would be really helpful.

In MySQL, you can use the REGEXP operator:
select distinct city
from station
where city not regexp '^[aeiou].*[aeiou]$'
If your column has a case-sensitive collation, then you can lower the value before comparison:
where lower(city) not regexp '^[aeiou].*[aeiou]$'
Or, if you are running MySQL 8.0, you can use regexp_like() and set the match argument to 'i' to make the search case-insensitive:
where not regexp_like(city, '^[aeiou].*[aeiou]$', 'i')

Something like this may help
SELECT DISTINCT city
FROM station
WHERE city NOT LIKE '[AEIOUaeiou]%[AEIOUaeiou]'
However, this answer is not %100 correct without knowing exact RDBMS system which you are using

If regex is an option then check that the city does not match the following pattern:
^[aeiou]|[aeiou]$
This pattern matches ax, xa, aa and a i.e. "city starts with or ends with a vowel". When you invert the result it becomes "city does not start with and does not end with a vowel". See De Morgan's law.
MySQL implementation:
SELECT DISTINCT city, NOT city REGEXP '^[aeiou]|[aeiou]$' AS result
FROM (
SELECT 'xx' AS city UNION
SELECT 'ax' UNION
SELECT 'xa' UNION
SELECT 'aa' UNION
SELECT 'x' UNION
SELECT 'a'
) AS station
-- returns 1 for xx and x, 0 for all others

#Dervis - This might be a bit faster.
SELECT DISTINCT(city)
FROM station
WHERE UPPER(city) NOT LIKE '[AEIOU]%[AEIOU]'

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.
THIS IS FOR MS SQL USERS
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE '[^aeiou]%' OR CITY LIKE '%[^aeiou]';

Related

HackerRank Weather Observation Station 11 [duplicate]

This question already has answers here:
Could someone help me clean this up? (Beginner, SQL) [duplicate]
(5 answers)
Closed 1 year ago.
Query the list of CITY names from STATION that either does not start with vowels or do not end with vowels. Your result cannot contain duplicates.
SELECT DISTINCT CITY FROM STATION WHERE CITY NOT IN ('%a','%e','%i','%o','%u') OR CITY NOT IN ('a%','e%','i%','o%','u%');
This is giving me the names of all the cities.
Wildcard characters can't use with in. You should use like operator for use wildcard characters.
Solution:
SELECT DISTINCT CITY FROM STATION
WHERE (NOT(CITY LIKE 'A%' OR CITY LIKE 'E%' OR CITY LIKE 'O%' OR CITY LIKE 'I%' OR CITY LIKE 'U%')) OR
(NOT (CITY LIKE '%a' OR CITY LIKE '%e' OR CITY LIKE '%i' OR CITY LIKE '%o' OR CITY LIKE '%u'))
ORDER BY CITY;

How to get vowels in Like operator of SQL?

I was solving a problem on SQL from Hackerrank. I have to make a query such that it gives me all city names starting with a, e, i, o or u. I'm using Like operator but still wrong answer.
Here's the problem Link
Here's my solution-
SELECT DISTINCT CITY
FROM STATION
WHERE CITY LIKE '[AEIOU]%'
Can anybody explain?
LIKE does not support that parttern. You need a regular expression match for this:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[AEIOU]'
In the regex, ^ represents the beginning of the string, and the square brackets define a custom character class (meaning that the first character must belong to that list).
On the other hand, if you were to use LIKE, you would need multiple conditions, which would make the code lengthier (and probably less efficient):
WHERE
CITY LIKE 'A%'
OR CITY LIKE 'E%'
OR CITY LIKE 'I%'
OR CITY LIKE 'O%'
OR CITY LIKE 'U%'
select distinct city from station where Left(city,1)='A' or Left(city,1)='E' or Left(city,1)='I' or Left(city,1)='O' or Left(city,1)='U';
This worked fine with me (MS SQL)
SELECT DISTINCT CITY FROM STATION WHERE LEFT(CITY, 1) IN ('A', 'E', 'I', 'O', 'U');
This return the City names starting with vowel.

Are these two different ```RLIKE ^[^aeiou]``` and ```NOT RLIKE ^[aeiou]```?

How is NOT RLIKE '^[aeiou].*[aeiou]$' different from RLIKE '^[^aeiou].*[^aeiou]$' ?
I was attempting mysql practice set on hackerrank and I cam across this doubt. At first I had a question to select all the cities from the table station that do not start or end with vowels. The query that gave me the answer was
SELECT city FROM station WHERE CITY NOT RLIKE '^[aeiou].*[aeiou]$'
The next question was to select all the cities from the table that do not start and do not end with a vowel. The previous line of code didn't give me the answer..
SELECT city FROM station WHERE CITY RLIKE '^[^aeiou].*[^aeiou]$'
gave me the right answer.
P.S: station is a table with columns id, city(this is the city name),state,long,lat.
They are not really similar at all. Just by glancing at them, the first cares about the first and last characters. The second does not.
So here is an example where they are different:
select col NOT RLIKE '^[aeiou].*[aeiou]$', col RLIKE '^[^aeiou].*'
from (select 'axyz' as col) x

Query the list of CITY names from STATION that do not start with vowels and do not end with vowels

This is a challenge question from HackerRank.
My first query is working fine:
select distinct city
from station
where city not regexp '^[aeiou]'
and city not regexp '[aeiou]$'
But my second query is giving wrong answer
select distinct city
from station
where city not regexp '^[aeiouAEIOU].*[aeiouAEIOU]$'
It is compiling but giving wrong results
https://dev.mysql.com/doc/refman/8.0/en/regexp.html
'NOT REGEXP' is Negation of REGEXP,
'REGEXP' -> Checks the 'REGEXP' if satisfied then return 1 else 0
select 'Chelsea' REGEXP '^[aeiou].*[aeiou]$' ;
0
'NOT REGEXP' -> checks the 'REGEXP' then return the opposite of the result.
select 'Chelsea' not REGEXP '^[aeiou].*[aeiou]$' ;
1
In this case, 'Chelsea' doesn't start with vowel hence REGEXP is 0 and not REGEXP is 1.
Hence you are counting the cities, even the name starts or ends with vowels.
This works in MS SQL server :
SELECT DISTINCT city FROM station WHERE city NOT LIKE '[AEIOU]%' AND city NOT LIKE '%[aeiou]';
Try this it worked for me
select distinct city
from station
where city RLIKE'^[^aeiouAEIOU].*$';
SELECT DISTINCT city
FROM station
WHERE city NOT REGEXP '^[aeiouAEIOU].*|[aeiouAEIOU]$'
Explanation: I make sure the city does not start with ('^') vowels and also does not end with ('$') vowels.
In Mysql,
where not CITY Regexp '^[aeiou].*$' or not CITY Regexp '^.*[aeiou]$'
This worked for me using the RLIKE operator... Hope this helps:
select distinct CITY from STATION where CITY NOT RLIKE '^[aeiouAEIOU]' AND CITY NOT RLIKE '[AEIOUaeiou]$' GROUP BY CITY;
You need to take care of 3 cases when using any DB
1. Word starts with vowel but ends with consonant
2. Word starts with consonant but ends with vowel
3. Word starts with consonant and ends with consonant
my solution implemented in mySQL
select distinct(CITY) from station where CITY RLIKE '(^[^aeiouAEIOU].*[aeiou]$|^[aeiouAEIOU].*[^aeiou]$|^[^aeiouAEIOU].*[^aeiou]$)';
Use this MySQL Command:
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[^aeiou].*[^aeiou]$';
Explanation:
DISTINCT is used to remove duplicates from the result set of a SELECT statement.
"^[^aeiou]" means city names that do not start with vowels.
"[^aeiou]$" means city names that do not end with vowels.
".*" represents any string of any length.
Here is the right answer:
select DISTINCT(CITY)
from STATION
Where CITY RLIKE '^[^aieouAEIOU].*[^AEIOUaeiou]$';
Try this
select distinct city
from station
where city rlike '^[^aeiouAEIOU].*' and city rlike '.*[^aeiouAEIOU]$'
I consider both the answer right.
But some how first query is failed whereas second one is passed from hackerrank platform.
First query
SELECT DISTINCT city FROM station
WHERE city not RLIKE '^[aeiou].*[aeiou]$';
Second query : working with no issue
SELECT DISTINCT city FROM station
WHERE city not RLIKE '^[aeiou]'
and city not rlike '[aeiou]$';
SELECT DISTINCT city
FROM station
WHERE NOT city LIKE 'A%'
AND NOT city LIKE 'E%'
AND NOT city LIKE 'I%'
AND NOT city LIKE 'O%'
AND NOT city LIKE 'U%';
a lengthy way but you will get the answer
Here is a query using a Where column Not Rlike clause.
This does a pattern match of a string expression against a pattern.
Select Distinct city
From station
Where city Not Rlike '^[aeiouAEIOU].*[aeiouAEIOU]$';
Additionally, the Distinct is used to remove all results that are the same.
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.
THIS IS FOR MS SQL USERS
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE '[^aeiou]%' OR CITY LIKE '%[^aeiou]';
If regex is an option then check that the city does not match the following pattern:
^[aeiou]|[aeiou]$
This pattern matches ax, xa, aa and a i.e. "city starts with or ends with a vowel". When you invert the result it becomes "city does not start with and does not end with a vowel". See De Morgan's law.
MySQL implementation:
SELECT DISTINCT city, NOT city REGEXP '^[aeiou]|[aeiou]$' AS result
FROM (
SELECT 'xx' AS city UNION
SELECT 'ax' UNION
SELECT 'xa' UNION
SELECT 'aa' UNION
SELECT 'x' UNION
SELECT 'a'
) AS station
-- returns 1 for xx and x, 0 for all others
This can also work:
SELECT DISTINCT city
FROM station
WHERE city NOT RLIKE '[aeiou]$';
Simplest code, you can try:
select distinct city
from station
WHERE lower(city) Not Rlike '^[aeiou].*[aeiou]$';
OR
SELECT DISTINCT city FROM station
WHERE lower(city) not RLIKE '^[aeiou]'
and lower(city) not rlike '[aeiou]$';
This is basic solution for this problem, I hope it will surely work.
Select Distinct City From Station
Where City not LIKE 'a%a' and City not LIKE 'a%e' and City not LIKE 'a%i' and City not LIKE 'a%o' and City not LIKE 'a%u' and City not LIKE 'e%a' and City not LIKE 'e%e' and City not LIKE 'e%i' and City not LIKE 'e%o' and City not LIKE 'e%u' and City not LIKE 'i%a' and City not LIKE 'i%e' and City not LIKE 'i%i' and City not LIKE 'i%o' and City not LIKE 'i%u' and City not LIKE 'o%a' and City not LIKE 'o%e' and City not LIKE 'o%i' and City not LIKE 'o%o' and City not LIKE 'o%u' and City not LIKE 'u%a' and City not LIKE 'u%e' and City not LIKE 'u%i' and City not LIKE 'u%o' and City not LIKE 'u%u';
SELECT DISTINCT CITY FROM STATION WHERE NOT LOWER(LEFT(CITY, 1)) IN ('a', 'e', 'i', 'o', 'u') AND NOT LOWER(RIGHT(CITY, 1)) IN ('a', 'e', 'i', 'o', 'u');
--This works for db2 databases
I got the correct out put for the below code for MySql server
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')
Most of us are reading the question wrong. They want either of the one condition to be fulfilled, not the both. The following solution works well in MS SQL Server :
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') );
select distinct city as cty from station where city not in (
select city from Station where
Right(city,1) in ('a','e','i','o','u') and
Left(city,1) in ('a','e','i','o','u')
)

SQL query select list where first and last letter arnt

please see the Q on https://www.hackerrank.com/challenges/weather-observation-station-11?h_r=next-challenge&h_v=zen
regarding an sql query:
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.
So far I have got
SELECT DISTINCT CITY FROM STATION
WHERE CITY NOT RLIKE'^[aeiouAEIOU].*$'
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'
and it is telling me this is wrong but I cant see any wrong answers in the output please advise....
thanks
Helo, it worked in my tests.
Try to compose your regular expression.
Not begins AND not ends with wovels:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT RLIKE '^[aeiouAEIOU].*'
AND CITY NOT RLIKE '.*[aeiouAEIOU]$';
Not begins OR not ends with wovels:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT RLIKE '^[aeiouAEIOU].*'
OR CITY NOT RLIKE '.*[aeiouAEIOU]$';
Not begins XOR (OR exclusive) not ends with wovels:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT RLIKE '^[aeiouAEIOU].*'
XOR CITY NOT RLIKE '.*[aeiouAEIOU]$';
Hope to help.
This worked for me using MySQL:
select distinct CITY from STATION where CITY NOT RLIKE '^[aeiouAEIOU]' OR CITY NOT RLIKE '[AEIOUaeiou]$' GROUP BY CITY;