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
Related
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.
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]$'
I am a beginner so please help me.
There are 2 things you need to combine in this case.
Because you didn't provide enough information in your question we have to guess what you mean by name. I'm going to assume that you have a single name column, but that would be unusual.
With strings, to match a character column that is not an exact match, you need to use LIKE which allows for wildcards.
You also need to negate the match, or in other words show things that are NOT (something).
First to match names that START with 'A'.
SELECT * FROM table_name WHERE name LIKE 'A%';
This should get you all the PEOPLE who have names that "Start with A".
Some databases are case sensitive. I'm not going to deal with that issue. If you were using MySQL that is not an issue. Case sensitivity is not universal. In some RDBMS like Oracle you have to take some steps to deal with mixed case in a column.
Now to deal with what you actually want, which is NOT (starting with A).
SELECT * FROM table_name WHERE name NOT LIKE 'A%';
your question should have more detail however you can use the substr function
SELECT name FROM yourtable
WHERE SUBSTR(name,1,1) <> 'A'
complete list of mysql string functions here
mysql docs
NOT REGXP operator
MySQL NOT REGXP is used to perform a pattern match of a string expression expr against a pattern pat. The pattern can be an extended regular expression.
Syntax:
expr NOT REGEXP pat
Query:
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^[a]';
or
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^a';
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?
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 %.