SlideShare a Scribd company logo
02 Advanced SQL
2019-02-27 유동호
Aggregates
• AVG, MIN, MAX, SUM, COUNT 등

• SELECT 의 Output 리스트에서만 사용 가능

• COUNT, AVG, SUM 은 DISTINCT 를 지원한다.

• SELECT 에 집계되지 않은 값이 있다면 undefined 이다.
Group by & Having
• Group by

• Tuple 을 서브셋으로 구성하며 각 서브셋에 대한 집계를 계산

• SELECT 절에서 집계되지 않은 값은 GROUP BY 절에 존재해야함

• Having

• Aggregation 계산을 기반으로 필터링

• GROUP BY 의 WHERE 절과 같다.
SELECT AVG(s.gpa) AS avg_gpa, e.cid
FROM enrolled AS e, student AS s
WHERE e.sid = s.sid -- AND avg_gpa > 3.9
GROUP BY e.cid
HAVING avg_gpa > 3.9 -- after aggregates
String Operations
• LIKE

• 문자열 탐색을 위해 사용됨 

• String-matching operators

• ‘%’: 부분 문자열(빈 문자열 포함)을 찾는다.

• ‘_’: 문자 하나를 찾는다.

• SUBSTRING: 문자열 추출

• LOWER, UPPER: 대소문자 변환

• CONCAT: 문자열 연결

• SQL 표준에서는 ‘||’ 로 문자열을 연결 가능
DBMSs String Case String Quotes
SQL-92 Sensitive Single Only
Postgres Sensitive Single Only
MySQL Insensitive Single/Double
SQLite Sensitive Single/Double
DB2 Sensitive Single Only
Oracle Sensitive Single Only
SELECT * FROM student AS s WHERE s.login LIKE '%@c_';
Nested Queries
Window Functions
• 단일 행과 Tuple 셋을 통해 계산을 수행한다.

• Aggregation 과 비슷하지만 결과가 Single tuple 로 그룹핑되지 않는다.
SELECT ... FUNC_NAME(...) OVER (...) FROM tableName;
Common Table Expressions
• 규모가 있는 쿼리에 사용할 보조 쿼리를 작성하는 방법을 제공

• Nested query 와 View 의 대안 중 하나이다.

More Related Content

More from Dongho Yu

2019 lightning talk_4
2019 lightning talk_42019 lightning talk_4
2019 lightning talk_4
Dongho Yu
 
2019 lightning talk_3
2019 lightning talk_32019 lightning talk_3
2019 lightning talk_3
Dongho Yu
 
2019 lightning talk_1
2019 lightning talk_12019 lightning talk_1
2019 lightning talk_1
Dongho Yu
 
Cache
CacheCache
Cache
Dongho Yu
 
Main memory
Main memoryMain memory
Main memory
Dongho Yu
 
Control unit
Control unitControl unit
Control unit
Dongho Yu
 
Instruction cycle
Instruction cycleInstruction cycle
Instruction cycle
Dongho Yu
 
Cpu basic
Cpu basicCpu basic
Cpu basic
Dongho Yu
 
Computer system
Computer systemComputer system
Computer system
Dongho Yu
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
Dongho Yu
 
Intro to algorithms
Intro to algorithmsIntro to algorithms
Intro to algorithms
Dongho Yu
 

More from Dongho Yu (11)

2019 lightning talk_4
2019 lightning talk_42019 lightning talk_4
2019 lightning talk_4
 
2019 lightning talk_3
2019 lightning talk_32019 lightning talk_3
2019 lightning talk_3
 
2019 lightning talk_1
2019 lightning talk_12019 lightning talk_1
2019 lightning talk_1
 
Cache
CacheCache
Cache
 
Main memory
Main memoryMain memory
Main memory
 
Control unit
Control unitControl unit
Control unit
 
Instruction cycle
Instruction cycleInstruction cycle
Instruction cycle
 
Cpu basic
Cpu basicCpu basic
Cpu basic
 
Computer system
Computer systemComputer system
Computer system
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Intro to algorithms
Intro to algorithmsIntro to algorithms
Intro to algorithms
 

2019 lightning talk_2

  • 2. Aggregates • AVG, MIN, MAX, SUM, COUNT 등 • SELECT 의 Output 리스트에서만 사용 가능 • COUNT, AVG, SUM 은 DISTINCT 를 지원한다. • SELECT 에 집계되지 않은 값이 있다면 undefined 이다.
  • 3. Group by & Having • Group by • Tuple 을 서브셋으로 구성하며 각 서브셋에 대한 집계를 계산 • SELECT 절에서 집계되지 않은 값은 GROUP BY 절에 존재해야함 • Having • Aggregation 계산을 기반으로 필터링 • GROUP BY 의 WHERE 절과 같다. SELECT AVG(s.gpa) AS avg_gpa, e.cid FROM enrolled AS e, student AS s WHERE e.sid = s.sid -- AND avg_gpa > 3.9 GROUP BY e.cid HAVING avg_gpa > 3.9 -- after aggregates
  • 4. String Operations • LIKE • 문자열 탐색을 위해 사용됨 • String-matching operators • ‘%’: 부분 문자열(빈 문자열 포함)을 찾는다. • ‘_’: 문자 하나를 찾는다. • SUBSTRING: 문자열 추출 • LOWER, UPPER: 대소문자 변환 • CONCAT: 문자열 연결 • SQL 표준에서는 ‘||’ 로 문자열을 연결 가능 DBMSs String Case String Quotes SQL-92 Sensitive Single Only Postgres Sensitive Single Only MySQL Insensitive Single/Double SQLite Sensitive Single/Double DB2 Sensitive Single Only Oracle Sensitive Single Only SELECT * FROM student AS s WHERE s.login LIKE '%@c_';
  • 6. Window Functions • 단일 행과 Tuple 셋을 통해 계산을 수행한다. • Aggregation 과 비슷하지만 결과가 Single tuple 로 그룹핑되지 않는다. SELECT ... FUNC_NAME(...) OVER (...) FROM tableName;
  • 7. Common Table Expressions • 규모가 있는 쿼리에 사용할 보조 쿼리를 작성하는 방법을 제공 • Nested query 와 View 의 대안 중 하나이다.