HackerRank - Top Earners,Weather Observation Station 2, 13, 14, 15
Top Earners 📝 ⭐️
https://www.hackerrank.com/challenges/earnings-of-employees/problem?isFullScreen=true
➡️ employee 테이블
- salary * months 의 최댓값과 이 최댓값을 가진 사람 수 구하기
참고 1) 필드명 alias 할 경우 HAVING은 alias을 사용하여야 한다, GROUP BY 안에는 alias 필드명 사용해도 안사용해도 무관(MYSQL 기준), ORACLE에서는 GROUPBY alias 사용하면 안된다(salary*months) ⭐️
참고 2) 상위 뽑기 할 때, MYSQL - LIMT 사용, ORACLE - rownum ⭐️
We define an employee's total earnings to be their monthly 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 space-separated integers.
Input Format
The Employee table containing employee data for a company is described as follows:
where employee_id is an employee's ID number, name is their name, monthsis the total number of months they've been working for the company, and salary is the their monthly salary.
Sample Input
Sample Output
69952 1
Explanation
The table and earnings data is depicted in the following diagram:
The maximum earnings value is . The only employee with earnings is Kimberly, so we print the maximum earnings value () and a count of the number of employees who have earned (which is ) as two space-separated values.
--MYSQL
SELECT salary*months as total,COUNT(name)
FROM employee
GROUP BY (total)
ORDER BY total DESC
LIMIT 1;
--ORACLE
SELECT *
FROM
(
SELECT salary*months as total,COUNT(name)
FROM employee
GROUP BY (salary*months)
ORDER BY total DESC
)
WHERE rownum<=1;
SELECT salary*months as total,COUNT(*)
FROM employee
GROUP BY (total)
HAVING total = (SELECT MAX(salary*months) FROM employee);
Weather Observation Station 2📝
https://www.hackerrank.com/challenges/weather-observation-station-2/problem?isFullScreen=true
➡️ station 테이블
- lat_n,long_w의 합을 구하기(단, 소수점 세자리에서 반올림) => SUM,ROUND 함수 이용하기
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.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Output Format
Your results must be in the form:
lat lon
where is the sum of all values in LAT_N and is the sum of all values in LONG_W. Both results must be rounded to a scale of decimal places.
SELECT ROUND(SUM(lat_n),2),ROUND(SUM(long_w),2) FROM station;
Weather Observation Station 13📝
https://www.hackerrank.com/challenges/weather-observation-station-13/problem?isFullScreen=true
➡️ station 테이블
- 38.7880보다 크고 137.2345보다 작은 lat_n의 합 구하기(단, 소수점 5자리에서 버림) => SUM,TRUNCATE 함수 이용
참고 ) 버림은 TRUNCATE ⭐️
Query the sum of Northern Latitudes (LAT_N) from STATIONhaving values greater than 38.7880 and less than 137.2345. Truncate your answer to 4 decimal places.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
SELECT TRUNCATE(SUM(lat_n),4)
FROM station
WHERE lat_n>38.7880 AND lat_n<137.2345;
Weather Observation Station 14📝
https://www.hackerrank.com/challenges/weather-observation-station-14/problem?isFullScreen=true
➡️ station 테이블
- 137.2345보다 작은 lat_n의 최댓값 구하기(단, 소수점 5자리에서 버림) => MAX , TRUNCATE 함수 이용
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.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
SELECT TRUNCATE(MAX(lat_n),4)
FROM station
WHERE lat_n<137.2345;
Weather Observation Station 15📝
https://www.hackerrank.com/challenges/weather-observation-station-15/problem?isFullScreen=true
➡️ station 테이블
- lat_n이 137.2345보다 작은 값을 큰 순서대로 나열
- 나열한 것 중에 lat_n이 가장 큰 값의 lat_n 값 구하기( 단, 소수점 다섯번째에서 반올림)
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 decimal places.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
SELECT ROUND(LONG_W, 4)
FROM STATION
WHERE LAT_N < 137.2345
ORDER BY LAT_N DESC LIMIT 1;
SELECT ROUND(LONG_W, 4)
FROM STATION
WHERE lat_n = (SELECT MAX(lat_n) FROM station WHERE lat_n<137.2345);