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


This content originally appeared on DEV Community and was authored by SpiffyEight77

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        |

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;

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        |

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);

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        |

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');

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        |

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');

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       |

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;


This content originally appeared on DEV Community and was authored by SpiffyEight77


Print Share Comment Cite Upload Translate Updates
APA

SpiffyEight77 | Sciencx (2023-03-12T05:40:00+00:00) HackerRank SQL Basic Select. Retrieved from https://www.scien.cx/2023/03/12/hackerrank-sql-basic-select/

MLA
" » HackerRank SQL Basic Select." SpiffyEight77 | Sciencx - Sunday March 12, 2023, https://www.scien.cx/2023/03/12/hackerrank-sql-basic-select/
HARVARD
SpiffyEight77 | Sciencx Sunday March 12, 2023 » HackerRank SQL Basic Select., viewed ,<https://www.scien.cx/2023/03/12/hackerrank-sql-basic-select/>
VANCOUVER
SpiffyEight77 | Sciencx - » HackerRank SQL Basic Select. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/12/hackerrank-sql-basic-select/
CHICAGO
" » HackerRank SQL Basic Select." SpiffyEight77 | Sciencx - Accessed . https://www.scien.cx/2023/03/12/hackerrank-sql-basic-select/
IEEE
" » HackerRank SQL Basic Select." SpiffyEight77 | Sciencx [Online]. Available: https://www.scien.cx/2023/03/12/hackerrank-sql-basic-select/. [Accessed: ]
rf:citation
» HackerRank SQL Basic Select | SpiffyEight77 | Sciencx | https://www.scien.cx/2023/03/12/hackerrank-sql-basic-select/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.