2021. 1. 5. 22:12ㆍToday I Learned.../MySQL
Revising Aggregations - The Count Function
Query a count of the number of cities in CITY having a Population larger than 100,000.
Input Format
The CITY table is described as follows:
select count(*)
from city
where population > 100000
Revising Aggregations - The Sum Function
Query the total population of all cities in CITY where District is California.
select sum(population)
from city
where district = 'california'
Revising Aggregations - Averages
Query the average population of all cities in CITY where District is California.
select avg(population)
from city
where district = 'california'
Average Population
Query the average population for all cities in CITY, rounded down to the nearest integer.
select floor(avg(population))
from city
round() | 반올림 |
ceil() | 올림 |
floor() | 내림 |
Japan Population
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'
Population Density Difference
Query the difference between the maximum and minimum populations in CITY.
select max(population)-min(population)
from city
The Blunder
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 zeroes 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.
Input Format
The EMPLOYEES table is described as follows:
Note: Salary is measured in dollars per month and its value is < 10^5.
Sample Input
Sample Output
2061
Explanation
The table below shows the salaries without zeroes as they were entered by Samantha:
Samantha computes an average salary of 98.00. The actual average salary is 2159.00.
The resulting error between the two calculations is 2159.00 - 98.00 = 2061.00 which, when rounded to the next integer, is 2061.
select ceil(avg(salary)-avg(replace(salary, 0, '')))
from employees
Top Earners
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.
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, months is 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 69952. The only employee with earnings = 69952 is Kimberly, so we print the maximum earnings value (69952) and a count of the number of employees who have earned $69952 (which is 1) as two space-separated values.
select salary*months earnings, count(*)
from employee
group by earnings
order by 1 desc
limit 1
문제출처: 해커랭크
'Today I Learned... > MySQL' 카테고리의 다른 글
[HackerRank MySQL] INNER JOIN 문제풀기 (Basic) (0) | 2021.01.05 |
---|---|
[HackerRank MySQL] Weather Observation Station 2, 13-17 - ROUND & TRUNCATE (Basic) (0) | 2021.01.05 |
[HackerRank MySQL] ORDER BY 문제풀기 (Basic) (0) | 2021.01.05 |
[HackerRank MySQL] Weather Observation Station 1, 3-12 + REGEXP (Basic) & MOD() Function (0) | 2021.01.05 |
[HackerRank MySQL] SELECT, WHERE 문제풀기 (Basic) (0) | 2021.01.05 |