Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet Code] 1280. Students and Examinations

์ธ” 2023. 10. 17. 14:29

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โœ”๋ฌธ์ œ

https://leetcode.com/problems/students-and-examinations/description/

 

Students and Examinations - LeetCode

Can you solve this real interview question? Students and Examinations - Table: Students +---------------+---------+ | Column Name | Type | +---------------+---------+ | student_id | int | | student_name | varchar | +---------------+---------+ student_id is

leetcode.com

 

Table : Students

Table : Subjects

Table : Examinations

 

Write a solution to find the number of times each student attended each exam.

Return the result table ordered by student_id and subject_name.


โœ” ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

 

ํ•™์ƒ ์ •๋ณด(student_id, student_name)

๊ณผ๋ชฉ ์ •๋ณด(subject_name)

ํ•™์ƒ๋“ค์˜ ์‹œํ—˜ ์‘์‹œ ์ •๋ณด(student_id, subject_name) ๋ฅผ ์กฐ์ธํ•ด์„œ '๋ชจ๋“  ํ•™์ƒ๋“ค'์˜ ์‹œํ—˜ ์‘์‹œ ํšŸ์ˆ˜(์‘์‹œ ์ด๋ ฅ็„ก)๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

์กฐ์ธํ•  ๋•Œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๊ฐ ํ…Œ์ด๋ธ” ๊ฐ’ ๋ฒ”์œ„๋ฅผ ์ž˜ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

(๋ฌธ์ œ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฐ ํ…Œ์ด๋ธ”์˜ primary key๋ฅผ ์ž˜ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์กฐ์ธ ๊ด€๊ณ„๋ฅผ ํ˜•์„ฑํ•  ๋•Œ ๋„์›€์ด ๋๋‹ค.)

 

์˜ˆ)

Students ํ…Œ์ด๋ธ”์˜ ํ•™์ƒ์ˆ˜๋Š” Examinations ์˜ ํ•™์ƒ์ˆ˜๋ณด๋‹ค ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค.(=์‹œํ—˜ ์‘์‹œ ์ด๋ ฅ์ด ์—†๋Š” ํ•™์ƒ ์กด์žฌ)

Subjects ํ…Œ์ด๋ธ”์˜ ๊ณผ๋ชฉ์ข…๋ฅ˜๋Š” Examinations  ์—์„œ ๊ฐ ํ•™์ƒ์ด ์‘์‹œํ•œ ๊ณผ๋ชฉ์ˆ˜๋ณด๋‹ค ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค. (=์‹œํ—˜ ๊ณผ๋ชฉ์ค‘ ์‘์‹œ ์•ˆํ•œ ๊ณผ๋ชฉ์ด ์žˆ๋Š” ํ•™์ƒ ์กด์žฌ)

 

์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด์„œ ํ•™์ƒ, ์‹œํ—˜๊ณผ๋ชฉ, ์‘์‹œ์ด๋ ฅ์ด ๋ˆ„๋ฝ๋˜์ง€ ์•Š๋„๋ก ์•„๋ž˜์™€ ๊ฐ™์ด ์กฐ์ธํ–ˆ๋‹ค.

 SELECT stu.student_id, stu.student_name, sub.subject_name,
        COUNT(exam.subject_name) AS attended_exams
  FROM Subjects sub
  INNER JOIN Students stu
  LEFT JOIN Examinations exam
  ON stu.student_id = exam.student_id
    AND sub.subject_name = exam.subject_name
  GROUP BY 1, 3
  ORDER BY 1, 3;

 

๐Ÿ™‹‍โ™€๏ธ ๋ฌธ์ œ ํ’€์ด์™€ mysql ์ฟผ๋ฆฌ ํšจ์œจ์„ฑ ๋†’์ด๊ธฐ ๋“ฑ ํฌ์ŠคํŒ…์— ๋Œ€ํ•œ ์˜๊ฒฌ ๋ฐ ๊ธฐ์ˆ  ์กฐ์–ธ ๋Œ“๊ธ€ ๋ชจ๋‘ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.

'Growth ๐ŸŒณ > Practice ๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leet Code] 1407. Top Travellers  (1) 2023.10.20
[Leet Code] 1251. Average Selling Price  (0) 2023.10.18
[Leet Code] 1965. Employees With Missing Information  (0) 2023.10.12
[Leet code] 1978. Employees Whose Manager Left the Company  (0) 2023.10.11
[Leet code] 1084. Sales Analysis III  (0) 2023.10.10