Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hackerrank Solutions #4

Merged
merged 8 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
solve: 17/17 Aggregation problems added.
  • Loading branch information
faizanxmulla committed Feb 9, 2024
commit d4d616c25c7b09691b3f1bf53f90374c6de2221f
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Query a count of the number of cities in CITY having a Population larger than .

SELECT COUNT(id)
FROM City
WHERE population > 100000
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Query the total population of all cities in CITY where District is California.

SELECT SUM(population)
FROM City
WHERE district='California'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Query the average population of all cities in CITY where District is California.

SELECT AVG(population)
FROM City
WHERE district='California'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Query the average population for all cities in CITY, rounded down to the nearest

SELECT ROUND(AVG(population))
FROM City
5 changes: 5 additions & 0 deletions hackerrank-solutions/03 - Aggregation/05-japan-population.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.

SELECT SUM(population)
FROM City
WHERE COUNTRYCODE='JPN'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Query the difference between the maximum and minimum populations in CITY.

SELECT MAX(population) - MIN(population)
FROM City
8 changes: 8 additions & 0 deletions hackerrank-solutions/03 - Aggregation/07-the-blunder.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Write a query calculating the amount of error (i.e.: average monthly salaries), and round it up to the next integer.

SELECT CEIL(AVG(salary) - AVG(REPLACE(salary, '0', '')))
FROM Employees


-- REMARKS :
-- did not read the question properly and was using ROUND instead of CEIL.
8 changes: 8 additions & 0 deletions hackerrank-solutions/03 - Aggregation/08-top-earners.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- 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 space-separated integers.

SELECT (salary * months) as total_salary,
Count(*)
FROM employee
GROUP BY 1
ORDER BY 1 DESC
LIMIT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Query the following two values from the STATION table:

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


SELECT ROUND(SUM(LAT_N), 2), ROUND(SUM(LONG_W), 2)
FROM Station
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- 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 decimal places.

SELECT ROUND(SUM(LAT_N), 4)
FROM Station
WHERE LAT_N BETWEEN 38.7880 AND 137.2345
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than . Truncate your answer to decimal places.

SELECT ROUND(MAX(LAT_N), 4)
FROM Station
WHERE LAT_N < 137.2345
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than 137.2345 . Truncate your answer to decimal places.

SELECT ROUND(LONG_W, 4)
FROM Station
WHERE LAT_N < 137.2345
ORDER BY LAT_N DESC
LIMIT 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7780. Round your answer to decimal places.


SELECT ROUND(LAT_N, 4)
FROM Station
WHERE LAT_N > 38.7780
ORDER BY LAT_N
LIMIT 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Query the Western Longitude (LONG_W)where the smallest Northern Latitude (LAT_N) in STATION is greater than 38.7780. Round your answer to decimal places.


SELECT ROUND(LONG_W, 4)
FROM Station
WHERE LAT_N > 38.7780
ORDER BY LAT_N
LIMIT 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Query the Manhattan Distance between points P1 and P2 and round it to a scale of 4 decimal places.

SELECT Round(Abs(Min(lat_n)-Max(lat_n))
+ Abs(Min(long_w)-Max(long_w)), 4)
FROM station;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Query the Euclidean Distance between points P1 and P2 and round it to a scale of 4 decimal places.

SELECT Round(Sqrt(Power(Max(lat_n) - Min(lat_n), 2)
+ Power(Max(long_w) - Min(long_w), 2)), 4)
FROM station;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- 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 decimal places.


SELECT ROUND(MEDIAN(LAT_N), 4)
FROM Station;