HAZEL

[SQL : Leetcode] 595. Big Countries / 620. Not Boring Movies : 짝수, 홀수 본문

DATA ANALYSIS/SQL

[SQL : Leetcode] 595. Big Countries / 620. Not Boring Movies : 짝수, 홀수

Rmsid01 2021. 2. 23. 21:51

1] 595. Big Countries


>> 문제

There is a table World

+-----------------+------------+------------+--------------+---------------+
| name            | continent  | area       | population   | gdp           |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |
| Albania         | Europe     | 28748      | 2831741      | 12960000      |
| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |
| Andorra         | Europe     | 468        | 78115        | 3712000       |
| Angola          | Africa     | 1246700    | 20609294     | 100990000     |
+-----------------+------------+------------+--------------+---------------+

A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.

Write a SQL solution to output big countries' name, population and area.

For example, according to the above table, we should output:

+--------------+-------------+--------------+
| name         | population  | area         |
+--------------+-------------+--------------+
| Afghanistan  | 25500100    | 652230       |
| Algeria      | 37100000    | 2381741      |
+--------------+-------------+--------------+

 

>> 문제 해결 코드

Select name, population, area
from World
where (  area > 3000000 ) or ( population > 25000000 )

※ where 절을 두개로 묶는 것 

 

Leetcode 595. Big Countries: https://leetcode.com/problems/big-countries/

 

Big Countries - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

2] 620. Not Boring Movies


>> 문제

X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descriptions.

Please write a SQL query to output movies with an odd numbered ID and a description that is not 'boring'. Order the result by rating.

 

For example, table cinema:

+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   1     | War       |   great 3D   |   8.9     |
|   2     | Science   |   fiction    |   8.5     |
|   3     | irish     |   boring     |   6.2     |
|   4     | Ice song  |   Fantacy    |   8.6     |
|   5     | House card|   Interesting|   9.1     |
+---------+-----------+--------------+-----------+

For the example above, the output should be:

+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   5     | House card|   Interesting|   9.1     |
|   1     | War       |   great 3D   |   8.9     |
+---------+-----------+--------------+-----------+

 

 

>> 문제 해결 코드

select id , movie, description, rating
from cinema
where description != 'boring' and mod(id,2) = 1 
order by rating desc

※ 짝수, 홀수 번호만 불러오는 방법 

: where MOD( ID, 2 ) = 0   : 짝수  /   where MOD( ID, 2 ) = 1   : 홀수  

 

 

Leetcode 620. Not Boring Movies: https://leetcode.com/problems/not-boring-movies/

 

Not Boring Movies - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com