working_helen
[코테 연습] Leetcode 코딩테스트 연습 MySQL Easy (3) 본문
Leetcode 코딩테스트 연습 MySQL Easy
1633. Percentage of Users Attended a Contest
: find the percentage of the users registered in each contest rounded to two decimals
풀이 1
- select 문 내에서 서브쿼리 사용
select contest_id, round(count(contest_id)/(select count(*) from Users)*100, 2) as percentage
from Register
group by contest_id
order by 2 desc, 1
풀이 2 > count가 0인 조합까지 포함해서 모든 가능한 조합별 개수 세기
- cross join으로 모든 가능한 조합이 포함된 테이블 생성
- left outer join으로 값이 존재하는 조합에 대해서만 열이 추가되도록 (USING 말고 ON절 사용해야 함)
select tmp.contest_id, round(count(r.user_id)/count(u.user_id)*100, 2) as percentage
from Users u cross join (select distinct contest_id from Register) tmp
left outer join Register r on u.user_id = r.user_id and tmp.contest_id = r.contest_id
group by tmp.contest_id
order by 2 desc, 1
1667. Fix Names in a Table
: Write a solution to fix the names so that only the first character is uppercase and the rest are lowercase. Return the result table ordered by user_id
풀이
- SUBSTR(대상, m, n) : 대상 문자열의 m 위치에서부터 n개의 문자열 추출, n 생략시 끝까지 추출
- || : Oracle에서는 문자열 연결 연산자
select user_id, (upper(substr(name,1,1)) || lower(substr(name,2))) as name
from Users
order by 1
1873. Calculate Special Bonus
: calculate the bonus of each employee. The bonus of an employee is 100% of their salary if the ID of the employee is an odd number and the employee's name does not start with the character 'M'. The bonus of an employee is 0 otherwise.
풀이 1
- MOD(값1, 값2) : Oracle에서 나머지 연산 시 % 대신 MOD 함수를 사용
- regexp_like(대상, 찾을 문자열) : 찾을 문자열이 포함되어 있는지 true/false 리턴
select employee_id, case
when (mod(employee_id, 2) = 1 and not regexp_like(name, '^M')) then salary
else 0 end as bonus
from Employees
order by 1
풀이 2
- LIKE 연산자 : 문자열 패턴 매칭을 할 때 사용하는 조건절
WHERE 절, HAVING 절, CASE WHEN문 안, IF문 안, JOIN 조건 안, CHECK 제약 조건 등에 사용 가능
select employee_id, case
when (mod(employee_id, 2) = 1 and name not like 'M%') then salary
else 0 end as bonus
from Employees
order by 1
3436. Find Valid Emails
: find all the valid email addresses. It contains exactly one @ symbol and ends with '.com'. The part before the @ symbol contains only alphanumeric characters and underscores. The part after the @ symbol and before .com contains a domain name that contains only letters.
풀이
- [ ] 안에서는 각 특수문자에 '\'를 붙히지 않아도 됨, [ ] 밖에서는 특수문자에 '\' 붙히기
- [ ] 안에서는 or 연결끼리 '|'로 연결하지 않아도 됨, [ ] 밖에서는 '|'로 연결
- 각종 정규표현식
- [:alnum:] : 알파벳 + 숫자
- [:digit:] : 숫자 0~9
- [:alpha:] : 알파벳 대소문자
- [:lower:] : 알파벳 소문자
- [:upper:] : 알파벳 대문자
- a-zA-Z0-9 소문자 + 대문자 + 숫자
select *
from Users
where regexp_like(email, '^[[:alnum:]_]+@[[:alpha:]]+\.com$')
order by user_id
select *
from Users
where regexp_like(email, '^[a-zA-Z0-9_]+@[a-zA-Z]+\.com$')
order by user_id
1517. Find Users With Valid E-Mails
: find the users who have valid emails.
풀이
- [ ] 안에서는 각 특수문자에 '\'를 붙히지 않아도 됨, [ ] 밖에서는 특수문자에 '\' 붙히기
- [ ] 안에서는 or 연결끼리 '|'로 연결하지 않아도 됨, [ ] 밖에서는 '|'로 연결
select *
from Users
where regexp_like(mail, '^[[:alpha:]][[:alnum:]_.-]*@leetcode\.com$')
select *
from Users
where regexp_like(mail, '^[A-Za-z][A-Za-z0-9_.-]*@leetcode\.com$')
'외부 수업 > SQL 스터디' 카테고리의 다른 글
[코테 연습] Leetcode 코딩테스트 연습 MySQL Med. (0) | 2025.05.12 |
---|---|
[SQL 개념 정리] Database / Phase of DB design / ER model / Relational Model / SQL (0) | 2025.05.05 |
[코테 연습] Leetcode 코딩테스트 연습 MySQL Easy (2) (0) | 2025.04.21 |
[코테 연습] Leetcode 코딩테스트 연습 MySQL Easy (0) | 2025.04.14 |
[코테 연습] 프로그래머스 코딩테스트 연습 Lv4 (3) | 2025.04.07 |