SQL 6일차 chapter10(~6)

박영선·2023년 5월 23일
0

CONCAT

여러문자열 하나로 합치거나 연결

select concat('연결','할','문자열') from 테이블;

ALIAS

컬럼이나 테이블 이름에 별칭 생성

select column as alias from 테이블이름;(컬럼)
select column from 테이블이름 as alias;

name을 이름, agency를 소속사로 별칭 만들어서 검색

select name as '이름' , agency as '소속사' from celeb;

name, job_title 합쳐서 profile 별칭 만들어서 검색

select concat(name,' : ', job_title) as 'profile' from celeb;

snl 출연 celeb 기준으로 두테이블 조인, celeb은 c, snl은 s로 별칭 만들어서
출연 시즌, 에피소드, 이름, 직업 검색

select s.season, s.episode, c.name, c.job_title
    -> from celeb as c, snl_show as s
    -> where c.name = s.host;

snl 출연 celeb 기준 두테이블 조인 / 시즌 에피소드 방송일 합쳐서 방송정보 / 이름 직업 합쳐서 출연자 정보

select concat(s.season, '-', s.episode, ' (', s.broadcast_date, ')') as '방송정보',
    -> concat(c.name, ' (', c.job_title, ')') as '출연자정보'
    -> from celeb as c, snl_show as s
    -> where c.name = s.host;

이름이 세글자인 연예인 정보 이름/ 소속사 출력

select concat('이름 : ', name, ' 소 속 사 : ', agency) as '연예인 정보'
    -> from celeb
    -> where name like '___';

앞글자가 2글자이고 엔터테인먼트로 끝나는 소속사 연예인중
snl에 출연한 연예인의 신상정보(나이,성별) , 출연정보(시즌-에피,방송날짜), 소속사정보를 방송날짜 최신순으로 정렬

 select c.agency '소속사정보',concat('나이 : ',c.age, '(',c.sex,')') as 
 '신상정보',
    -> concat(s.season,'-',s.episode,'방송날짜 : ', s.broadcast_date) as 
    '출연정보'
    -> from celeb as c, snl_show as s
    -> where c.agency like '__엔터테인먼트'
    -> and c.name = s.host
    -> order by s.broadcast_date DESC;

DISTINCT

검색결과의 중복제거

select distinct column1, column2.... from 테이블이름;

연예인 소속사 종류 검색

select agency from celeb;(중복포함)
select distinct agency from celeb;(중복제거)

celeb테이블에서 성별, 소속사별 종류 검색 / 성별, 소속사 순으로 정렬

select sex, agency from celeb order by sex, agency;

LIMIT

검색결과를 정렬된 순으로 주어진 숫자만큼만 조회

select column1,2... from 테이블이름 where 조건 limit 숫자;

select * from celeb limit 3;

나이가 가장적은 연예인 4명

 select * from celeb order by age asc limit 4;

남자연예인중 나이가 가장 많은 2명

 select * from celeb where sex='M' order by age desc limit 2;

snl 출연 연예인의 정보를 최신 방송날짜 순으로 2개만 검색

select concat('SNL 시즌 ', s.season, '에피소드 ', s.episode, ' 호스트', s.host) as '방송정보', c.age
    ->  from celeb as c, snl_show as s
    -> where s.host = c.name
    -> order by s.broadcast_date desc limit 2;

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

0개의 댓글