Ch6-7 Comparison Operators, Logical Operators (기초 5-9)

김민지·2023년 4월 15일
0

Part 05. SQL

목록 보기
3/7
  1. Comparison Operators (비교 연산자)
    A <> B : A가 B보다 작거나 크다 (같지 않다)
    A != B : A와 B가 같지 않다
select name, age from celeb where age=29 order by age;
select name, age from celeb where age!=29 order by age;
select name, age from celeb where age>29 order by age;
select name, age from celeb where age<29 order by age;
select name, age from celeb where age>=29 order by age;
select name, age from celeb where age<=29 order by age;
select name, age from celeb where age<>29 order by age;
  1. 논리연산자
  • AND : 조건을 모두 만족하는 경우 TRUE
  • OR : 조건을 하나라도 만족하는 경우 TRUE
  • NOT : 조건을 만족하지 않는 경우 TRUE
  • BETWEEN : 조건값이 범위 사이에 있으면 TRUE
  • IN : 조건값이 목록에 있으면 TRUE
  • LIKE : 조건값이 문자열 패턴에 맞으면 TRUE
  1. AND
  • 조건을 모두 만족하는 경우 TRUE
SELECT column1, column2, ...
FROM tablename
WHERE condition1 AND condition2 AND condition3, ...;
SELECT * FROM celeb WHERE age=29 AND sex='F';
SELECT * FROM celeb WHERE sex='M' AND age>40 ORDER BY name DESC;
  1. OR
  • 하나의 조건이라도 만족하는 경우 TRUE
  • 괄호가 없으면 AND가 OR보다 우선적으로 계산됨
SELECT column1, column2, ...
FROM tablename
WHERE condition1 OR condition2 OR condition3, ...;
SELECT * FROM celeb WHERE age<25 OR age>30 ORDER BY age;
select * from celeb where (age<29 and sex='F') or (age>30 and sex='M')
order by age, sex;
select * from celeb where (agency='YG엔터테인먼트' or agency='나무엑터스') and age<30;
select * from celeb where (ID%2 = 1 and sex='M') or (ID%2 = 0 and agency='YG엔터테인먼트') order by age;
  1. NOT
  • 조건을 만족하지 않는 경우 TRUE
SELECT column1, column2, ...
FROM tablename
WHERE NOT condition;
select * from celeb where not sex='F';
select * from celeb
where (agency='YG엔터테인먼트' and not sex='M')
or (job_title='가수' and not agency='YG엔터테인먼트');
select * from celeb
where (birthday > 19891231 and not sex='F')
or (birthday < 19800101 and not agency='안테나');

-> 생일이 1990년 이후면서 여자가 아니거나, 생일이 1979년 이전이면서 소속사가 안테나가 아닌 데이터 검색

  1. BETWEEN
  • 조건값이 범위 사이에 있으면 TRUE
SELECT column1, column2, ...
FROM tablename
WHERE column1 BETWEEN value1 AND value2;

-> value1, value2 포함! (>=, <=)

select * from celeb
where age BETWEEN 20 AND 40;
select * from celeb
where (NOT birthday BETWEEN 19800101 AND 19951231 and sex='F') or
(agency='YG엔터테인먼트' and NOT age BETWEEN 20 AND 45);
  1. IN
  • 목록 안에 조건이 존재하는 경우 TRUE
SELECT column1, column2, ...
FROM tablename
WHERE column IN (value1, value2, ...);

-> column값이 value1 or value2 or...

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

-> 나이가 28, 또는 48인 데이터 검색

select * from celeb
where (NOT agency IN ('나무엑터스', '안테나', '울림엔터테인먼트')) and (
sex='F' or age>=45);
  1. LIKE ('%', '_')
  • 조건값이 패턴에 맞으면 TRUE
SELECT column1, column2, ...
FROM tablename
WHERE column LIKE pattern;
select * from celeb where agency LIKE 'YG엔터테인먼트';
select * from celeb where agency LIKE 'YG%';

-> 'YG'로 시작하는 소속사 이름을 가진 데이터 검색

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

-> '엔터테인먼트'로 끝나는 소속사 이름을 가진 데이터 검색

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

-> '가수'를 포함한 직업명을 가진 데이터 검색

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

-> 소속사 이름의 두 번째 글자가 'G'인 데이터 검색
-> 언더바(_) 개수 : 글자수

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

-> 직업명이 '가'로 시작하고 최소 2글자 이상인 데이터 검색

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

-> 직업명이 '가'로 시작하고 최소 5글자 이상인 데이터 검색
-> '가' + 글자수를 표현하는 언더바 _ 4개

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 '%텔런트%');

-> 직업이 하나 이상(2개이상)인 연예인 중 영화배우 혹은 텔런트가 아닌 연예인 검색

select * from celeb where name like '__';

-> 이름이 두 글자인 데이터 검색

<제로베이스 데이터 취업 스쿨>

0개의 댓글