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.
Recommended SQL Courses:
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 |
Sample data in Table (STATION):
Solution-1: Using REGEXP_LIKE Function (MySQL Query):
NOTE:
- "^" (Caret operator) character in REGEXP_LIKE function is used to check string starting with matching pattern.
- "$" character in REGEXP_LIKE function is used to check string ending with matching pattern.
- ".*" (period operator followed by asterisk character) in REGEXP_LIKE function is used to match with zero or more instances of any character except NULL.
- "[]" (square brackets) are used to pass the list of characters to check. Here, it is a,e,i,o,u.
- "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):
NOTE:
- "^" (Caret operator) character in REGEXP_LIKE function is used to check string starting with matching pattern.
- "$" character in REGEXP_LIKE function is used to check string ending with matching pattern.
- "[]" (square brackets) are used to pass the list of characters to check. Here, it is a,e,i,o,u.
- "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):
NOTE:
- 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.
- 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:
--------------------------------------------------------------------------------
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