Group functions

Introduction to Oracle SQL

Hadrien Lacroix

Content Developer

Aggregating data

Aggregating data

Introduction to Oracle SQL

Group functions

 

 

Group functions

  • SUM
  • AVG
  • MEDIAN
  • MIN
  • MAX
  • COUNT
Introduction to Oracle SQL

SUM

SELECT SUM(Milliseconds)
FROM Track
| SUM(Milliseconds) |
|-------------------|
| 1,378,778,040     |
Introduction to Oracle SQL

AVG, MEDIAN

SELECT AVG(Milliseconds), MEDIAN(Milliseconds)
FROM Track
| AVG(Milliseconds) | MEDIAN(Milliseconds) |
|-------------------|----------------------|
| 393,599.2         | 255,634              |
Introduction to Oracle SQL

MIN, MAX

SELECT MIN(Milliseconds), MAX(Milliseconds)
FROM Track
| MIN(Milliseconds) | MAX(Milliseconds) |
|-------------------|-------------------|
| 1,071             | 5,286,953         |
Introduction to Oracle SQL

COUNT

-- Number of rows in a table
SELECT COUNT(*)
FROM Track
-- Number of rows with non-null values
SELECT COUNT(Milliseconds)
FROM Track
-- Number of distinct non-null values
SELECT COUNT(DISTINCT Milliseconds)
FROM Track
| COUNT(*) |
|----------|
| 3503     |
| COUNT(Milliseconds) |
|---------------------|
| 3503                |
| COUNT(DISTINCT Milliseconds) |
|------------------------------|
| 3080                         |
Introduction to Oracle SQL

Column aliases

SELECT MIN(Milliseconds)
       SUM(Milliseconds)
FROM Track
| MIN(Milliseconds) | SUM(Milliseconds) |
|-------------------|-------------------|
| 1,071             | 1,378,778,040     |
SELECT MIN(Milliseconds) AS minimum
       SUM(Milliseconds) AS "Total Duration"
FROM Track
| MINIMUM           | Total Duration    |
|-------------------|-------------------|
| 1,071             | 1,378,778,040     |
Introduction to Oracle SQL

Data types

Numeric data Character data Date data
AVG x
SUM x
MIN x x x
MAX x x x
COUNT x x x
Introduction to Oracle SQL

Character

CHAR, VARCHAR2

SELECT Name 
FROM Track
| Name           |
|----------------|
| Ashes To Ashes |
| Layla          |
| Everlong       |
| ...            |
Introduction to Oracle SQL

Numeric

NUMBER

SELECT Milliseconds 
FROM Track
| Milliseconds |
|--------------|
| 343719       |
| 205688       |
| 267728       |
| ...          |
Introduction to Oracle SQL

Date

DATE, DATETIME

SELECT BirthDate 
FROM Employee
| BirthDate  |
|------------|
| 1962-02-18 |
| 1958-12-08 |
| 1973-08-29 |
| ...        |
Introduction to Oracle SQL

Let's practice!

Introduction to Oracle SQL

Preparing Video For Download...