zacharyrperales / HackerRank_SQL_Solutions

HackerRank SQL Challenge Solutions

Home Page:https://www.hackerrank.com/domains/sql

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

License HitCount Languages Top Language Last Commit

LeetHub - Automatically sync your code to GitHub.
HackerRank SQL Challenge Solutions

Index

Basic Select

Revising the Select Query

The CITY table is described as follows:

Column Type
ID NUMBER
NAME VARCHAR2(17)
COUNTRYCODE VARCHAR2(3)
DISTRICT VARCHAR2(20)
POPULATION Number

Revising the Select Query 1: Easy

Query all columns for all American cities in the CITY table with populations larger than 100000. The COUNTRYCODE for America is USA.

Revising the Select Query 2: Easy

Query the NAME field for all American cities in the CITY table with populations larger than 120000. The COUNTRYCODE for America is USA.

Select All: Easy

Query all columns (attributes) for every row in the CITY table.

Select By Id: Easy

Query all columns for a city in CITY with the ID 1661.

Japanese Cities' Attributes: Easy

Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.

Japanese Cities' Names: Easy

Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.

Weather Observation Station

The STATION table is described as follows:

Column Type
ID NUMBER
CITY VARCHAR2(21)
STATE VARCHAR2(2)
LAT_N NUMBER
LONG_W NUMBER

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

Weather Observation Station 1: Easy

Query a list of CITY and STATE from the STATION table.

Weather Observation Station 2: Easy

Query the following two values from the STATION table:

  1. The sum of all values in LAT_N rounded to a scale of 2 decimal places.
  2. The sum of all values in LONG_W rounded to a scale of 2 decimal places.

Weather Observation Station 3: Easy

Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.

Weather Observation Station 4: Easy

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

Weather Observation Station 5: Easy

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.

Weather Observation Station 6: Easy

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

Weather Observation Station 7: Easy

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

Weather Observation Station 8: Easy

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.

Weather Observation Station 9: Easy

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

Weather Observation Station 10: Easy

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

Weather Observation Station 11: Easy

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.

Weather Observation Station 12: Easy

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

Weather Observation Station 13: Easy

Query the sum of Northern Latitudes (LAT_N) from STATION having values greater than 38.7880 and less than 137.2345. Truncate your answer to 4 decimal places.

Weather Observation Station 14: Easy

Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than 137.2345. Truncate your answer to 4 decimal places.

Weather Observation Station 15: Easy

Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is less than 137.2345. Round your answer to 4 decimal places.

Weather Observation Station 16: Easy

Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7780. Round your answer to 4 decimal places.

Weather Observation Station 17: Easy

Query the Western Longitude (LONG_W) where the smallest Northern Latitude (LAT_N) in STATION is greater than 38.7780. Round your answer to 4 decimal places.

Weather Observation Station 18: Medium

Consider P subscript(1) (a,b) and P subscript(2) (c,d) to be two points on a 2D plane.

  • a happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
  • b happens to equal the minimum value in Western Longitude (LONG_W in STATION).
  • c happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
  • d happens to equal the maximum value in Western Longitude (LONG_W in STATION).

Query the Manhattan Distance between points P subscript(1) and P subscript(2) and round it to a scale of decimal 4 places.

Weather Observation Station 19: Medium

ConsiderP subscript(1) (a,b) and P subscript(2) (c,d) to be two points on a 2D plane where (a,b) are the respective minimum and maximum values of Northern Latitude (LAT_N) and (c,d) are the respective minimum and maximum values of Western Longitude (LONG_W) in STATION.

Query the Euclidean Distance between points P subscript(1) and P subscript(2) and format your answer to display 4 decimal digits.

Weather Observation Station 20: Medium

A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to 4 decimal places.

Higher Than 75 Marks: Easy

Query the Name of any student in STUDENTS who scored higher than 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: The Name column only contains uppercase (A-Z) and lowercase (a-z) letters.

Column Type
ID Integer
Name String
Marks Integer

Employees

Column Type
EMPLOYEE_ID INTEGER
NAME STRING
MONTHS INTEGER
SALARY INTEGER

Employee Names: Easy

Write a query that prints a list of employee names (i.e.: the name attribute) from the Employee table in alphabetical order.

Employee Salaries: Easy

Write a query that prints a list of employee names (i.e.: the name attribute) for employees in Employee having a salary greater than $2000 per month who have been employees for less than 10 months. months. Sort your result by ascending employee_id.

Advanced Select

Type of Triangle: Easy

