SQL은 관계형 데이터베이스에 저장된 데이터를 조작하고 관리하기 위해 만들어진 프로그래밍 언어이다.
- 관계형 데이터베이스는 하나 이상의 테이블에 정보를 구조화한 데이터베이스이다.
- 테이블은 행과 열로 구조화된 데이터의 집합이다.
문장은 데이터베이스가 유효한 명령으로 인식하는 문자열이다.
SQL 명령은 표1 정도를 알아볼 것이고 먼저 질의하는 SELECT 문의 사용법을 알아 본다.
표 1. SQL 명령.
SQL 명령 |
설명 |
CREATE TABLE |
새로운 테이블을 생성한다. |
INSERT INTO |
테이블에 새로운 행을 추가한다. |
SELECT | 테이블로부터 데이터를
|
UPDATE |
테이블에 있는 행을 편집한다. |
ALTER TABLE | 기존 테이블을 변경한다. |
DELETE | 테이블에서 행들을 분리한다. |
0. 사용할 movies table
DBMS_Exam_v1.xlsx
Database Schema
id |
INTEGER |
name |
TEXT |
genre |
TEXT |
year |
INTEGER |
imdb_rating |
REAL |
movies 테이블은 id, name, genre, year, imdb_rating의 열을 가지고 레코드들의 일부는 아래와 같다.
id |
name |
genre |
year |
imdb_rating |
1 |
Avatar |
action |
2009 |
7.9 |
2 |
Jurassic World |
action |
2015 |
7.3 |
3 |
The Avengers |
action |
2012 |
8.1 |
4 |
The Dark Knight |
action |
2008 |
9 |
5 |
Star Wars: Episode I - The Phantom Menace |
action |
1999 |
6.6 |
6 |
Star Wars |
action |
1977 |
8.7 |
7 |
Avengers: Age of Ultron |
action |
2015 |
7.9 |
8 |
The Dark Knight Rises |
action |
2012 |
8.5 |
9 |
Pirates of the Caribbean: Dead Mans Chest |
action |
2006 |
7.3 |
10 |
Iron Man 3 |
action | 2013 |
7.3 |
1. SELECT
SELECT는 데이터베이스에서 데이터를 조회 할 때마다 사용한다.
예1) 원하는 열만 조회
-. 여러 열을 쉼표로 열 이름을 분리하여 한 번에 조회 할 수 있다.
SELECT name, imdb_rating FROM movies;
-. 실행 결과
name |
imdb_rating |
Avatar |
7.9 |
Jurassic World |
7.3 |
The Avengers |
8.1 |
The Dark Knight |
9 |
Star Wars: Episode I - The
Phantom Menace |
6.6 |
Star Wars |
8.7 |
Avengers: Age of Ultron |
7.9 |
The Dark Knight Rises |
8.5 |
Pirates of the Caribbean: Dead
Mans Chest |
7.3 |
Iron Man 3 |
7.3 |
예2) 조회할 열에서 중복된 행(레코드)을 제외한 유일한 값만 반환.
-. genre 속성의 DISTINCT 키워드를 가진 SELECT를 수행하면 중복된 장르(genre) 제외한 장르의 종류만 볼 수 있다.
SELECT DISTINCT genre FROM movies;
-. 실행 결과
genre |
action |
comedy |
horror |
romance |
drama |
예3) WHERE 절 이용하여 레코드를 선택할 기준을 지정하기.
-. WHERE 절의 조건들을 적용하여 조회한 결과를 걸러준다.
SELECT * FROM movies WHERE year >= 2000;
-. WHERE 절에 사용할 operator
Operator |
설명 |
= |
equals |
!= |
not equals |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
1 |
Avatar |
action |
2009 |
7.9 |
2 |
Jurassic World |
action |
2015 |
7.3 |
3 |
The Avengers |
action |
2012 |
8.1 |
4 |
The Dark Knight |
action |
2008 |
9 |
7 |
Avengers: Age of Ultron |
action |
2015 |
7.9 |
8 |
The Dark Knight Rises |
action |
2012 |
8.5 |
9 |
Pirates of the Caribbean: Dead Mans Chest |
action |
2006 |
7.3 |
10 |
Iron Man 3 |
action |
2013 |
7.3 |
11 |
Spider-Man |
action |
2002 |
7.3 |
12 |
Transformers: Revenge of the Fallen |
action |
2009 |
6 |
13 |
Star Wars: Episode III - Revenge of the
Sith |
action |
2005 |
7.7 |
14 |
Spider-Man 2 |
action |
2004 |
7.3 |
15 |
Transformers: Dark of the Moon |
action |
2011 |
6.3 |
16 |
American Sniper |
action |
2014 |
7.4 |
17 |
Furious Seven |
action |
2015 |
7.4 |
18 |
Spider-Man 3 |
action |
2007 |
6.2 |
19 |
Guardians of the Galaxy |
action |
2014 |
8.1 |
20 |
Transformers |
action |
2007 |
7.1 |
예4) WHERE 절에 LIKE(특수 오퍼레이터) 이용해 특정 패턴 찾기.
지원하는 두 개의 wild character 연산자
_ |
하나의 문자에 해당한다. |
% |
0개 이상의 문자를 표시한다. |
그리고 LIKE는 대소 문자를 구분하지 않는다.
예 4.1) WHERE 절에 LIKE(특수 오퍼레이터) 조건들을 적용하여 열에서 특정 패턴을 가진 값을 찾아 준다.
SELECT * FROM movies
WHERE name LIKE 'Se_en';
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
219 |
Se7en |
drama |
1995 |
8.6 |
220 |
Seven |
drama |
1979 |
6.1 |
예 4.2) LIKE에 조건들을 wild character 적용하기.
SELECT * FROM movies
WHERE name LIKE '%war%';
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
5 |
Star
Wars: Episode I - The Phantom Menace |
action |
1999 |
6.6 |
6 |
Star Wars |
action |
1977 |
8.7 |
13 |
Star Wars: Episode III - Revenge of the
Sith |
action |
2005 |
7.7 |
24 |
Star Wars: Episode II - Attack of the
Clones |
action |
2002 |
6.7 |
26 |
Star Wars: Episode VI - Return of the Jedi |
action |
1983 |
8.4 |
32 |
Star Wars: Episode V - The Empire Strikes
Back |
action |
1980 |
8.8 |
98 |
World War Z |
horror |
2013 |
7 |
142 |
Warm Bodies |
horror |
2013 |
6.9 |
161 |
Snow White and the Seven Dwarfs |
romance |
1937 |
7.7 |
예5) WHERE 절에 BETWEEN 이용해 지정한 범위 내의 결과를 필터링 하기.
"greater than equal AND less than equal" 조건들의 조합을 대신하여 사용한다.
SELECT * FROM movies
WHERE year BETWEEN 1998 AND 2000;
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
5 |
Star
Wars: Episode I - The Phantom Menace |
action |
1999 |
6.6 |
66 |
How the Grinch Stole Christmas |
comedy |
2000 |
6 |
70 |
Toy Story 2 |
comedy |
1999 |
7.9 |
92 |
Austin Powers: The Spy Who Shagged Me |
comedy |
1999 |
6.6 |
99 |
What Lies Beneath |
horror |
2000 |
6.6 |
101 |
The Blair Witch Project |
horror |
1999 |
6.4 |
111 |
Sleepy Hollow |
horror |
1999 |
7.4 |
113 |
The Haunting |
horror |
1999 |
4.9 |
114 |
Scream 3 |
horror |
2000 |
5.5 |
139 |
End of Days |
horror |
1999 |
5.7 |
162 |
What Women Want |
romance |
2000 |
6.4 |
169 |
Tarzan |
romance |
1999 |
7.2 |
177 |
Runaway Bride |
romance |
1999 |
5.4 |
193 |
The Sixth Sense |
drama |
1999 |
8.2 |
197 |
Cast Away |
drama |
2000 |
7.7 |
202 |
Gladiator |
drama |
2000 |
8.5 |
204 |
The Perfect Storm |
drama |
2000 |
6.4 |
218 |
Big Daddy |
drama |
1999 |
6.4 |
예6) WHERE 절에 다중 조건 결합하여 사용 하기.
여러 조건들을 AND, OR 등을 이용하여 결합한다.
예 6.1) AND 연산자.
SELECT * FROM movies
WHERE year BETWEEN 1990 AND 2000
AND genre = 'action';
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
5 |
Star
Wars: Episode I - The Phantom Menace |
action |
1999 |
6.6 |
27 |
Independence Day |
action |
1996 |
6.9 |
42 |
Twister |
action |
1996 |
6.3 |
49 |
The Lost World: Jurassic Park |
action |
1997 |
6.5 |
예 6.2) OR 연산자.
SELECT * FROM movies
WHERE year BETWEEN 1998 AND 1999
OR name = 'Bat%';
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
5 |
Star
Wars: Episode I - The Phantom Menace |
action |
1999 |
6.6 |
39 |
Batman |
action |
1989 |
7.6 |
70 |
Toy Story 2 |
comedy |
1999 |
7.9 |
92 |
Austin Powers: The Spy Who Shagged Me |
comedy |
1999 |
6.6 |
101 |
The Blair Witch Project |
horror |
1999 |
6.4 |
111 |
Sleepy Hollow |
horror |
1999 |
7.4 |
113 |
The Haunting |
horror |
1999 |
4.9 |
138 |
Blade |
horror |
1998 |
7.1 |
139 |
End of Days |
horror |
1999 |
5.7 |
167 |
Theres Something About Mary |
romance |
1998 |
7.1 |
169 |
Tarzan |
romance |
1999 |
7.2 |
177 |
Runaway Bride |
romance |
1999 |
5.4 |
193 |
The Sixth Sense |
drama |
1999 |
8.2 |
199 |
Saving Private Ryan |
drama |
1998 |
8.6 |
218 |
Big Daddy |
drama |
1999 |
6.4 |
예7) ORDER BY 절에 다중 조건 결합하여 사용 하기.
하나 이상의 열에 대하여 데이터를 오름차순 또는 내림차순으로 정렬하여 보여 준다.
-. 기본 문법.
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
-. 2014년 이후 만들어진 영화 중 점수가 높은 것부터 보여주는 SQL 문.
SELECT * FROM movies
WHERE year > 2014
ORDER BY imdb_rating DESC;
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
56 |
Inside
Out |
comedy |
2015 |
8.6 |
7 |
Avengers: Age of Ultron |
action |
2015 |
7.9 |
17 |
Furious Seven |
action |
2015 |
7.4 |
2 |
Jurassic World |
action |
2015 |
7.3 |
156 |
Cinderella |
romance |
2015 |
7.1 |
58 |
Minions |
comedy |
2015 |
6.7 |
171 |
Fifty Shades of Grey |
romance |
2015 |
4.2 |
예8) LIMIT 절로 출력 개수를 제한 하기.
-. 2014년 이후 만들어진 영화 중 점수가 높은 것 3개만 보여준다.
SELECT * FROM movies
ORDER BY imdb_rating DESC
LIMIT 3;
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
4 |
The
Dark Knight |
action |
2008 |
9 |
30 |
Inception |
action |
2010 |
8.8 |
32 |
Star Wars: Episode V - The Empire Strikes
Back |
action |
1980 |
8.8 |
-. 2014년 이후 만들어진 영화 중 점수가 낮은 것 3개만 보여준다.
SELECT * FROM movies
ORDER BY imdb_rating ASC
LIMIT 3;
-. 실행 결과
id |
name |
genre |
year |
imdb_rating |
171 |
Fifty
Shades of Grey |
romance |
2015 |
4.2 |
79 |
Alvin and the Chipmunks: The Squeakquel |
comedy |
2009 |
4.4 |
143 |
Anaconda |
horror |
1997 |
4.6 |