Full answer
Integer division does not apply to AVG: both engines return a fractional result. The type differs.
sql
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');SQLite returns a REAL (a double-precision float):
sql
SELECT AVG(salary) AS avg_salary, typeof(AVG(salary)) AS type FROM employees WHERE dept_id = 1;| avg_salary | type |
|---|---|
| 103333.33333333333 | real |
PostgreSQL returns numeric, an exact decimal, and shows a long scale:
sql
SELECT AVG(salary) AS avg_salary, pg_typeof(AVG(salary)) AS type FROM employees WHERE dept_id = 1;| avg_salary | type |
|---|---|
| 103333.333333333333 | numeric |
For display, round explicitly:
sql
SELECT ROUND(AVG(salary), 2) AS avg_salary FROM employees WHERE dept_id = 1;| avg_salary |
|---|
| 103333.33 |
sql
SELECT ROUND(AVG(salary), 2) AS avg_salary FROM employees WHERE dept_id = 1;| avg_salary |
|---|
| 103333.33 |
A float can show artefacts in other calculations (0.1 + 0.2 is not exactly 0.3 in binary floating point). PostgreSQL's numeric avoids that at some speed cost. For money, store whole units such as cents in integers, or use a decimal type, and round only at the final display step.
sql
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
SELECT AVG(salary) AS avg_salary, typeof(AVG(salary)) AS type FROM employees WHERE dept_id = 1;
SELECT ROUND(AVG(salary), 2) AS avg_salary FROM employees WHERE dept_id = 1;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- SQLite AVG returns REAL; PostgreSQL AVG(integer) returns numeric
- AVG does not truncate like integer division
- ROUND(AVG(x), 2) for display
- Store money as integers or decimals, not floats
Common mistakes
- Comparing a float average for exact equality
- Rounding intermediate values and losing precision in totals
Follow-ups
- How would you format the rounded average with a thousands separator?
- Why are floats a poor fit for money?
Tips
- Mention the type, not just the value; it explains the different output