6번부터 분노의 문제풀이 시작
참고로 글을 쓰기 시작한 현재 시간은 오전 12시28분이다
✔ SQL 문법 연습문제
3번6번~ 풀기
연습문제 6번
어제의 글에서 바로 이어진다면 절대 푸는것이 불가능한 문제이지만 평행우주의 나는 이미 SQL 입문 강의를 5주차까지 수강한 상태이기 때문에 아주 쉽게 풀 수 있다.
Select *, rank() over(order by rating desc) as 'Rank' FROM lol_users; Select name from lol_users order by join_date desc limit 1 ; Select * from lol_users order by region, rating desc; Select region, avg(rating) from lol_users group by 1;하나씩 실행 해 보면 결과는 다음과 같다.
빠르게 다음문제
연습문제 7번
데이터테이블을 만드는 과정은 캡쳐하기 귀찮은 관계로 앞으로 코드만 올릴 생각이다.
CREATE TABLE lol_feedbacks ( id INT , user_name VARCHAR(50), satisfaction_score INT, feedback_date DATE ); INSERT INTO lol_feedbacks (id, user_name, satisfaction_score, feedback_date) VALUES (1, '르탄이', 5, '2023-03-01'), (2, '배캠이', 4, '2023-03-02'), (3, '구구이', 3, '2023-03-01'), (4, '이선이', 5, '2023-03-03'), (5, '구구이', 4, '2023-03-04');문제의 답은 다음과 같다.(아마도)
Select * from lol_feedbacks order by satisfaction_score desc; Select user_name, max(feedback_date) from lol_feedbacks group by 1; Select count(satisfaction_score) from lol_feedbacks where satisfaction_score = 5; Select user_name, count(1) from lol_feedbacks group by 1 order by 2 desc limit 3; Select feedback_date, avg(satisfaction_score) from lol_feedbacks group by feedback_date;실행 결과는 다음과 같다.
바로 다음문제
연습문제 8번빠르게 데이터테이블
CREATE TABLE doctors ( id int, name VARCHAR(50), major VARCHAR(100), hire_date DATE ); INSERT INTO doctors (id, name, major, hire_date) VALUES (1, '르탄이', '피부과', '2018-05-10'), (2, '배캠이', '성형외과', '2019-06-15'), (3, '구구이', '안과', '2020-07-20');빠르게 문제
Select name from doctors where major='성형외과'; Select major, count(major) from doctors group by 1; Select count(1) from doctors where curdate()-hire_date>= 50000; Select *, date_format((curdate()-hire_date),'%y년%m개월%d일') from doctors;이렇게 푸는 게 맞나 싶지만 아무튼 풀면 된단 마인드
실행결과는 다음과 같다.
아직까지는 매우 순조롭다. 그리고 피곤하다.
연습문제 9번
Table
CREATE TABLE patients ( id INT , name VARCHAR(50), birth_date DATE, gender VARCHAR(10), last_visit_date DATE ); INSERT INTO patients (id, name, birth_date, gender, last_visit_date) VALUES (1, '르탄이', '1985-04-12', '남성', '2023-03-15'), (2, '배캠이', '1990-08-05', '여성', '2023-03-20'), (3, '구구이', '1982-12-02', '여성', '2023-02-18'), (4, '이선이', '1999-03-02', '남성', '2023-03-17');Query
Select gender, count(1) from patients group by 1; Select count(1) from patients where curdate()-birth_date>=400000; Select * from patients where curdate()-last_visit_date>=10000; Select * from patients where substr(birth_date,2,2)=98;상수가 많아 뭔가 야매처럼 보이지만 놀랍게도 아주 정석적이라는 생각을 가지며 풀었다.
실행 결과는 이러하다.
daum
연습문제 10번
Quary
Select count(distinct(name)) from departments; Select e.*, d.name from employees e left join departments d on e.department_id=d.id; Select e.name from employees e left join departments d on e.department_id=d.id where d.name='기술팀'; Select d.name, count(d.name) from employees e left join departments d on e.department_id=d.id group by 1 ; Select name from(Select d.name, count(d.name) cnt from employees e left join departments d on e.department_id=d.id group by 1 ) a where cnt=0; Select e.name from employees e left join departments d on e.department_id=d.id where d.name='마케팅팀'실행 결과s
연습문제 LAST
table
CREATE TABLE products ( id INT primary key, name VARCHAR(50), price INT ); INSERT INTO products (id, name, price) VALUES (1, '랩톱', 1200), (2, '핸드폰', 800), (3, '타블렛', 400); CREATE TABLE orders ( id INT primary key, product_id INT, quantity INT, order_date DATE ); INSERT INTO orders (id, product_id, quantity, order_date) VALUES (101, 1, 2, '2023-03-01'), (102, 2, 1, '2023-03-02'), (103, 3, 5, '2023-03-04'), (104, 1, 2, '2023-03-05'), (105, 1, 1, '2023-03-06'), (106, 2, 5, '2023-03-07');확실한 검산을 위해 임의의 데이터를 추가했다.
quarySelect o.id, p.name From products p join orders o on p.id=o.product_id; Select p.id, p.price*sum(o.quantity) From products p join orders o on p.id=o.product_id group by 1 order by 2 desc limit 1; Select p.id, sum(o.quantity) from products p join orders o on p.id=o.product_id group by 1; select p.name From products p join orders o on p.id=o.product_id where o.order_date>'2023-03-03'; Select p.name From products p join orders o on p.id=o.product_id group by 1 order by sum(o.quantity) desc limit 1; Select p.id, avg(o.quantity) From products p join orders o on p.id=o.product_id group by 1; Select pi, pn from( Select p.id pi, p.name pn, sum(quantity) s From products p join orders o on p.id=o.product_id group by 1 ) a where s=0 or s is null;실행 결과는 다음과 같다.
✔ SQL 실전문제 1번~풀기
실전문제 1번
어떤 이유에서인지 난이도가 하락했다.
게다가 데이터도 강의 때 썼던 데이터이기 때문에 귀찮게 추가할 필요가 없어졌다.
답은 다음과 같다.SELECT count(1) from users where SUBSTR(name,1,1)='김';너무 쉬워서 발가락으로 풀어버렸다.
쉬운 문제들은 빠르게 빠르게 넘어가기 위해 문제와 답만 적고 넘어갈 생각이다.
실전문제 2번
Select date(created_at) created_at, round(avg(point)) average_points from point_users group by 1;
실전문제 3번문제가 너무 긴 관계로 핵심만 가지고 왔다.
Select u.user_id, u.email, coalesce(p.point,0) as point from users u left join point_users p on u.user_id = p.user_id order by p.point desc;
오?늘은 참... TIL도 날아가고 밤도 새고 이래저래 피곤한 날이었던 것 같다.
그래도 진도 쭉 빼서 나름 만족하지만 그럴수록 설렁설렁 넘어가지 않게끔 기본기도 다시 다질 필요가 있어보인다.