티스토리 뷰

728x90

Revising Aggregations - The Count Function

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

Input Format

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.

Input Format

SELECT SUM(POPULATION)
FROM CITY 
WHERE DISTRICT = 'California';

 

Revising Aggregations - Averages

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

Input Format

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.

Input Format

SELECT ROUND(AVG(POPULATION),0)	--소수점 0자리까지 생성
FROM CITY;

SELECT AVG(POPULATION)
    , ROUND(AVG(POPULATION),1)	-- 소수점 1자리까지 생성
    , ROUND(AVG(POPULATION),2)	-- 소수점 2자리까지 생성
FROM CITY;

 

Japan Population

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

Input Format

SELECT SUM(POPULATION)
FROM CITY
WHERE COUNTRYCODE = 'JPN';

 

Population Density Difference

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

Input Format

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

Input Format

The EMPLOYEES table is described as follows:

Note: Salary is per month.

Constraints

.

Sample Input

Sample Output

2061

Explanation

The table below shows the salaries without zeros as they were entered by Samantha:

Samantha computes an average salary of 98. The actual average salary is 2159.

The resulting error between the two calculations is 2159.00-98.00 = 2061.00 . Since it is equal to the integer , it does not get rounded up.

/*round up = ceil*/
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.

/*MS-SQL*/
DECLARE @max_earnings INT 
    , @count_max_earnings INT 

SELECT @max_earnings = MAX(months*salary)
FROM Employee;

SELECT @count_max_earnings = COUNT(*) 
FROM Employee
WHERE months*salary = @max_earnings;

SELECT @max_earnings, @count_max_earnings

/*MySQL*/
SET @max_earnings = (
        SELECT MAX(months*salary) 
        FROM Employee
    );
    
SET @count_max_earnings = (
    SELECT COUNT(*) 
    FROM Employee 
    WHERE months*salary = @max_earnings
);

SELECT @max_earnings, @count_max_earnings;
728x90
LIST
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함