Repository navigation
Expand file tree
/
Copy path13_Aggregate_Functions.sql
More file actions
59 lines (49 loc) · 1.86 KB
/
Copy path13_Aggregate_Functions.sql
File metadata and controls
59 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* ==============================================================================
SQL Aggregate Functions
---------------------------------------------------------------------------------
This document provides an overview of SQL aggregate functions, which allow
performing calculations on multiple rows of data to generate summary results.
Table of Contents:
1. BASIC AGGREGATE FUNCTIONS
- COUNT
- SUM
- AVG
- MAX
- MIN
2. GROUPED AGGREGATIONS | GROUP BY
=================================================================================
*/
-- Run this line first: every query in this script uses MyDatabase
USE MyDatabase;
/* ==============================================================================
1. BASIC AGGREGATE FUNCTIONS
============================================================================== */
-- Find the total number of customers
SELECT COUNT(*) AS total_customers
FROM customers
-- Find the total sales of all orders
SELECT SUM(sales) AS total_sales
FROM orders
-- Find the average sales of all orders
SELECT AVG(sales) AS avg_sales
FROM orders
-- Find the highest score among customers
SELECT MAX(score) AS max_score
FROM customers
-- Find the lowest score among customers
SELECT MIN(score) AS min_score
FROM customers
/* ==============================================================================
2. GROUPED AGGREGATIONS | GROUP BY
============================================================================== */
-- Find the number of orders, total sales, average sales,
-- highest sales, and lowest sales per customer
SELECT
customer_id,
COUNT(*) AS total_orders,
SUM(sales) AS total_sales,
AVG(sales) AS avg_sales,
MAX(sales) AS highest_sales,
MIN(sales) AS lowest_sales
FROM orders
GROUP BY customer_id