Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 633cd7f

Browse files
committed
DB - Aggregate functions
Signed-off-by: Serhii Horodilov <sgorodil@gmail.com>
1 parent 5c74645 commit 633cd7f

File tree

5 files changed

+339
-0
lines changed

5 files changed

+339
-0
lines changed

assets/img/aggregate-function.svg

Lines changed: 78 additions & 0 deletions
Loading
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
-- Dumped from database version 13.1 (Debian 13.1-1.pgdg100+1)
6+
-- Dumped by pg_dump version 14.5
7+
8+
SET statement_timeout = 0;
9+
SET lock_timeout = 0;
10+
SET idle_in_transaction_session_timeout = 0;
11+
SET client_encoding = 'UTF8';
12+
SET standard_conforming_strings = on;
13+
SELECT pg_catalog.set_config('search_path', '', false);
14+
SET check_function_bodies = false;
15+
SET xmloption = content;
16+
SET client_min_messages = warning;
17+
SET row_security = off;
18+
19+
SET default_tablespace = '';
20+
21+
SET default_table_access_method = heap;
22+
23+
--
24+
-- Name: product; Type: TABLE; Schema: public; Owner: postgres
25+
--
26+
27+
CREATE TABLE public.product (
28+
id integer NOT NULL,
29+
name text NOT NULL,
30+
price integer NOT NULL,
31+
category text NOT NULL,
32+
sold integer NOT NULL
33+
);
34+
35+
36+
ALTER TABLE public.product OWNER TO postgres;
37+
38+
--
39+
-- Name: product_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
40+
--
41+
42+
CREATE SEQUENCE public.product_id_seq
43+
AS integer
44+
START WITH 1
45+
INCREMENT BY 1
46+
NO MINVALUE
47+
NO MAXVALUE
48+
CACHE 1;
49+
50+
51+
ALTER TABLE public.product_id_seq OWNER TO postgres;
52+
53+
--
54+
-- Name: product_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
55+
--
56+
57+
ALTER SEQUENCE public.product_id_seq OWNED BY public.product.id;
58+
59+
60+
--
61+
-- Name: product id; Type: DEFAULT; Schema: public; Owner: postgres
62+
--
63+
64+
ALTER TABLE ONLY public.product ALTER COLUMN id SET DEFAULT nextval('public.product_id_seq'::regclass);
65+
66+
67+
--
68+
-- Data for Name: product; Type: TABLE DATA; Schema: public; Owner: postgres
69+
--
70+
71+
COPY public.product (id, name, price, category, sold) FROM stdin;
72+
1 Samsung Galaxy 100000 Phones 500
73+
2 iPhone 13 Pro 120000 Phones 300
74+
3 MacBook Pro 350000 Laptops 100
75+
4 Lenovo ThinkPad 150000 Laptops 200
76+
5 HP Printer 20000 Printers 800
77+
6 Dell Monitor 50000 Monitors 400
78+
7 Sony Headphones 30000 Audio 0
79+
8 Bose Soundbar 70000 Audio 0
80+
9 Xbox Series X 60000 Gaming 250
81+
10 PlayStation 5 55000 Gaming 350
82+
\.
83+
84+
85+
--
86+
-- Name: product_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
87+
--
88+
89+
SELECT pg_catalog.setval('public.product_id_seq', 10, true);
90+
91+
92+
--
93+
-- Name: product product_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
94+
--
95+
96+
ALTER TABLE ONLY public.product
97+
ADD CONSTRAINT product_pkey PRIMARY KEY (id);
98+
99+
100+
--
101+
-- PostgreSQL database dump complete
102+
--
103+

