SQL 3일차 chapter7(~17)

박영선·2023년 5월 20일
0

BETWEEN

조건값이 범위 사이에 있으면 TRUE

나이가 20세~40세 사이 검색

select * from celeb where age between 20 and 40;

1980년~1995년 사이 출생이 아니면서 여자이거나 / YG면서 나이가 20세에서 45세가 아닌 것

select * from celeb where ((not birthday between 19800101 and 19951231) and sex = 'F') or
    -> (agency = 'YG엔터테인먼트' and not age between 20 and 45);

나이가 30~60 사이이고 성별이 남자인 데이터 나이순 정렬

select * from celeb where age between 30 and 60 and sex = 'M' order by age;

나이가 30~60 사이가 아니거나 / YG 소속인 데이터 나이 역순 정렬

select * from celeb where (not age between 30 and 60) or agency = 'YG엔터테인먼트' order by age DESC;

아이디가 1~5 사이면서 여자거나 / 아이디 홀수, 남자면서 20~30세 사이

 select * from celeb where (id between 1 and 5 and sex = 'F') or (id%2)=1 and sex = 'M' and age between 20 and 30;

IN

목록 안에 조건이 존재하는 경우 TRUE
(select, from where column in (value1, value2....)

나이가 28세, 48세 중 하나인 데이터 검색

select * from celeb where age in (28,48);

소속사가 나무액터스, 안테나, 울림이 아니면서 / 여자거나 /45세 이상인 데이터

select * from celeb where not agency in ('나무액터스', '안테나','울림엔터테인먼트') and
    -> (sex = 'F' or age >=45);

아이유, 이미주, 유재석, 송강 중 나무액터스 소속인 데이터 조회

select * from celeb where name in ('아이유', '이미주', '유재석', '송강') and agency = '나무액터스';

안테나,YG가 아니고 성별이 여자

select * from celeb where not agency in ('안테나','YG엔터테인먼트') and sex = 'F';

아이유 송강 강동원 차승원 중 YG 소속 아니거나 나이가 40~50 사이인 데이터

select * from celeb where name in ('아이유', '송강', '강동원', '차승원') and (agency != 'YG엔터테인먼트' or
    -> age between 40 and 50);

Like

조건값이 패턴에 맞으면 TRUE (패턴은 조건의 일부라고 생각하면 됨)
ex) 'YG%' 는 YG로 시작하는 단어, '%가수% 는 가수가 들어간 단어,
'_G%'는 두번째 글자가 G인 단어, job_title like '%,%' 는 직업이 두개 이상인 사람

소속사가 YG / YG로 시작하는 소속사 이름을 가진 데이터 검색 /엔터테인먼트로 끝나는 데이터

select * from celeb where agency like 'YG엔터테인먼트';

select * from celeb where agency like 'YG%';

select * from celeb where agency like '%엔터테인먼트';

직업명에 가수가 포함된 데이터 / 소속사 이름 두번째 글자가 G / 2글자 이상 가 시작 직업

select * from celeb where job_title like '%가수%';

select * from celeb where agency like '_G%';

select * from celeb where job_title like '가_%';

5글자 이상 가 시작 직업 / '영'시작 , 모델로 끝나는 직업 데이터

select * from celeb where job_title like '가____%';

select * from celeb where job_title like '영%모델';

영화배우, 탤런트 병행하는 데이터

select * from celeb where job_title like '%영화배우%' and job_title like '%탤런트%';

직업이 하나 이상인 연예인중 영화배우, 혹은 탤런트가 아닌 연예인

select * from celeb where (job_title like '%,%') and not(job_title like '%영화배우%' or job_title like '%탤런트%');

직업 중 가수 포함, 성은 이씨 데이터

select * from celeb where job_title like '%가수%' and name like '이%';

남자거나 직업명이 탤런트로 끝나면서 최소 5글자 이상인 데이터

select * from celeb where sex='M' or job_title like '%__탤런트';

이름이 두글자인 데이터

select * from celeb where name like '__';

나이가 30세 이상 50세 이하 개그맨이 아닌 데이터

select * from celeb where age between 30 and 50 and job_title not like '%개그맨%';

아이유,이미주,유재석,송강 중 소속사가 나무 로 시작하는 데이터

select * from celeb where name in ('아이유', '이미주', '유재석', '송강')
    -> and agency like '나무%';

아이유,이미주,송강,이수현 중에 가수만 직업으로 가졌거나, 가수 병행하지않고 탤런트 하는 사람

select * from celeb where name in ('아이유', '이미주', '송강', '이수현')
    -> and (job_title like '가수' or (not job_title like '%가수%' and job_title like '%탤런트%'));

profile
데이터분석 공부 시작했습니다

0개의 댓글