Column Type
A INTEGER
B INTEGER
C INTEGER

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:

  • Equilateral: It's a triangle with 3 sides of equal length.
  • Isosceles: It's a triangle with 2 sides of equal length.
  • Scalene: It's a triangle with 3 sides of differing lengths.
  • Not A Triangle: The given values of A, B, and C don't form a triangle.

Binary Search Tree: Medium

You are given a table, BST, containing two columns: N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.

Column Type
N INTEGER
P INTEGER

Write a query to find the node type of Binary Tree ordered by the value of the node. Output one of the following for each node:

  • Root: If node is root node.
  • Leaf: If node is leaf node.
  • Inner: If node is neither root nor leaf node.

The PADS: Medium

Generate the following two result sets:

  1. Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).

  2. Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format: There are a total of [occupation_count] [occupation]s. where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically. Note: There will be at least two entries in the table for each type of occupation.

Input Format

The OCCUPATIONS table is described as follows:

Column Type
NAME STRING
OCCUPATION STRING

Aggregation

The CITY table is described as follows:

Column Type
ID NUMBER
NAME VARCHAR2(17)
COUNTRYCODE VARCHAR2(3)
DISTRICT VARCHAR2(20)
POPULATION NUMBER

The EMPLOYEES table is described as follows:

Column Type
ID INTEGER
NAME STRING
SALARY INTEGER

The EMPLOYEE table is described as follows:

Column Type
EMPLOYEE_ID INTEGER
NAME STRING
MONTHS INTEGER
SALARY INTEGER

Revising Aggregations - The Sum Function: Easy

Query the total population of all cities in CITY where District is California.

Revising Aggregations - Averages: Easy

Query the average population for all cities in CITY, rounded down to the nearest integer.

Revising Aggregations - The Count Function: Easy

Query a count of the number of cities in CITY having a Population larger than 100,000.

The Blunder: Easy

Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table, but did not realize her keyboard's 0 key was broken until after completing the calculation. She wants your help finding the difference between her miscalculation (using salaries with any zeros removed), and the actual average salary.

Write a query calculating the amount of error (i.e.: actual - miscalculated average monthly salaries), and round it up to the next integer.

Top Earners: Easy

We define an employee's total earnings to be their monthly salary x months worked, and the maximum total earnings to be the maximum total earnings for any employee in the EMPLOYEE table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as 2 space-separated integers.

Japan Population: Easy

Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.

Average Population: Easy

Query the average population for all cities in CITY, rounded down to the nearest integer.

Population Density Difference: Easy

Query the difference between the maximum and minimum populations in CITY.

Basic Join

Population Census: Easy

Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.

Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

Input Format

The CITY and COUNTRY tables are described respectively as follows:

Field Type
ID NUMBER
NAME VARCHAR2(17)
COUNTRYCODE VARCHAR2(3)
DISTRICT VARCHAR2(20)
POPULATION NUMBER
Field Type
CODE VARCHAR2(3)
NAME VARCHAR2(44)
CONTINENT VARCHAR2(13)
REGION VARCHAR2(25)
SURFACEAREA NUMBER
INDEPYEAR VARCHAR2(5)
POPULATION NUMBER
LIFEEXPECTANCY VARCHAR2(4)
GNP NUMBER
GNPOLD VARCHAR2(9)
LOCALNAME VARCHAR2(44)
GOVERNMENTFROM VARCHAR2(44)
HEADOFSTATE VARCHAR2(32)
CAPITAL VARCHAR2(4)
CODE2 VARCHAR2(2)

Challenges: Medium

Julia asked her students to create some coding challenges. Write a query to print the hacker_id, name, and the total number of challenges created by each student. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges, then sort the result by hacker_id. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.

Input Format

The following tables contain challenge data:

  • Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
Column Type
hacker_id Integer
name String
  • Challenges: The challenge_id is the id of the challenge, and hacker_id is the id of the student who created the challenge.
Column Type
challenge_id Integer
hacker_id Integer

Alternative Queries

Draw the Triangle

Draw the Triangle 1: Easy

P(R) represents a pattern drawn by Julia in R rows. The following pattern represents P(5):

* * * * * 
* * * * 
* * * 
* * 
*

Write a query to print the pattern P(20).

Draw the Triangle 2: Easy

P(R) represents a pattern drawn by Julia in R rows. The following pattern represents P(5):

* 
* * 
* * * 
* * * * 
* * * * * 

Write a query to print the pattern P(20).

Print Prime Numbers: Medium

Write a query to print all prime numbers less than or equal to . Print your result on a single line, and use the ampersand (&) character as your separator (instead of a space).