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

HackerRank: [Basic Select - 15/20] Weather Observation Station-10 | REGEXP_LIKE, 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 that do not end with vowels. 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 NOT REGEXP_LIKE(CITY, '[aeiou]$', 'i');

NOTE: 
  1. "$" character in REGEXP_LIKE function is used to check string ending with matching pattern.
  2. "[]" (square brackets) are used to pass the list of characters to check. Here, it is a,e,i,o,u.
  3. "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 CITY
FROM STATION
WHERE
RIGHT(CITY,1) NOT IN ('a','e','i','o','u','A','E','I','O','U');

NOTE: 
  1. 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 Mills
Loma Mar
Sandy Hook
Tipton
Arlington
Turner
Slidell
Negreet
Chignik Lagoon
Hanna City
Albany
Monument
Manchester



--------------------------------------------------------------------------------
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
إرسال تعليق (0)
أحدث أقدم