MySQL 기초

[SQL 다루기] Logical Operators

jasonshin 2021. 12. 8. 14:41

-- Logical Operators
select * from books ;

-- 2017년도의 책을 가져오시오.
select * from books
where release_year = 2017;

-- 2017년도를 제외한 책을 가져오시오.
select * from books
where released_year != 2017;

-- 검색 : w로 시작하는 책을 가져오시오.
select * from books
where title like 'w%' ; 

-- w로 시작하지 않는 책들만 가져오시오. 
select * from books
where title not like 'w%' ;

-- 연도가 2000년 이후에 나온 책들을 가져오시오.
select * from books
where released_year > 2000; 

-- 위의 책들을 연도로 정렬하세요.
select * from books
where released_year > 2000
order by released_year; 

-- 재고가 100개 이상인 책들만 재고가 많은 순으로 가져오시오. 
select * from books
where stock_quantity >= 100
order by stock_quantity desc;

-- lname이 Eggers인 사람의 데이터를 가져오시오. 
select * from books
where author_lname = 'Eggers';

-- 조건 결합!
-- lname이 Eggers이고 발행연도는 2010년 이상인 책들을 가져오시오.
select * from books
where author_lname = 'Eggers' and released_year > 2010;

-- 2010년 이후에 발간되었고, 제목에 nove1이 들어가는 책들을 가져오시오. 
select * from books
where released_year > 2010 and title like '%nove1%';

-- lname이 Eggers이거나 2010년 이후에 발행된 책들을 가져오시오. 
select * from books
where author_lname = 'Eggers' or released_year>2010;

-- 2004년부터 2015년 사이에 발간된 책들을 가져오세요.
select * from books
where released_year>=2004 and released_year <= 2015;

select * from books
where released_year between 2004 and 2015;

select * from people2; 

-- 1980년 1월 1일 부터 2000년 1월 1일 까지의 데이터를 가져오시오. 
select * from people2
where birthdate between '1980-01-01' and '2000-01-01';

select * from people2
where birthdt between '1980-01-01' and '2000-01-01';

select * from books

-- 2003년, 2001년, 1981년, 1985년에 쓰여진 책들만 가져오시오.
select * from books
where released_year in (2003, 2001, 1981, 1985);

-- 2003년, 2001년, 1981년, 1985년을 제외한 책들만 가져오시오.
select * from books
where released_year not in (2003, 2001, 1981, 1985);

-- 2000년 포함하여 그 이후에 출간된 책들이며, 짝수 해에 발행된 책들만 가져오시오. 
select * from books 
where released_year >= 2000 and released_year % 2 = 0
order by released_year; 

-- case 문법
-- 연도가 2000년 보다 크면, '최근 책'이라고 하고, 
-- 그렇지 않으면 '옛날 책'이라고 하시오. 
select * , 
case 
when released_year >= 2000 then '최근 책' else '옛날 책' 
end as genre 
from books 

-- 재고가 0~50 사이면, * (별표 한개)로 표시하고
-- 재고가 51~100 사이면, **(별표 두개)로 표시하고
-- 나머지는 ***로 표시하자.
select * , 
case 
when stock_quantity between 0 and 50 then '*' 
    when stock_quantity between 51 and 100 then '**' 
    else '***'
end as stock 
from books; 
-- 실습문제2-2
select * from books
where released_year < 1980;
-- 실습문제2-3
select * from books
where author_lname = 'Eggers' or author_lname = 'Chabon' ;

select * from books
where author_lname in ('Eggers','Chabon');
-- 실습문제2-4
select * from books
where author_lname = 'Lahiri' and released_year > 2000 ;
-- 실습문제2-5
select * from books
where pages between 100 and 200;
-- 실습문제2-6
select * from books
where author_lname like 'C%' or author_lname like 'S%';

반응형