DECIMAL — SQL Technical Interview Questions & Answers
DECIMAL
DECIMAL
- DECIMAL(10,2) is exact: 10 digits, 2 after the point — money, percentages you must not round wrongly. FLOAT/DOUBLE are approximate binary. Banks and fees: DECIMAL.
- 10,2 means 8 digits before the point + 2 after (total 10). Don’t guess — say ‘precision and scale’.
- Trap: FLOAT for salary.
- On the example next to this theory: DECIMAL: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
Technical Interview Tip: fee DECIMAL(10,2) NOT NULL
Code Examples for DECIMAL
DECIMAL — sample query
-- DECIMAL
CREATE TABLE demo (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL,
qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
(1, 'alpha', 2),
(2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;DECIMAL: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
How to Explain DECIMAL to an Interviewer
Why DECIMAL for money?
Exact decimal FLOAT rounds badly