스터디/SQL

[SQL 연습 문제] 코딩 테스트 대비 해커랭크 easy 문제 풀이(4)

skyggg3 2022. 7. 26. 12:39

사용한 키워드

- SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT, MAX, MIN, SUM, AVG, ROUND, FLOOR, TRUNCATE, INNER JOIN, SET, REPEAT

 

 

1. Weather Observation Station 13

 

Weather Observation Station 13 | HackerRank

Query the sum of Northern Latitudes having values greater than 38.7880 and less than 137.2345, truncated to 4 decimal places.

www.hackerrank.com

SELECT TRUNCATE(SUM(lat_n),4)
FROM station
WHERE lat_n > 38.7880
AND lat_n <137.2345;

숫자 함수

TRUNCATE(숫자, 자릿수): 자릿수를 기준으로 숫자를 버림

ex) TRUNCATE(13.56789, 2) -> 13.56

 

 

2. Weather Observation Station 14

 

Weather Observation Station 14 | HackerRank

Query the greatest value of the Northern Latitudes from STATION that are under 137.2345 and truncated to 4 decimal places.

www.hackerrank.com

SELECT TRUNCATE(MAX(lat_n),4)
FROM station
WHERE lat_n <137.2345;

 

 

3. Weather Observation Station 15

 

Weather Observation Station 15 | HackerRank

Query the Western Longitude for the largest Northern Latitude under 137.2345, rounded to 4 decimal places.

www.hackerrank.com

SELECT ROUND(long_w,4)
FROM station
WHERE lat_n <137.2345
ORDER BY lat_n DESC
LIMIT 1;

 

 

4. Weather Observation Station 16

 

Weather Observation Station 16 | HackerRank

Query the smallest of STATION's Northern Latitudes that is greater than 38.7780, and round to 4 decimal places

www.hackerrank.com

SELECT ROUND(MIN(lat_n), 4)
FROM station
WHERE lat_n > 38.7780;

 

 

5. Weather Observation Station 17

 

Weather Observation Station 17 | HackerRank

Query the Western Longitude for the smallest value of the Northern Latitudes greater than 38.7780 in STATION and round to 4 decimal places.

www.hackerrank.com

SELECT ROUND(long_w,4)
FROM station
WHERE lat_n > 38.7780
ORDER BY lat_n
LIMIT 1;

 

 

6. Population Census

 

Population Census | HackerRank

Query the sum of the populations of all cities on the continent 'Asia'.

www.hackerrank.com

SELECT SUM(city.population)
FROM city
    INNER JOIN country ON CITY.CountryCode = COUNTRY.Code
WHERE country.Continent = 'Asia';

 

 

7. African Cities

 

African Cities | HackerRank

Query the names of all cities on the continent 'Africa'.

www.hackerrank.com

SELECT city.name
FROM city
    INNER JOIN country ON CITY.CountryCode = COUNTRY.Code
WHERE country.Continent = 'Africa';

 

 

8. Average Population of Each Continent

 

Average Population of Each Continent | HackerRank

Query the names of all continents and their respective city populations, rounded down to the nearest integer.

www.hackerrank.com

SELECT country.continent, FLOOR(AVG(city.population))
FROM city
    INNER JOIN country ON CITY.CountryCode = COUNTRY.Code
GROUP BY country.continent;

 

 

9. Draw The Triangle 1

 

Draw The Triangle 1 | HackerRank

Draw the triangle pattern using asterisks.

www.hackerrank.com

SET @number = 21;
SELECT REPEAT('* ', @number := @number - 1) 
FROM information_schema.tables 
LIMIT 20;

 

 

10. Draw The Triangle 2

 

Draw The Triangle 2 | HackerRank

Draw the triangle pattern using asterisks.

www.hackerrank.com

SET @number = 0;
SELECT REPEAT('* ', @number := @number + 1) 
FROM information_schema.tables 
LIMIT 20;