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

HackerRank: [Basic Select - 13/20] Weather Observation Station-8 | 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.

Click Here for the previous blog-post in the series.


SQL Problem Statement:

Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.



The STATION table is described as follows:

Table: STATION
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_W
794 Kissee Mills MO 140 73
824 Loma Mar CA 49 131
603 Sandy Hook CT 72 148
478 Tipton IN 34 98
619 Arlington CO 75 93
711 Turner AR 50 101
839 Slidell LA 85 152
.
.
.
754 South Haven MI 145 53
144 Eskridge KS 1




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

SELECT DISTINCT CITY
FROM STATION
WHERE REGEXP_LIKE(CITY, '^[aeiou].*[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. ".*" (period operator followed by asterisk character) in REGEXP_LIKE function is used to match with zero or more instances of any character except NULL.
  4. "[]" (square brackets) are used to pass the list of characters to check. Here, it is a,e,i,o,u.
  5. "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 REGEXP_LIKE Function (MySQL Query):

SELECT DISTINCT CITY
FROM STATION
WHERE REGEXP_LIKE(CITY, '^[aeiou]', 'i')
AND 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-3: Using LEFT & RIGHT Function (MySQL Query):

SELECT DISTINCT CITY
FROM STATION
WHERE LEFT(CITY,1) in ('a','e','i','o','u','A','E','I','O','U')
AND RIGHT(CITY,1) 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:
Upperco
Aguanga
East China
East Irvine
Amo
Eleele
Oconee
Amazonia
Aliso Viejo
Andersonville
Arkadelphia
Eriline
Eastlake



--------------------------------------------------------------------------------
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks & Regards,
-Akshay P Daga
Post a Comment (0)
Previous Post Next Post