Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- stratascratch
- sql문제
- 데이터캠프
- SQL
- 데이터분석가양성과정
- sql문제풀이
- BDA과정
- Python
- 비즈니스분석가양성과정
- 데이터베이스
- 태블로
- 비즈니스 분석가
- Tableau
- 크롤링
- 데이터 분석을 위한 sql 레시피
- 데이터분석가 과정
- GA
- 시각화
- 패스트캠퍼스
- groupby
- eda
- 논리적사고
- 국비지원
- while
- sql with
- 데이터분석
- for
- SubQuery
- sql partition by
- 파이썬
Archives
- Today
- Total
원시인
[24일차] BDA과정 Business Analyst를 위한 핵심 SQL 실전 본문
[KDT] 패스트캠퍼스 비즈니스 데이터 분석가 양성과정 24일 차
BDA과정 Business Analyst를 위한 핵심 SQL 실전
안녕하세요 ㅎㅎ 오늘은 Business Analyst를 위한 핵심 SQL 실전 마지막 시간이었습니다. 추가적인 쿼리문 실습과
터미널, 서버 백업 등을 배우는 시간을 가졌습니다. 배운 내용 정리해보겠습니다.
Sub Query squery 문 안에 있는 query를 의미합니다. SELECT절 FROM절, WHERE 등에 사용이 가능합니다.
select절 sub Query
# 전체나라수 , 전체 도시수, 전체 언어수 출력(가로방향)
# select
select (select count(code)from country) as tot_country,
(select count(countrycode)from city) as tot_city ,
(select count(distinct (language)) from countrylanguage) as tot_language
from dual; #from절에 쓸게 없을때 쓴다.
From절 sub Query
#from
# 인구수가 900만 이상인 도시의 국가코드, 국가이름, 도시이름, 도시인구수를 출력
select country.code , country.name , city.name , city.population
from city
join country
on city.countrycode = country.code
having city.population >= 900*10000
#sub query를 사용시 조인을 할때 테이블을 수를 작게해 동작시킴으로 쿼리 성능이 더 좋다.
# city.population >=900*10000 (6) 먼저 실행 후 , city-country join (240*6)
select country.code , country.name , city.name , city.population
from ( select countrycode , name , population
from city
where population >=900*10000) as city
join country
on city.countrycode = country.code
Where절 sub Query
#where
#900만 이상 인구의 도시 국가코드, 국가이름, 대통령이름을 출력
select code , name , headofstate
from country
where code in (select countrycode
from city
where population >=900*10000 )
View 가상테이블 생성
#view : 가상테이블
select country.code , country.name , city.name , city.population
from ( select countrycode , name , population
from city
where population >=900*10000) as city
join country
on city.countrycode = country.code
#view 생성 (테이블처럼 활용가능 )
create view city_900 as
select countrycode , name , population
from city
where population >=900*10000
# 쿼리의 크기를 줄여주는 용도로 사용
select country.code , country.name , city.name , city.population
from city_900 as city
join country
on city.countrycode = country.code
Index 설정
index 서비스에서 where절에 많이 사용되는 칼럼을 인덱스로 설정하면 효율적임
데이터 분석 성능을 높여줄 수 있는 index를 적절한 사용 중요
인덱스 종류 : 클러스터형 인덱스(정렬) , 보조 인덱스 (일부 데이터만 추출해서 만든다)
# index 확인
show index from salaries
select * from salaries where to_date < "1980-01-01" ;
# index 설정 컬럼을 기준으로 생성
create index tdate on salaries (to_date) ;
show index from salaries
select * from salaries where to_date < "1980-01-01" ;
# index 삭제
drop index tdate on salaries
# 실행계획을 확인하여 인덱스를 사용하는지 확인합니다
explain select * from salaries where to_date < "1980-01-01"
create index tdate on salaries (to_date) ;
TRIGGER
특정 테이블을 감시하고 있다가 설정한 조건에 감지되면 지정해 놓은 쿼리가 자동으로 실행되도록 하는 방법입니다.
create table chat(
cid int primary key auto_increment,
msg varchar(20) )
create table chat_backup (
cbid int primary key auto_increment ,
backup_msg varchar(20),
backup_date timestamp default current_timestamp
)
insert into chat (msg) values("hello") ,("hi") ,("my name is jin!"), ("hi")
delimiter |
before delete on chat # chat 테이블에서 delete가 실행되기 전에
for each row begin # 미리 쿼리 작성 한다는 의미
insert into chat_backup(backup_msg)
values ( old.msg) ; # update 되기전 데이터 insert
end |
# 우리가 쿼리를 작성하다보면 ";" 두개 써야 할 상황이 생기는데
이렇게 되면 쿼리 실행 불가 delimiter 사용해서 쿼리의 종결이라는 것을 다른 문자로 설정해 끝나는걸 명시
show triggers
select * from chat
select * from chat_backup
delete from chat where msg like "h%" limit 10 ;
SQL 쿼리 뿐만 아니라 AWS 인스턴스를 통해 백업하는 방법 등 접해 보지 못한 여러 실습들을 해본 시간이었던 거 같습니다. SQL을 깊이 있게 배우고 쿼리를 다양하게 다뤄보고 싶은 분들에게는 아쉬운 수업이 되었을 수도 있겠다는 생각이 드네요. 이번 주도 이렇게 끝이 났는데 일주일이 정말 순식간에 지나가는 거 같네요.
모두 주말 잘 보내시고 인시오 사스!!!
'비즈니스 분석가 양성과정' 카테고리의 다른 글
[27일차] BDA과정 Tableau를 활용한 시각화 입문 (0) | 2021.11.24 |
---|---|
[25,26일차] BDA과정 Tableau를 활용한 시각화 입문 (0) | 2021.11.23 |
[23일차] BDA과정 Business Analyst를 위한 핵심 SQL 실전 (0) | 2021.11.18 |
[22일차] BDA과정 Business Analyst를 위한 핵심 SQL 실전 (0) | 2021.11.17 |
[21일차] BDA과정 데이터베이스와 SQL (0) | 2021.11.16 |
Comments