SQL 기초

Bravesong·2022년 11월 23일
0

SQL 시작하기

WHERE 문의 기본 형식

SELECT [열] FROM [테이블] WHERE [열] = [조건값] * =: 연산자, <, <=, =, >, >=, <>, != :조건과 같지 않은 값

select * from nasdaq_company where ipo_year <2021

select * from nasdaq_company where ipo_year BETWEEN 2010 AND 2021

select * from nasdaq_company where symbol between 'A' and 'B' --심볼 열에서 A나B를 포함하는 값 검색

select * from nasdaq_company where symbol not between 'A' and 'B' -- A나 B 제외 검색

select * from nasdaq_company
where symbol = 'AAPL' or symbol = 'MSFT' or symbol = 'TSLA'

select * from nasdaq_company where symbol in ('AAPL', 'MSFT', 'TSLA')

select symbol, company_name from nasdaq_company
where symbol in ('AAPL', 'MSFT', 'TSLA')

와일드 카드로 문자 검색

select [열] from [테이블] where [열] like [조건값]

--A를 포함한 글자 다 찾기
select from nasdaq_company where symbol like '%A%'
--A로 시작하는 글자 다 찾기
select
from nasdaqcompany where symbol like 'A%'
--A로 끝는 글자 다 찾기
select from nasdaq_company where symbol like '%A'
--A로 시작하지 않는글자 다 찾기
select
from nasdaq_company where symbol not like 'A%'
--AA로 시작하는 글자 다 찾기
select * from nasdaq_company where symbol like 'AA%'
--A로 시작하면서 어떤 글자든 (
) 1자리 글자가 오고 전체가 2글자인 것 다 찾기
select from nasdaqcompany where symbol like 'A'
--A로 끝나서 어떤 글자든 (_) 1자리 글자가 앞오고 전체가 2글자인 것 다 찾기
select
from nasdaqcompany where symbol like '_A'
--세글자 중 가운데 A인 것 다 찾기
select * from nasdaq_company where symbol like '_A
'
--A로 시작하며 세번째는 글자가 C인 것 다 찾기
select from nasdaq_company where symbol like 'A_C%'
--A로 시작하며 C로 끝나는 4글자 다 찾기
select
from nasdaqcompany where symbol like 'A__C'
--AA로 시작하면서 c 또는 p 글자 다 찾기
select from nasdaq_company where symbol like 'AA[c,p]%'
--AA로 시작하면서 L을 포함하지 않는 글자 다 찾기
select
from nasdaq_company where symbol like 'AA[^L]%'
--A로 시작하면서 어떤 글자도 상관없고 마지막은 L과 어떤 한 글자 찾기
select * from nasdaq_company where symbol like 'A%L
'

[]로 문자나 문자 범위를 지정해 문자열 검색하기

[]의 사용법

[A,B,C]% 또는 [A-C]%: 첫글자가 A 또는 B 또는 C로 시작하는 모든 문자열 검색

%[A,B,C] 또는 %[A-C]: 마지막 글자가 A 또는 B 또는 C로 끝나는 모든 문자열 검색

select from nasdaq_company where symbol like 'A[A-C]'
select
from nasdaq_company where symbol like 'A[A-C, G, M-R]'

--문자나 문자범위를 제외한 문자열 검색하기

-- not가 아닌 ^를 사용

select from nasdaq_company where symbol like 'A[^A, ^B, ^C]'
select
from nasdaq_company where symbol like 'A[^A-C]'

다양한 방법 응용

A로 시작하고 두번째는 C나 P를 포함하며 세번째는T를 제외하고 마지막에 W로 끝나는 문자열 검색

select from nasdaq_company where symbol like 'A[C,P][^T]%W'
select
from nasdaq_company where symbol like 'A[A-C]_O%'

%가 들어있는 문자열을 찾고 싶을 떄 ESCAPE '#', '!', '&'

WITH CTE(col_1) AS(
SELECT 'A%BC' UNION ALL
SELECT 'A_BC' UNION ALL
SELECT 'ABC'
)
SELECT * FROM CTE WHERE col_1 LIKE '%#%%' ESCAPE '#'

ORDER BY .... 명시한 컬럼 순서대로 정렬, ASC(오름) DESC(내림)

select * from nasdaq_company
ORDER BY symbol desc

년도는 내림차순으로, 심볼은 오름차순으로 정렬

select * from nasdaq_company
ORDER BY ipo_year desc, symbol ASC

테이블의 열 목록을 확인하는 쿼리

EXEC sp_columns @table_name =N'nasdaq_company', @table_owner = N'dbo'

퀴즈1. nasdaq_company 테이블에서 sector, industry 열만 검색하시오

select sector, industry from nasdaq_company

퀴즈2. nasdaq_company 테이블에서 symbol, close_price 열만 검색하시오

select symbol, close_price from nasdaq_company

요구사항: sector가 "Technology' 또는 'Customer Services'이면서 symbol이 'MSFT', 'AMD', 'AMZN' 검색

select * from nasdaq_company where (sector ='Technology' or sector = 'Consumer Services')
AND symbol in ('MSFT', 'AMD', 'AMZN')

select * from nasdaq_company where sector IN('Technology', 'Consumer Services')
AND symbol in ('MSFT', 'AMD', 'AMZN')

null이 들어 있는 데이터 확인

select * from nasdaq_company where sector is null

null이 없는 데이터 확인

select * from nasdaq_company where sector is not null

퀴즈3. nasdaq_company 테이블에서 ipo_year가 2021년이면서,

sector가 FInance이면서 symbol이 AGAC, TIRX, VLATW인 목록을 출력하세요.

select * from nasdaq_company where ipo_year = 2021 and sector = 'Finance'
and symbol in('AGAC', 'TIRX', 'VLATW')

연습문제

1. nasdaq_company 전체 목록을 출력하세요.

select * from nasdaq_company

2. nasdaq_company에서 symbol이 'MSFT'인 목록을 출력하세요.

select * from nasdaq_company where symbol = 'MSFT'

3. nasdaq_company에서 company_name이 'apple'이라는 글자를 포함하느 목록을 출력하세요.

select * from nasdaq_company where company_name like '%apple%'

4. nasdaq_company에서 symbol이 'AA'로 시작하면서 'L', 'Q'를 포함하는 목록을 출력하세요

select * from nasdaq_company where symbol LIKE 'AA%[L,Q]%'

5. nasdaq_company에서 last_sales가 $1 이하인 것을 출력하세요

select * from nasdaq_company where close_price <= 1 and symbol not like '%^%'

6. nasdaq_company에서 close_price가 높은 순서대로 출력하세요

select * from nasdaq_company
where symbol not like '%[-,^,=]%' and close_price <>0
order by close_price asc

7. nasdaq_company에서 last_sales가 $10이상, $20이하, volume이 10억 이상이면서, company_name이 'A'를 포함하지 않으면서, ipo_year가 최근 5년 이하인 목록을 출력하면서, close_price 내림차순, ipo_year는 오름차순으로 출력하세요

select * from nasdaq_company
where close_price >=10 and close_price <=20 and
company_name not like '%a%' and ipo_year >= '2017'
order by close_price desc, ipo_year asc

profile
용감한 송이

0개의 댓글