DEV Community

Cover image for HackerRank SQL Basic Select
SpiffyEight77
SpiffyEight77

Posted on

HackerRank SQL Basic Select

Before Starting

I haven't handwritten SQL for a long time, but I have recently started practicing again to regain my skills. This post mainly records interesting problems and solutions in SQL Basic Select.

SQL Basic Select

1. Weather Observation Station 4

Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

The STATION table is described as follows:

| Field    | Type          | 
|----------|---------------| 
| ID       | NNUMBER       | 
| CITY     | VARCHAR2(21)  | 
| STATE    | VARCHAR2(2)   | 
| LAT_N    | NUMBER        | 
| LONG_W   | NUMBER        |
Enter fullscreen mode Exit fullscreen mode

The objective of this problem is to determine how to utilize the functions COUNT and DISTINCT in order to compute the quantity of answers.

SELECT COUNT(CITY) - COUNT(DISTINCT(CITY))
FROM STATION;
Enter fullscreen mode Exit fullscreen mode

2. Weather Observation Station 5

Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.

The STATION table is described as follows:

| Field    | Type          | 
|----------|---------------| 
| ID       | NNUMBER       | 
| CITY     | VARCHAR2(21)  | 
| STATE    | VARCHAR2(2)   | 
| LAT_N    | NUMBER        | 
| LONG_W   | NUMBER        |
Enter fullscreen mode Exit fullscreen mode

The goal of this problem is to use the UNIONLIMIT keywords and LENGTH function effectively for querying answers.

(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) ASC, CITY ASC
LIMIT 1)
UNION
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY ASC
LIMIT 1);
Enter fullscreen mode Exit fullscreen mode

3. Weather Observation Station 8

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

Input Format

The STATION table is described as follows:

| Field    | Type          | 
|----------|---------------| 
| ID       | NNUMBER       | 
| CITY     | VARCHAR2(21)  | 
| STATE    | VARCHAR2(2)   | 
| LAT_N    | NUMBER        | 
| LONG_W   | NUMBER        |
Enter fullscreen mode Exit fullscreen mode

The goal of this problem is to use the LIKELIMIT keyword effectively for querying answers.

SELECT DISTINCT(CITY)
FROM STATION
WHERE (CITY LIKE 'a%' 
    OR CITY LIKE 'e%' 
    OR CITY LIKE 'i%'
    OR CITY LIKE 'o%'
    OR CITY LIKE 'u%')
AND (CITY LIKE '%a' 
    OR CITY LIKE '%e' 
    OR CITY LIKE '%i'
    OR CITY LIKE '%o'
    OR CITY LIKE '%u');
Enter fullscreen mode Exit fullscreen mode

4. Weather Observation Station 11

Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

| Field    | Type          | 
|----------|---------------| 
| ID       | NNUMBER       | 
| CITY     | VARCHAR2(21)  | 
| STATE    | VARCHAR2(2)   | 
| LAT_N    | NUMBER        | 
| LONG_W   | NUMBER        |
Enter fullscreen mode Exit fullscreen mode

The goal of this problem is to use the LIKENOT and OR keyword effectively for querying answers.

SELECT DISTINCT(CITY)
FROM STATION
WHERE NOT (CITY LIKE 'a%' 
    OR CITY LIKE 'e%' 
    OR CITY LIKE 'i%'
    OR CITY LIKE 'o%'
    OR CITY LIKE 'u%')
OR NOT (CITY LIKE '%a' 
    OR CITY LIKE '%e' 
    OR CITY LIKE '%i'
    OR CITY LIKE '%o'
    OR CITY LIKE '%u');
Enter fullscreen mode Exit fullscreen mode

5. Higher Than 75 Marks

Query the Name of any student in STUDENTS who scored higher than 75  Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.

Input Format

The STUDENTS table is described as follows:

| Colume    | Type         | 
|----------|---------------| 
| ID       | Integer       | 
| Name     | String        | 
| Marks    | Integer       |
Enter fullscreen mode Exit fullscreen mode

The goal of this problem is to use the SUBSTR function for querying answers.

SELECT Name
FROM STUDENTS
WHERE Marks > 75
ORDER BY SUBSTR(Name, -3, 3) ASC, ID ASC;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)