description:Notes and solutions on the HackerRank SQL challenges for MySQL/MariaDB.
---
# Baisc HackerRank SQL Challenges
# Basic HackerRank SQL Challenges
## Intro
Unless otherwise noted, assume these examples where tested on MySQL and MySQL CLI. I prefer MariaDB and PostgreSQL by it seems HackerRank does not provide any of those (as of June 18, 2023 at least).
Unless otherwise noted, assume these examples where tested on MySQL and MySQL CLI.
I prefer MariaDB and PostgreSQL by it seems HackerRank does not provide any of those (as of June 18, 2023 at least).
Also, we'll try to stick to standard-compliant (and shy away from vendor-specific features and syntax) SQL whenever possible for max portability.
```{note}
RackerRank SQL challenges come in three main levels of difficulty:
@@ -17,7 +20,6 @@ RackerRank SQL challenges come in three main levels of difficulty:
- Advanced
This page deals with the basic ones.
```

@@ -279,3 +281,54 @@ WHERE
LOWER(SUBSTR(title,1,1))
IN('a','e','i','o','u');
```
## Weather Observation Station 5
-[Weather Observation Station 5 :: HackerRank Easy SQL Challenge](https://www.hackerrank.com/challenges/weather-observation-station-5)
### UNION ALL of two queries
One approach is to use two queries: one for the city name length, and another one for the min city name length, limit by 1, and union the results to create a single resulting tabular structure:
```sql
(SELECT
city
,LENGTH(city)ASlen_city
FROMstation
ORDERBYlen_cityASC,cityDESC
LIMIT1)
UNIONALL
(SELECT
city
,LENGTH(city)ASlen_city
FROMstation
ORDERBYlen_cityDESC,cityDESC
LIMIT1);
```
### WHERE IN union of subquery
Or creating a sub-table to select the lengths from.
First, select the max and min lengths:
```sql
SELECTMAX(LENGTH(city))FROMstation
UNIONALL
SELECTMIN(LENGTH(city))FROMstation;
```
It returns a tabular structures with the max and min lengths, which can be used in a `WHEER/IN` clause: