별칭과 산술 연산

중급 SQL

Jasmin Ludolf

Data Science Content Developer, DataCamp

산술 연산

+, -, *, /

SELECT (4 + 3);
|7|
SELECT (4 * 3);
|12|
SELECT (4 - 3);
|1|
SELECT (4 / 3);
|1|
중급 SQL

산술 연산

SELECT (4 / 3);
|1|
SELECT (4.0 / 3.0);
|1.333...|
중급 SQL

집계 함수 vs. 산술 연산

집계 함수 An example of a table of data with a red box around a single column, also known as field in SQL.

산술 연산 An example of a table of data with a red box around a single record, also known as row.

중급 SQL

별칭과 산술 연산

SELECT (gross - budget)
FROM films;
|?column?|
|--------|
|null    |
|2900000 |
|null    |
...
SELECT (gross - budget) AS profit
FROM films;
|profit  |
|--------|
|null    |
|2900000 |
|null    |
...
중급 SQL

함수와 별칭

SELECT MAX(budget), MAX(duration)
FROM films;
|max        |max|
|-----------|---|
|12215500000|334|
SELECT MAX(budget) AS max_budget,
       MAX(duration) AS max_duration
FROM films;
|max_budget |max_duration|
|-----------|------------|
|12215500000|334         |
중급 SQL

실행 순서

  • 1단계: FROM
  • 2단계: WHERE
  • 3단계: SELECT (여기서 별칭 정의)
  • 4단계: LIMIT
  • 실행 순서로 인해 SELECT 절에 정의된 별칭은 WHERE 절에서 사용할 수 없음
SELECT budget AS max_budget
FROM films
WHERE max_budget IS NOT NULL;
column "max_budget" does not exist
LINE 5: WHERE max_budget IS NOT NULL;
              ^
중급 SQL

연습해봅시다!

중급 SQL

Preparing Video For Download...