MySQL wildcard query - mysql

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 %.

Related

Mysql LIKE 2nd character and 2nd last character both are 'm'

I try to find query to find a string that 2nd character and 2nd last character both are letter m.
SELECT last_name
FROM employees
WHERE (last_name LIKE '_m%m_' AND LENGTH(last_name) >= '3');
Thanks in advance :)
Why not just OR instead of AND? I don't see the point of AND when your LIKE operator allready rules out names below three characters. You don't need to use regex nor a check for length:
SELECT last_name FROM employees
WHERE last_name LIKE '_m_'
OR last_name LIKE '_m%m_';
The use of OR and LIKE does catch any string that has at least 3 characters.
If you must use regex, try REGEXP operator:
SELECT last_name FROM employees WHERE last_name REGEXP '^.m(.*m)?.$';
Where the pattern means:
^.m - Start-line anchor with a single character and a literal 'm';
(.*m)? - Optional capture group to match 0+ characters upto a literal 'm';
.$ - A single character with end-line anchor.
The benefit of REGEXP is that it's a bit less verbose if you need case-insensitive matching using pattern: '^.[Mm](.*[Mm])?.$'. See an online demo.
If you need all record with second and last character is m you can use the following query:
select * from <table> where <column> like '_m%m'
the _ in the query is a placeholder for one character and % for many characters

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

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

Regex Error in MYSQL

I want to select cities starting with a,e, i,o,u and ending with a,e, i,o,u in MySQL.(Case not matters)
Query1
SELECT CITY FROM STATION WHERE CITY REGEXP '^[AEIOU]' and CITY REGEXP '[AEIOU]$';
Query2
SELECT CITY FROM STATION WHERE CITY REGEXP '^[AEIOU]*[AEIOU]$';
Why Query2 is giving me an error although Query1 is correct.
With your first query, you only fetch entries that start or end with vowels. The second one only matches entries that start with 0 or more vowels and end with a vowel (so, you will get results like a or Aou only).
You might try using
SELECT CITY FROM STATION WHERE CITY REGEXP '^[AEIOU].*[AEIOU]$'
^^
The .* pattern matches any 0+ chars, as many as possible, so it will matching any string that starts AND ends with a vowel.
However, WHERE CITY REGEXP '^[AEIOU]' and CITY REGEXP '[AEIOU]$' fetches entries only consisting of 1 vowel, and the above will not match a record like A (one-vowel string). To match those use an optional group:
SELECT CITY FROM STATION WHERE CITY REGEXP '^[AEIOU](.*[AEIOU])?$'
^ ^^
Here, (...)? is a capturing group (MySQL regex does not support non-capturing ones) that matches a sequence of patterns 1 or 0 times (due to the ? quantifier).
A couple of notes on the regex:
^[AEIOU].*[AEIOU]$ - matches a whole string that starts and ends with a vowel in a case insensitive way (REGEXP is not case sensitive, except when used with binary strings)
^ - matches the start of input
[AEIOU] - a single vowel from the set
.* - any 0+ chars as many as possible (POSIX regex used in MySQL does not support lazy quantifiers, and . matches any chars, even line break chars, too)
[AEIOU] - a vowel
$ - end of input.
^ : Match the beginning of a string.and $ : Match the end of a string.
so you can try with above both regex and also use % , may be helpful.

My sql:how to use Wildcard in Subquery?

example:
SELECT country
FROM data
WHERE city LIKE
(SELECT LEFT ('jakartada',7));
it's working nicely but if i give delimiter with value:4 ,i must give wildcard
like this ---> "%string%" ,where i can give wildcard in the query?
With LIKE you can use the following two wildcard characters in the pattern.
Character Description
% Matches any number of characters, even zero characters
_ Matches exactly one character
\% Matches one “%” character
\_ Matches one “_” character
does this query help you?
SELECT country
FROM data
WHERE city LIKE CONCAT ('%', (SELECT LEFT ('jakartada',7)), '%');
why you using sub query?
SELECT * FROM `data` WHERE city LIKE 'jakar%'