count 함수로 행의 개수를 구할수있다.
in:
select count(*)
from books;
out:

count와 distinct 함수를 활용하여 고유한 값의 개수를 구할수있다.
author_fname의 개수를 구해보자
in:
select count(distinct author_fname)
from books;
out:

like 를 활용하여 특정 단어가 포함된 데이터를 셀수있다.
책제목에 the 가 들어있는 책은 몇개인지 구해보자.
in:
select count(title)
from books
where title like '%the%';
out:

group by를 사용해서 author_lname 별로 몇권의 책을 냈는지 나타내보자
in:
select author_lname, count(*) as cnt
from books
group by author_lname;
out:

여러개 컬럼을 group by에 넣을수있다.
in:
select author_fname, author_lname,count(*)
from books
group by author_fname, author_lname;
out:

# 작가의 이름과 성으로 group by 해줬다.
min 함수로 출시년도의 최소값을 구하자
in:
select min(released_year)
from books;
out:

# 1945년에 출시된 책이 가장 오래된 책이다.
max 함수로 페이지수 중에서 가장 큰 값을 구하자
in:
select max(pages)
from books;
out:

# 가장 두꺼운책의 페이지 수는 634페이지다.
avg 함수로 books 테이블에서의 페이지수 평균은?
in:
select avg(pages)
from books;
out:

# 평균 페이지수는 349 페이지다.
sum 함수로 이 테이블의 전체 페이지수의 총 합을 구해보자
in:
select sum(pages)
from books;
out:

[실습]
각 작가별로 자신이 쓴 책의 최소 년도는?
작가의 성과 이름 그리고 최소년도가 나오도록 가져오세요.
in:
select author_fname, author_lname, min(released_year)
from books
group by author_fname, author_lname;
out:

각 작가별로 자신이 쓴 책의 페이지가 가장 많은 책의 페이지 수를
작가의 성과 이름 그리고 최대 페이지수로 나오도록 가져오세요.
in:
select author_fname, author_lname, max(pages)
from books
group by author_fname, author_lname;
out:

연도별로 책의 수와 평균 페이지 수를 구하세요.
in:
select released_year, count(*) as '#books', avg(pages) as 'avg pages'
from books
group by released_year
order by released_year;
out:

댓글