SQL Interview — 1

HackerRank Questions

Learning Zone
2 min readFeb 21, 2019

Problem 1 :

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.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Sample Input

Let’s say that CITY only has four entries: DEF, ABC, PQRS and WXY

Sample Output

ABC 3
PQRS 4

Explanation

When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY, with the respective lengths and . The longest-named city is obviously PQRS, but there are options for shortest-named city; we choose ABC, because it comes first alphabetically.

Note
You can write two separate queries to get the desired output. It need not be a single query.

Declare @Small INT
Declare @Large INT
SELECT @Small = MIN(LEN(CITY)) FROM STATION
SELECT @Large = MAX(LEN(CITY)) FROM STATION
SELECT TOP 1 CITY AS SmallestCityName,LEN(CITY) AS Minimumlength FROM STATION WHERE LEN(CITY) = @Small ORDER BY CITY ASC
SELECT TOP 1 CITY AS LargestCityName,LEN(CITY) AS MaximumLength FROM STATION WHERE LEN(CITY) = @Large ORDER BY CITY ASC

Problem 2 :

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.

SELECT DISTINCT CITY FROM STATION WHERE SUBSTRING(CITY,1,1) in('A','E','I','O','U');

Problem 3 :

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

SELECT DISTINCT CITY FROM STATION WHERE SUBSTRING(REVERSE(CITY),1,1) in('A','E','I','O','U');

Problem 4 :

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.

SELECT DISTINCT CITY FROM STATION WHERE SUBSTRING(CITY,1,1) in('A','E','I','O','U') AND SUBSTRING(REVERSE(CITY),1,1) in('A','E','I','O','U');

Problem 5 :

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

SELECT DISTINCT CITY FROM STATION WHERE SUBSTRING(CITY,1,1) NOT IN ('A','E','I','O','U')

Problem 6 :

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.

SELECT DISTINCT CITY FROM STATION WHERE SUBSTRING(CITY,1,1) NOT in('A','E','I','O','U') OR SUBSTRING(REVERSE(CITY),1,1) NOT in('A','E','I','O','U');

Problem 7 :

Let N be the number of CITY entries in STATION, and let N’ be the number of distinct CITY names in STATION; query the value of N-N’ from STATION. In other words, find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

SELECT COUNT(CITY)- COUNT(DISTINCT CITY) FROM STATION

--

--