SQL
SQL 문법 연습 - 9)아프면 안됩니다! 항상 건강 챙기세요!
김예나
2024. 12. 7. 22:33
id | name | birth_date | gender | last_visit_date |
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 |
1. patients 테이블에서 각 성별(gender)에 따른 환자 수를 계산하는 쿼리를 작성해주세요!
select gender, count(*)
from patients
group by gender
2. patients 테이블에서 현재 나이가 40세 이상인 환자들의 수를 계산하는 쿼리를 작성해주세요!
select count(*)
from patients
where 40 <= timestampdiff(year, birth_date, curdate())
SELECT COUNT(*) FROM patients
WHERE birth_date <= DATE_SUB(CURDATE(), INTERVAL 40 YEAR);
3. patients 테이블에서 마지막 방문 날짜(last_visit_date)가 1년 이상 된 환자들을 선택하는 쿼리를 작성해주세요!
select *
from patients
where last_visit_date <= date_sub(curdate(), interval 1 year)
4. patients 테이블에서 생년월일이 1980년대인 환자들의 수를 계산하는 쿼리를 작성해주세요!
select count(*)
from patients
where birth_date between '1980-01-01' and '1989-12-31'