Difference between LIKE and REGEXP in MYSQL - mysql

I have to select all CITY names from STATION database that are starting with vowels.
I tried in MYSQL using:
Like operator:
select DISTINCT CITY from STATION where CITY LIKE "[aeiouAEIOU]%";
REGEXP operator:
select DISTINCT CITY from STATION where CITY REGEXP '^[AEIOUaeiou]';
When I used the query with Like operator it gave zero results. But the query with REGEXP worked fine.
Question:
Are brackets ([...]) not supported with LIKE operator in MySQL?
Apart from performance advantage of LIKE over REGEXP, what are the differences between them?

Related

Can someone please explain why the same logic of a query seems to be working in ORACLE but not in MySQL?

The original question:
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.(source hackerrank)
name of the table: Station; the name of required attribute: city
My query when I use MYSQL:
select distinct city
from station
where lower(city) regexp "^[^aeiou]*"
or lower(city) regexp "[^aeiou]$";
I get names like 'Aguanga' and 'East Irvine' in my output on MySQL BUT when I use oracle and write the same logic in that, I get the correct output. The query I wrote for Oracle:
select distinct city
from station
where regexp_like(lower(city), '^[^aeiou]')
or regexp_like(lower(city),'[^aeiou]$');
I am new at SQL and do not understand why this is not working. I've used this method to get the correct output in MySQL for not starting with a vowel and not ending with a vowel but for 2 different questions separately.

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

Use of $ symbol instead of %

I have started practising SQL and I think I need to brush up some topics.
This problem statement in hackerrank states that the query lists CITY names from STATION that do not end with vowels.
I tried using wildcard '%[^aeiou]'
SELECT Distinct City
FROM Station
Where City LIKE '%[^aeiou]'
Order By City;
Compiler Message: Answer Wrong.
I know other methods to execute the program but what is wrong with this one. Also, I am wondering how REGEXP '[^aiueo]$' is working but Like '%[^aeiou] or Not Like '%[aeiou]' is not executable?
MySQL does not support SQL Server extensions for LIKE. Use regular expressions instead:
SELECT DISTINCT City
FROM Station
WHERE City REGEXP '[^aeiou]$'
ORDER BY City;
Note that you have to anchor the regular expression to the end of the string. LIKE patterns automatically match the entire string; REGEXP patterns can match anywhere in the string, unless anchored to the beginning or end.
Or, eschew regular expressions:
SELECT DISTINCT City
FROM Station
WHERE RIGHT(City, 1) NOT IN ('a', 'e', 'i', 'o', 'u')
ORDER BY City;
LIKE only supports wildcards and you use for very simple match.
REGEXP or RLIKE has full regular expression support.
A regular expression is a powerful way of specifying a pattern for a
complex search. This section discusses the functions and operators
available for regular expression matching and illustrates, with
examples, some of the special characters and constructs that can be
used for regular expression operations.
See the manual on LIKE and on REGEXP
If you must use LIKE try this:
SELECT DISTINCT City
FROM Station
WHERE City NOT LIKE '%a'
OR City NOT LIKE '%e'
OR City NOT LIKE '%i'
OR City NOT LIKE '%o'
OR City NOT LIKE '%u';
If you want a faster query use RIGHT (Gordon Linoff answer) or REGEXP

mariadb vs mysql * how both handle SELECT * FROM Customers WHERE City LIKE '[acs]%';

When I run the following on MySql:
SELECT * FROM Customers WHERE City LIKE '[acs]%';
I get the expected results.
However, the same query on mariadb returns an empty set. Am I doing something wrong?
I've looked at the docs and it seems like they want it to be more like
SELECT * FROM Customers WHERE City RLIKE '(a|c|s)'.
Is there a command that will work on both? Should I use REGEXP?
SELECT * FROM Customers WHERE City REGEXP 'a|c|s'
Yo can you the REGEXP function:
SELECT * FROM Customers WHERE City REGEXP '[acs]'
It is highly unlikely that this query returns results on MySQL:
SELECT *
FROM Customers
WHERE City LIKE '[acs]%';
Why? No city names that I know of have the character '['. And, if they did, it seems even less likely that they would have the character ']'.
You may be confusing MySQL with SQL Server. The latter has extended LIKE patterns to include character classes. The equivalent logic in MySQL is:
WHERE City REGEXP '^[acs]'
Or, if you prefer:
WHERE LEFT(City, 1) IN ('a', 'c', 's')

regexp_like similar function in MySQL?

I was trying to solve a problem in SQL and I came across the problem:
Query the list of CITY names from STATION table that do not start with vowels. Your result cannot contain duplicates.
I used regexp_like() function using Oracle but how I can query the results using MySQL?
In Oracle I did regexp_like(city, '^[^aeiou]', 'i')
MySQL has a REGEXP keyword for just such an occasion.
SELECT ...
FROM ...
WHERE field REGEXP 'expression';
See: http://dev.mysql.com/doc/refman/5.7/en/regexp.html (first Google result for MySQL REGEXP)
Select distinct City
from Station
where Left(Upper(city),1))
not like '%[AEIOU]%'