3월 5주차

canugo·2023년 3월 21일
0

해커랭크

목록 보기
11/12

Revising Aggregations - The Count Function

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:

문제풀이 & 해석

인구가 100,000보다 큰 CITY의 도시 수를 쿼리합니다.

SELECT COUNT(*) ➡ 수를 세어라
FROM CITY ➡ CITY테이블에서 
WHERE POPULATION > 100000 ➡ 인구가 100,000초과인 

Revising Aggregations - The Sum Function

Revising Aggregations - The Sum Function 문제

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

문제해석 & 풀이

지역구가 캘리포니아인 CITY의 모든 도시의 총 인구를 쿼리합니다.

SELECT SUM(POPULATION) ➡ 인구의 합
FROM CITY ➡ CITY테이블로 부터
WHERE DISTRICT = 'California' ➡ DISTRICT가 'california'

Revising Aggregations - Averages

Revising Aggregations - Averages 문제

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

문제풀이 & 해석

지역구가 캘리포니아인 CITY의 모든 도시의 평균 인구를 쿼리합니다.

SELECT AVG(POPULATION) ➡ 인구의 평균
FROM CITY ➡ CITY 테이블로 부터
WHERE DISTRICT = 'California' ➡ 지역구가 'California'

Average Population

Average Population 문제

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

문제풀이 & 해석

CITY에 있는 모든 도시의 평균 인구를 가장 가까운 정수로 반올림하여 쿼리합니다.

SELECT ROUND(AVG(POPULATION)) ➡ 반올림 한 인구의 평균
FROM CITY ➡ CITY테이블에서 

Japan Population

Japan Population 문제

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

문제풀이 & 해석

CITY에 있는 모든 일본 도시의 인구 합계를 쿼리합니다. 일본의 국가 코드는 일본입니다.

SELECT SUM(POPULATION) ➡ 인구의 합계 
FROM CITY WHERE COUNTRYCODE = 'JPN' ➡ COUNTRYCODE가 JPN인

Population Density Difference

Population Density Difference 문제

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

문제풀이 & 해석

POPULATION의 최댓값과 최솟값의 차이

SELECT MAX(POPULATION) - MIN(POPULATION) FROM CITY 

The Blunder

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 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.

문제풀이 & 해석

사만다는 직원 테이블에 있는 모든 직원들의 평균 월급을 계산하는 임무를 맡았지만 계산을 마치고 나서야 키보드의 키가 부러진 것을 깨달았다.
그녀는 자신의 계산 착오(0이 제거된 급여 사용)와
실제 평균 급여 사이의 차이를 찾는 데 도움이 필요하다고 합니다.

오류 금액(즉, 실제 잘못 계산된 평균 월 급여)을 계산하는 쿼리를 작성하고 다음 정수로 반올림합니다.

  1. 모든 직원의 월급 평균 구하기
  2. 0이 있을 때와 없을 때의 차이
  3. 올림해서 보여줘야함 ➡ CEIL사용
  4. 원본데이터에서 0이 없는 데이터를 뺀 후 평균 값을 구한다음 CEIL()사용
SELECT CEIL(AVG(salary) - AVG(replace(salary,0,'')))
FROM employees

Top Earners

Top Earners 문제

We define an employee's total earnings to be their monthly
salary * 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 2space-separated integers.

Input Format

The Employee table containing employee data for a company is described as follows:

문제풀이 & 해석

우리는 직원의 총 수입을 매월로 정의한다
급여 * 근무 개월 수,
직원 표에 있는 직원의 최대 총 소득이 될 수 있는 최대 총 소득.
쿼리를 작성하여 모든 직원의 최대 총 수입과 최대 총 수입을 가진 직원의 총 수를 찾습니다.
그런 다음 이 값을 2개의 공백으로 구분된 정수로 인쇄합니다.

SELECT SALARY * MONTHS AS ANSWER, ➡ ANSWER 별칭 사용, MONTH * SALARY 
COUNT(*)FROM EMPLOYEE ➡ EMPLOYEE 테이블로 부터
GROUP BY ANSWER ➡ ANSWER로 묶어준다 
ORDER BY ANSWER DESC ➡ 내림차순 정렬
LIMIT 1 ➡ 최상위 값 추출

0개의 댓글