On the journey of

[Leetcode]1683.Invalid Tweets/577. Employee Bonus/1075. Project Employees 1/596. Classes More than 5 students 본문

코딩테스트/SQL

[Leetcode]1683.Invalid Tweets/577. Employee Bonus/1075. Project Employees 1/596. Classes More than 5 students

dlrpskdi 2023. 9. 9. 22:53

1683. Invalid Tweets

https://leetcode.com/problems/invalid-tweets/

 

Invalid Tweets - LeetCode

Can you solve this real interview question? Invalid Tweets - Table: Tweets +----------------+---------+ | Column Name | Type | +----------------+---------+ | tweet_id | int | | content | varchar | +----------------+---------+ tweet_id is the primary key (c

leetcode.com

Q.

Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.

Return the result table in any order. The result format is in the following example.

문제 자체도 그리 길지 않다. 테이블도 하나뿐이고 조건도 하나라 어렵지 않게 작성할 수 있었는 데다 조건이 Length() 함수를 사용해야 하는 건데, Length (content) 이렇게 쓰는 것만 알았다면야...10초컷 가능한 문제였던 듯!

SELECT tweet_id
FROM Tweets
WHERE LENGTH(content) > 15

결과도 good :) 다만 SQL은 도대체 어떻게 하면 이른바 '클린코드'를 작성할 수 있는지 잘 모르겠다 ... beats 45.75%는 좀 아쉬운디

 

577. Employee Bonus

https://leetcode.com/problems/employee-bonus/

 

Employee Bonus - LeetCode

Can you solve this real interview question? Employee Bonus - Table: Employee +-------------+---------+ | Column Name | Type | +-------------+---------+ | empId | int | | name | varchar | | supervisor | int | | salary | int | +-------------+---------+ empId

leetcode.com

Q.

Write a solution to report the name and bonus amount of each employee with a bonus less than 1000.

Return the result table in any order. The result format is in the following example.

A. 일단 결과를 위 example처럼 name과 bonus를 한 테이블에 담으려면 join 기능이 필요하다. 그리고 순서가 name | bonus 순서이므로 left join을 사용하였다 :)

# Write your MySQL query statement below
SELECT name, Bonus.bonus
FROM Employee
LEFT JOIN Bonus
ON Employee.empId = Bonus.empId
WHERE Bonus.bonus < 1000 OR Bonus.bonus IS NULL

1075. Project Employees 1

https://leetcode.com/problems/project-employees-i/

 

Project Employees I - LeetCode

Can you solve this real interview question? Project Employees I - Table: Project +-------------+---------+ | Column Name | Type | +-------------+---------+ | project_id | int | | employee_id | int | +-------------+---------+ (project_id, employee_id) is th

leetcode.com

Write an SQL query that reports the average experience years of all the employees for each project, rounded to 2 digits.

Return the result table in any order. The query result format is in the following example

A. 일단 결과값을 소수 2번째 자리까지 나타내기 위해 Round(a,b)에서 b=2로 해줘야 함을 알 수 있다. 그리고 여기서 중요한 건 ON절이다 :) ON절은 사실 Where과 유사한 기능을 한다. 여기서도 join을 하는 것까진 똑같다. 그러나

where절은 두 테이블을 조인한 후 일부 결과를 추출하는 반면 on절은 조인 전 테이블 중 일부만을 추출한 후 join시킨다!

SELECT             p.project_id,
                   ROUND(AVG(e.experience_years), 2) AS average_years
FROM               Project AS p
INNER JOIN         Employee AS e
ON                 p.employee_id = e.employee_id
GROUP BY           p.project_id

 

사실 Where 절 갖고 좀 애먹었어서 찾아보다가 알게 된 사실이라 결과가 더 눈물남 ....🤣🥺

On절 .. 절대 안 잊어먹어야

596. Classes More than 5 students

https://leetcode.com/problems/classes-more-than-5-students/

 

Classes More Than 5 Students - LeetCode

Can you solve this real interview question? Classes More Than 5 Students - Table: Courses +-------------+---------+ | Column Name | Type | +-------------+---------+ | student | varchar | | class | varchar | +-------------+---------+ (student, class) is the

leetcode.com

Q. Write a solution to find all the classes that have at least five students. Return the result table in any order.

The result format is in the following example.

A. Having 조건절만 잘 쓸 수 있었다면 사실 어려운 문제는 아니었다고 생각한다. Group by가 같이 추가되었지만(class별로 나타내야 하니까) 이 한 줄이 어렵진 않으니까 ... 중복 없이 : Distinct, count를 활용한 학생 수 조건을 Having으로 묶는 게 젤 까다롭다면 까다로운 부분이었던 듯 ?

select class 
from courses 
group by class 
having count(distinct student) >= 5

뭐 결과 깔쌈해서 좋다 !