# HackerRank: [SQL Basic Select] (17/20) Weather Observation Station-12 | regexp_like, left, right function in SQL

I started studying SQL from a very famous site - HackerRank. Here I will try to provide multiple approaches & solutions to the same problem. It will help you learn and understand SQL in a better way.

Please make use of my blog posts for learning purpose only and feel free to ask your questions in the comment box below in case of any doubt.

Recommended SQL Courses:

SQL Problem Statement:

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.

The STATION table is described as follows:

 Table: STATION
where LAT_N is the northern latitude and LONG_W is the western longitude.

Sample data in Table (STATION):
`ID CITY STATE LAT_N LONG_W794 Kissee Mills MO 140 73824 Loma Mar CA 49 131603 Sandy Hook CT 72 148478 Tipton IN 34 98619 Arlington CO 75 93711 Turner AR 50 101839 Slidell LA 85 152...754 South Haven MI 145 53144 Eskridge KS 1`

### Solution-1: Using REGEXP_LIKE Function (MySQL Query):

`SELECT DISTINCT CITYFROM STATIONWHERE NOT REGEXP_LIKE(CITY, '^[aeiou]', 'i')AND NOT REGEXP_LIKE(CITY, '[aeiou]\$', 'i');`

NOTE:
1. "^" (Caret operator) character in REGEXP_LIKE function is used to check string starting with matching pattern.
2. "\$" character in REGEXP_LIKE function is used to check string ending with matching pattern.
3. "[]" (square brackets) are used to pass the list of characters to check. Here, it is a,e,i,o,u.
4. "i" is passed as an argument to the REGEXP_LIKE function for case_insensitivity. i.e. It will try check match with small as well as capital letters.

### Solution-2: Using RIGHT Function (MySQL Query):

`SELECT DISTINCT CITYFROM STATIONWHERE LEFT(CITY,1) NOT IN ('a','e','i','o','u','A','E','I','O','U')AND RIGHT(CITY,1) NOT IN ('a','e','i','o','u','A','E','I','O','U');`

NOTE:
1. The LEFT function is used to take a substring of CITY starting from the left, and "1" is passed as an argument to select only 1 character from the left.
2. The RIGHT function is used to take a substring of CITY starting from the right, and "1" is passed as an argument to select only 1 character from the right.

Sample Output:
`Kissee MillsLoma MarSandy HookTiptonTurnerSlidellNegreetChignik LagoonHanna CityMonumentManchesterPrescottGraettinger`

--------------------------------------------------------------------------------