regexp_like similar function in MySQL? - 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]%'

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.

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')

Difference between LIKE and REGEXP in 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?

LIKE '[charlist]%' syntax not working in MySQL (phpMyAdmin)

There is table named Students. I want to extract the names of students whose name starts with either 'n' or 'p' or 'y'. I know that in TSQL (MS SQL server) I can write the query as follows and it works:
SELECT * FROM Students WHERE StudentName LIKE '[npy]%'
But when I execute the same query in MySQL (phpmyadmin) I was unable to retrieve the correct result set. I tried converting the letters of the student name into the same case which is mentioned in the charlist. I did bit of googling and found out that in MySQL we need to specify this as a regular expression. I executed the below query and got the expected result.
SELECT * FROM Students WHERE StudentName REGEXP '[[:<:]]n | [[:<:]]p | [[:<:]]y'
I want to know the reason why LIKE '[charlist]%' syntax is not working with MySQL. Is it due to the implementation in MySQL (MySQL doesn't support the syntax) or something wrong with the phpmyadmin version I'm using?
Any help regarding this is highly appreciated. Thanks :)
There is an even shorter way to write your query:
SELECT * FROM Students WHERE StudentName REGEXP '^[npy]'
And if you're concerned about being case sensitive:
SELECT * FROM Students WHERE StudentName REGEXP BINARY '^[npy]'
In MySQL, a REGEXP pattern match succeeds anywhere in the value, which differs from LIKE where the pattern must match the entire value.
The following link will give you a more complete answer:
MySQL Pattern Matching
MySQL :
Case insensitive: SELECT * FROM Students WHERE StudentName RLIKE '^[npy]' ;
Case sensitive : SELECT * FROM Students WHERE StudentName CAST(RLIKE as BINARY) '^[npy]' ;

Return first character SQL Derby

I know that I can use something like
select distinct left(table.column, 1) as firstChar
from table
In MySQL to return just the first character of a field.
The problem is I'm working with SQL Derby :( and it does not like this syntax.
Is there a way to perform this query using derby?
select substr(table.column,1,1)
from table
order by table.column