QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Database basics

Which data types should QA know?

QA should know column types because a type decides which values the database accepts and gives a ready list of boundary values. The main ones are integers (INTEGER ends at 2,147,483,647), NUMERIC for money, REAL and DOUBLE PRECISION with rounding error, strings, BOOLEAN, date and time (TIMESTAMP or TIMESTAMPTZ), UUID and JSONB. A mismatch of types between UI, API and database is itself a source of bugs.

SQL · Basics

The "Amount" field accepts 0.1 + 0.2, but the database holds 0.30000000000000004. Which type was chosen?

A bug in a report: the sum of two payments of 0.10 and 0.20 does not equal 0.30. The tester looks at the schema and sees that the column has a floating-point type. A column type decides which values the database accepts, how much space they take, how they compare and where the boundary it will not cross lies. So the type is a source of boundary values for tests. Below: which types are worth knowing, what breaks with them and how to check it.

How it works and why. Every column has a type, and the database checks it on write and uses it in comparison, sorting and calculations. That is why values that look identical behave differently: the string '10' sorts before '9', the number 10 after 9. The type's limits are a ready list of boundary values: the largest number, the longest string, the number of decimal places.

TypeUsed forWhat to watch
INTEGER, BIGINTIntegers, IDsINTEGER ranges from −2,147,483,648 to 2,147,483,647; BIGINT is much larger
NUMERIC(p,s)Exact decimals, moneyp is total digits, s after the point; NUMERIC(14,2) allows up to 12 digits before the point
REAL, DOUBLE PRECISIONApproximate decimalsThey carry an error, not for money
VARCHAR(n), TEXTStringsWhat happens to a string longer than n
BOOLEANYes / noCan also be NULL
DATE, TIMESTAMP, TIMESTAMPTZDates and timeWith or without a time zone
UUID, JSONBUnique IDs, flexible dataFormat; nested fields are not checked by the schema

Example: money in the "wrong" type. The amount column is declared as DOUBLE PRECISION. Binary floating point cannot store 0.1 exactly, so sums accumulate an error:

SELECT 0.1::double precision + 0.2::double precision AS f,
       0.1::numeric + 0.2::numeric AS n;
-- f = 0.30000000000000004   n = 0.3
SELECT 0.1::double precision + 0.2::double precision
     = 0.3::double precision;   -- false

Money: two types

Risk

DOUBLE PRECISION

An approximate value. 0.1 + 0.2 is not 0.3, and reconciling a balance with the history drifts by cents.

Correct

NUMERIC(14,2)

Exact decimal arithmetic. The schema sets how many decimal places are kept, extra ones are rounded.

Figure 1. For amounts and balances use NUMERIC or integers in the smallest unit (cents). A float for money is a schema defect.

The schema-level fix is NUMERIC(14,2). But it has limits too. Here is what happens to input values:

NUMERIC(14,2) on input

sent
value
99.50
1.239
999999999999.99
1000000000000 rejected
stored
value
99.50
1.24
999999999999.99
Figure 2. Extra decimal places are rounded, while a number that does not fit into 12 digits before the point is rejected with a "numeric field overflow" error.

Other typical boundaries for tests:

SELECT 2147483647 + 1;
-- ERROR: integer out of range

INSERT INTO users (email) VALUES (repeat('a', 300));
-- for varchar(255): ERROR: value too long
-- for type character varying(255)

Strings: in PostgreSQL inserting a value longer than varchar(n) is an error. In MySQL it depends on the mode: in strict mode (the default since 5.7) it is also an error, while in non-strict mode the value is silently truncated. That is a separate test case, because truncation is invisible in both the UI and the logs. Dates: TIMESTAMP stores time as is, without a zone, while TIMESTAMPTZ in PostgreSQL stores the moment in UTC and shows it in the session time zone, so different clients see a different "wall clock" for the same record.

Limits and special cases. A database type is not always an API type: JSON has no separate type for dates or for exact numbers, so a NUMERIC sent as a JSON number becomes a floating-point number in JavaScript and may lose precision. So a mismatch of types between UI, API and database is itself a source of bugs: the UI allows 300 characters and the column holds 255. For statuses people often use text with a constraint or an enum, and then values outside the list must be rejected.

Common mistakes.

  • Storing money in REAL or DOUBLE PRECISION: an error accumulates.
  • Not checking the upper bound of INTEGER for counters and IDs that grow for years.
  • Forgetting that the length in the UI and in the database can differ.
  • Comparing TIMESTAMP and TIMESTAMPTZ as if they were the same: the difference shows in another time zone.
  • Assuming JSONB validates structure: the schema does not know which fields are inside.

Check yourself

The amount column has type NUMERIC(14,2). What happens to the input values 12.345 and 1000000000000?

Answer

12.345 is rounded to two places: 12.35 in PostgreSQL, because numeric rounds ties away from zero, while 1000000000000 does not fit into 12 digits before the point, so the database returns an overflow error. Both values are worth checking through the API and the UI.

Why is BIGINT often chosen for IDs although INTEGER looks sufficient?

Answer

INTEGER ends at 2,147,483,647. In a table with many inserts over years of work this limit can be reached, and then inserts fail with an error. BIGINT removes the problem for decades.

In an interview. Junior: Most often it is integers for IDs, NUMERIC for money, strings, booleans and dates. The column type is a source of boundary values for me: string length, decimal places, number range. Middle/Senior: Money is kept in NUMERIC or in cents, because float carries an error. I check INTEGER bounds, the behaviour with a longer string (an error or silent truncation depending on the DBMS) and the difference between TIMESTAMP and TIMESTAMPTZ. A mismatch of types between UI, API and database is a source of defects on its own.

More articles in this chapter