๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ๋ฌธ์
https://leetcode.com/problems/top-travellers/description/
Top Travellers - LeetCode
Can you solve this real interview question? Top Travellers - Table: Users +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+ id is the column with unique values for thi
leetcode.com
Table : Users
Table : Rides
Write a solution to report the distance traveled by each user.
Return the result table ordered by travelled_distance in descending order, if two or more users traveled the same distance, order them by their name in ascending order.
…
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
์ฌํ์๋ณ๋ก ์ด๋ํ ๊ฑฐ๋ฆฌ๋ฅผ ์ถ๋ ฅํ๋๋ฐ
์ฌํ๊ฑฐ๋ฆฌ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ถ๋ ฅํ๋, ๋ง์ฝ ๋๋ช ์ด์์ ์ ์ ๊ฐ ๊ฐ์ ๊ฑฐ๋ฆฌ๋ฅผ ์ฌํํ๋ค๋ฉด ์ด๋ฆ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ผ ํ๋ค.
Users ํ ์ด๋ธ๊ณผ Rides ํ ์ด๋ธ์ ์กฐ์ธํ ๋ ์ฃผ์ํด์ผ ํ ์ ์
์กฐ์ธ ์กฐ๊ฑด ์ปฌ๋ผ์ด Users๋ id ์ด๊ณ Rides๋ user_id ๋ผ๋ ์ ์ด๋ค.
๋ํ ๊ฒฐ๊ณผ ์ ์ถ์ค์ ์์ด๋๊ฐ ๋ค๋ฅธ ๋๋ช ์ด์ธ์ ๊ฒฝ์ฐ๋ ๊ณ ๋ คํด์ผ ํ๋ค๋ ๊ฒ์ ๋ฐ๊ฒฌํ๋ค.
* ๋๋ช ์ด์ธ ๋ฐ์ดํฐ๋ก ์ธํด ๊ทธ๋ฃนํ์ name์ผ๋ก ํ ๊ฒฝ์ฐ, distance ๊ฐ์ด ํ์ฌ๋์ผ๋ก ๋ฌถ์ฌ๋ฒ๋ฆฌ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฏ๋ก
๊ทธ๋ฃน์ id๊ธฐ์ค์ผ๋ก ์ ์ฉํด์ผ ํ๋ค.
SELECT U.name
,IFNULL(SUM(R.distance), 0) AS travelled_distance
FROM Users U
LEFT JOIN Rides R
ON U.id = R.user_id
GROUP BY U.id
ORDER BY 2 DESC, 1;
๐โ๏ธ ๋ฌธ์ ํ์ด์ mysql ์ฟผ๋ฆฌ ํจ์จ์ฑ ๋์ด๊ธฐ ๋ฑ ํฌ์คํ ์ ๋ํ ์๊ฒฌ ๋ฐ ๊ธฐ์ ์กฐ์ธ ๋๊ธ ๋ชจ๋ ํ์ํฉ๋๋ค.
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leet Code] 1633. Percentage of Users Attended a Contest (0) | 2023.10.23 |
---|---|
[Leet Code] 1148. Article Views I (0) | 2023.10.21 |
[Leet Code] 1251. Average Selling Price (0) | 2023.10.18 |
[Leet Code] 1280. Students and Examinations (0) | 2023.10.17 |
[Leet Code] 1965. Employees With Missing Information (0) | 2023.10.12 |