src/rdbms/aggregation.txt

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
*******************************************************************************
2+
Data Aggregation
3+
*******************************************************************************
4+
5+
Here is some dummy data, that will be used as example in this section.
6+
7+
.. csv-table:: product
8+
:align: center
9+
:file: products_table.csv
10+
:header-rows: 1
11+
12+
.. only:: builder_html
13+
14+
The same dataset is available as:
15+
16+
- :download:`csv file <./products_table.csv>`
17+
- :download:`postgres dump file </../assets/sql/products.pg13-1.dump.sql>`
18+
19+
Aggregate functions
20+
===================
21+
22+
*Aggregate functions* compute a single result from a set of input values.
23+
Various RDBMS implement various built-in aggregate functions. In this section
24+
we will review 5 basic functions that are available in all RDBMS.
25+
26+
.. image:: /../assets/img/aggregate-function.svg
27+
28+
Count
29+
-----
30+
31+
The ``count(field)`` function returns a count of the number of times that
32+
``field`` is not ``NULL`` in a group.
33+
The ``count(*)`` function (with no arguments) returns the total number of rows
34+
in the group.
35+
36+
.. code-block:: sql
37+
38+
SELECT count(*) as "total products" FROM product;
39+
40+
::
41+
42+
total_products
43+
----------------
44+
10
45+
46+
Maximum value
47+
-------------
48+
49+
The ``max()`` aggregate function returns the maximum value of all values in
50+
the group. The maximum value is the value that would be returned last in
51+
an ORDER BY on the same column.
52+
Aggregate function returns ``NULL`` if and only if there are no non-NULL values
53+
in the group.
54+
55+
.. code-block:: sql
56+
57+
SELECT max(price) as "maximum price" FROM product;
58+
59+
::
60+
61+
maximum price
62+
---------------
63+
350000
64+
65+
Minimum value
66+
-------------
67+
68+
The ``min()`` aggregate function returns the minimum non-NULL value of all
69+
values in the group. The minimum value is the first non-NULL value that would
70+
appear in an ORDER BY of the column.
71+
Aggregate function returns ``NULL`` if and only if there are no non-NULL values
72+
in the group.
73+
74+
.. code-block:: sql
75+
76+
SELECT min(price) as "minimum price" FROM product;
77+
78+
::
79+
80+
minimum price
81+
---------------
82+
20000
83+
84+
Average value
85+
-------------
86+
87+
The ``avg()`` function returns the average value of all non-NULL inputs within
88+
a group. String and BLOB values that do not look like numbers are interpreted
89+
as 0. The result is always a floating point value whenever there is at least
90+
one non-NULL input even if all inputs are integers.
91+
The result is ``NULL`` if and only if there are no non-NULL inputs.
92+
93+
.. code-block:: sql
94+
95+
SELECT avg(price) as "average price" FROM product;
96+
97+
::
98+
99+
average price
100+
---------------------
101+
100500.000000000000
102+
103+
104+
Sum of values
105+
-------------
106+
107+
The ``sum()`` aggregate functions return the sum of all non-NULL values in
108+
the group.
109+
If there are no non-NULL input rows then function returns ``NULL``.
110+
111+
.. note::
112+
NULL is not normally a helpful result for the sum of no rows but
113+
the SQL standard requires it.
114+
115+
SQLite implements ``total`` aggregate function, that is similar to
116+
``sum``, but returns ``0.0`` (always floating point) if there are
117+
no non-NULL input rows.
118+
119+
.. code-block:: sql
120+
121+
SELECT sum(sold) as "total sold items" FROM product;
122+
123+
::
124+
125+
total sold items
126+
------------------
127+
3750

src/rdbms/index.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,25 @@
22
Relational Database
33
###############################################################################
44

5+
:abbr:`RDBMS (Relational Database Management System)` is a type of software
6+
that helps users to create, update, manage, and access relational databases.
7+
Relational databases organize data into tables, which consist of rows and
8+
columns. Each column represents a specific attribute of the data, while each
9+
row represents a specific instance of that data.
10+
11+
RDBMS allow users to interact with the database using
12+
:abbr:`SQL (Structured Query Language)`, which is a standard language for
13+
managing and manipulating relational databases. SQL allows users to perform
14+
a variety of operations on the database, including adding, updating, and
15+
deleting data, as well as retrieving data based on specific criteria.
16+
17+
RDBMS also provide a range of features to ensure data integrity, such as
18+
enforcing data constraints, supporting transactions, and providing backup
19+
and recovery capabilities. Additionally, RDBMS systems often provide tools
20+
for managing the database, such as user interfaces for creating and modifying
21+
tables and views, and monitoring tools for optimizing database performance.
22+
523
.. toctree::
624
:name: rdbms
25+
26+
aggregation

src/rdbms/products_table.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id,name,price,category,sold
2+
1,"Samsung Galaxy",100000,Phones,500
3+
2,"iPhone 13 Pro",120000,Phones,300
4+
3,"MacBook Pro",350000,Laptops,100
5+
4,"Lenovo ThinkPad",150000,Laptops,200
6+
5,"HP Printer",20000,Printers,800
7+
6,"Dell Monitor",50000,Monitors,400
8+
7,"Sony Headphones",30000,Audio,0
9+
8,"Bose Soundbar",70000,Audio,0
10+
9,"Xbox Series X",60000,Gaming,250
11+
10,"PlayStation 5",55000,Gaming,350

0 commit comments

Comments
 (0)