MySQL | Regular expressions (Regexp) - mysql

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

Related

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.

Understanding why querying cities with vowels at start and end doesnt work

Task:
Query the list of names from table which have vowels as both their first and last characters [duplicate].
I want to query the list of CITY names from the table STATION(id,city, longitude, latitude) which have vowels as both their first and last characters. The result cannot contain duplicates.
My query:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY LIKE '[aeiou]%' AND '%[aeiou]'
I found this solution:
Select distinct city
from station
Where regexp_like(city, '^[aeiou].*[aeiou]$','i');
Why isn't my query working?
'[aeiou]' is a regex character class, which is not supported by operator LIKE. So your query won't do what you expect: it actually searches for a litteral string that starts with '[aeiou]' (and even if it was, you would need to repeat expression city like ... twice: city like ... and ... does not do what you expect either).
The solution you found uses regexp_like() with the following regex: ^[aeiou].*[aeiou]$, which means:
^ beginning of the string
[aeiou] one of the characters in the list
.* a sequence of 0 to N characters
[aeiou] one of the characters in the list
$ end of the string
Option 'i' makes the search case insensitive.
This works, but requires MySQL 8.0. If you are running an earlier version, consider using a REGEXP condition instead:
CITY REGEXP '^[aeiou].*[aeiou]$'

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

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

MySQL wildcard query

I'm relatively new with SQL and I ran into a problem/question while doing some practice problems.
https://www.hackerrank.com/challenges/weather-observation-station-8
Here's the query I used with MySQL:
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE '[aeiou]%[aeiou]'
I'm confused why this doesn't work. Here's my thinking for the query:
SELECT DISTINCT CITY
^ make sure each city returned isn't repeated
FROM STATION
^ from the STATION table
WHERE CITY LIKE '[aeiou]%[aeiou]'
^ CITY names that are selected can have the first letter begin with [aeiou], have anything in between, and end with [aeiou].
Any help or advice would be appreciated, thanks!
If you are using regex, you can use regexp or RLIKE in place of LIKE. The other thing you need to do is put ^ to denote the first character, $ to denote the last character, and .* for wildcard. See this and this:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY RLIKE '^[aeiou].*[aeiou]$'
The like operator has only 2 wildcard characters: % and _, it does not handle the bracket syntax. You need regular expressions for that, and you can use the rlike operator for that. But in regular expressions you need to use . instead of _ and .* instead of %.