Is there a Shorter way - SQL - multiple LIKE and OR - mysql

Is there away to shorten a SQL statement like this? If so what would it be in MySQL and also in PostgreSQL?
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%')
AND (city LIKE '%a' OR city LIKE '%e' OR city LIKE '%i' OR
city LIKE '%o' OR city LIKE '%u');

With MySQL, we could use a regular expression, something like this:
WHERE city REGEXP '^[AEIOU].*[aeiou]$'
Not to dive too deep into regular expressions, but as an aid to deciphering that:
the caret ^ matches the beginning of the string
the next character has to be one of the characters in the set in the square brackets A, E, I, O or U
followed by any number of any character, the dot . matches any character, the * is repetition, zero, one or more
the last character has to match one of characters in the square brackets a, e, i, o, u because the dollar sign $ matches the end of the string
For testing, use a SELECT statement
SELECT t.city
, t.city REGEXP '^[AEIOU].*[aeiou]$'
FROM ( SELECT 'A' AS city
UNION ALL SELECT 'Aa'
UNION ALL SELECT 'Abba'
UNION ALL SELECT 'a'
) t
As Gordon points out in a comment, the same approach using a regular expression comparison will work in PostgreSQL. But there syntax is a a little different, the comparison operation is a tilde ~ character, in place of MySQL REGEXP or RLIKE keyword.
WHERE city ~ '^[AEIOU].*[aeiou]$'
https://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP

Related

MySQL | Regular expressions (Regexp)

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.
Input Format
The STATION table is described as follows:
station table
Select distinct city from station where city REGEXP '^[aeiou].';
In this query what does this '.' operator stands for "^[aeiou]." when i am removing this '.' i am getting wrong answer why is it so?
In the context of a regular expression, dot . means any single character. So ^[aeiou]. means match a city name which starts with a vowel, followed by any second character. You don't even need the dot here, and could just use:
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^[aeiou]';

difference between REGEXP and LIKE?

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates
select distinct city from station where city LIKE '[a,e,i,o,u]%'
You have two ways of doing this. If you want to use LIKE, you'll need to have separate checks for each starting vowel:
SELECT DISTINCT city
FROM station
WHERE city LIKE 'a%' OR city LIKE 'e%' OR city LIKE 'i%' OR
city LIKE 'u%' OR city LIKE 'u%';
If you can use REGEXP, it is possible to write the above logic more succinctly:
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^[aeiou]'; -- ^ means starts with a vowel letter

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.

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

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]';

How to write an SQL query to match city names ending with vowels?

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 A DISTINCT CITY FROM THE 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?
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';
The correct answer is above 👆
this is accepted, as inside the quotations, the alphabet is considered case sensitive
hence use '%a' and similarly all the other vowels.
(% is a wildcard entry)
You can try this also-
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '[aeiouAEIOU]$';
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;
Using MySQL;
select distinct CITY
from STATION
where right(city,1) in ('a', 'e', 'i', 'o', 'u');
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;
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
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.
select distinct CITY from STATION where CITY like '%[a,e,i,o,u]';
Try this in MS SQL server
select distinct(city) from station where city like '%[a,e,i,o,u]' order by city;
select CITY
from STATION
where right(lower(CITY),1) in ('a','e','i','o','u')
group by CITY;
Using Regular Expression in MySQL would be like this:
SELECT DISTINCT(CITY) FROM STATION WHERE REGEXP_LIKE(CITY, '[aiueo]$', 'i') ORDER BY CITY;
you can use substring to solve that
SELECT DISTINCT CITY FROM STATION WHERE SUBSTRING(CITY,-1,1) NOT IN ('a','e','i','o','u')
For Mysql, this worked for me -
select DISTINCT(CITY) from STATION
where CITY REGEXP '[aeiou]$';
Select distinct city
from Station
where lower(SUBSTR(city,length(city),1))in ('a','e','i','o','u');
Simple but easy
select distinct(CITY)
from Station
where substr(City,LENGTH(CITY),Length(City)) in ('a','e','i','o','u')
In MsSql
Try this:
SELECT DISTINCT CITY FROM STATION WHERE RIGHT(CITY,1) IN ('a', 'e', 'i', 'o', 'u');