SQL Data Types: Integers, Decimal, Text, Dates and Boolean

A data type defines how a column represents values and which ranges, comparisons and calculations are appropriate for those values.

Lesson 52 of 102Intermediate10 min read

A data type defines how a column represents values and which ranges, comparisons and calculations are appropriate for those values.

Choosing a type is choosing a meaning, not just a place to store characters. A telephone number is usually text, a price needs a monetary precision policy, and a date needs consistent calendar rules. Good types prevent many mistakes before a query is written.

This reference adds detail to the main course. You can read it independently: the small database below supplies every table and row used in the examples. Start with the question each query answers, then compare the returned values with the data rather than memorising a line of syntax.

Set up the practice database

Run this setup once in a new practice database, before the examples. It uses four customers and six sales. Ana has two sales, Ben has one, Cara has two, and Dev has none.

Sale 106 has no customer link; sale 104 has an unknown amount. These deliberate test cases expose missing information and unmatched relationships.

SQL
CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name VARCHAR(40) NOT NULL,
  city VARCHAR(40),
  manager_id INTEGER
);
CREATE TABLE sales (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER,
  amount DECIMAL(10,2),
  status VARCHAR(20) NOT NULL,
  created_on DATE NOT NULL
);
INSERT INTO customers (id,name,city,manager_id) VALUES
  (1,'Ana','Leeds',NULL),(2,'Ben','Leeds',1),
  (3,'Cara','Porto',1),(4,'Dev',NULL,2);
INSERT INTO sales (id,customer_id,amount,status,created_on) VALUES
  (101,1,10,'paid','2026-01-01'),
  (102,1,30,'pending','2026-01-02'),
  (103,2,20,'paid','2026-01-02'),
  (104,3,NULL,'pending','2026-02-01'),
  (105,3,20,'paid','2026-02-02'),
  (106,NULL,5,'paid','2026-02-03');

An amount of zero would be a known value. The NULL amount here means the value is unknown. A customer with no matching sale differs from a sale whose customer_id is NULL.

The primary keys identify individual records even when amounts or cities repeat. Dates use year-month-day format. The examples specify sorting whenever order matters; insertion order alone does not guarantee display order.

How SQL data types works

Integer types hold whole numbers such as counts and identifiers. Their ranges vary by type and database. A code composed of digits is not automatically an integer: a postal code or phone number may need leading zeros and can contain nonnumeric characters. Choose text for identifiers whose digit spelling matters more than arithmetic. A primary key can use different types as long as it meets its identity requirements.

DECIMAL and NUMERIC describe exact decimal precision in common server databases. A declaration such as DECIMAL(10,2) commonly permits ten total digits with two after the decimal point. Floating-point types approximate a range of numbers and are useful for scientific measurements, but can represent decimal fractions imperfectly. Financial calculations should define currency, precision and rounding explicitly. SQLite’s DECIMAL declaration uses numeric affinity and does not supply the same fixed-decimal enforcement.

Text types and encodings

Text types include CHAR, VARCHAR and engine-specific large-text types. CHAR is fixed-width in many systems and can involve padding behaviour, while VARCHAR is variable-length subject to the engine’s length rules. Unicode support depends on encoding and type choices; SQL Server distinguishes common varchar and nvarchar use, although collation choices also matter. Character count, byte count and collation are separate concerns. Avoid a length limit chosen only because the four sample names happen to fit.

DATE represents a calendar date, while timestamp types describe date-and-time values with different timezone semantics. Boolean support varies: PostgreSQL has BOOLEAN, MySQL BOOLEAN is a synonym for a small integer type, SQL Server commonly uses BIT, and SQLite relies on its dynamic representations and affinity rules. Binary values and structured JSON also have dialect-specific types or functions. A portable schema may share a conceptual model while needing local type declarations to enforce that model correctly. Constraints add business rules that a type alone cannot express, such as a nonnegative amount or a required relationship.

MySQL, PostgreSQL, SQL Server and SQLite

Server database types often enforce range and representation rules that ordinary SQLite tables do not enforce the same way. SQLite STRICT tables provide stronger checks but accept a restricted set of type names; DECIMAL is not one of those names. BOOLEAN, timestamp-zone types, JSON and auto-generated identity declarations are not uniform across engines.

Check the engine-specific syntax and version requirements in SQLite datatypes and affinity, PostgreSQL data types.

Practice task and worked solution

Show sales with known amounts below twenty. Use a numeric literal rather than a quoted display label.

Try the task before reading its worked solution. The supplied rows make the expected answer checkable by hand.

SQL
SELECT id,amount FROM sales
WHERE amount<20 ORDER BY id;
Output
id | amount
101 | 10
106 | 5

Ten and five pass the numeric threshold. The known twenties and thirty do not, and the unknown amount is not established as less than twenty. A client’s display of trailing decimal zeros does not change the numerical comparison.

Compare text sorting of the literal labels '100' and '20' with numeric sorting after an appropriate conversion. Next inspect the declared amount type in metadata and distinguish that declaration from SQLite’s actual stored value categories. For an engine with strong typed columns, test out-of-range and malformed inputs in a disposable table so validation expectations are based on observed enforcement.

SQL Data Types: Integers, Decimal, Text, Dates and Boolean Syntax

SQL data types

SELECT id,amount,created_on FROM sales
WHERE amount>=20 ORDER BY id;

SQL Data Types: Integers, Decimal, Text, Dates and Boolean Summary

  • Choose a type for the meaning of the data, not just its current appearance.
  • Use integer types for counts and ids, DECIMAL or whole minor units for money, and avoid FLOAT for currency.
  • Store phone numbers, postcodes and codes with leading zeros as text, even if they contain only digits.
  • Dates and timestamps belong in temporal types; decide whether a timestamp needs time zone information.
  • Types don't replace constraints: add CHECK, NOT NULL and UNIQUE for business rules.

SQL Data Types: Integers, Decimal, Text, Dates and Boolean Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Comparing a numeric amount and a date

amount is declared as a numeric measure, so the threshold is numeric rather than a textual ordering rule. created_on carries the sample date representation. The NULL amount does not become a numeric zero and cannot satisfy the comparison.

SQL
SELECT id,amount,created_on FROM sales
WHERE amount>=20 ORDER BY id;
Output
id | amount | created_on
102 | 30 | 2026-01-02
103 | 20 | 2026-01-02
105 | 20 | 2026-02-02

Common SQL Data Types: Integers, Decimal, Text, Dates and Boolean Mistakes

Prices as text and phone numbers as integers

Storing price as VARCHAR makes valid numeric order depend on later conversions and may admit malformed values. Treating a phone number as an integer can discard leading zeros. Select a type for the domain meaning, then add constraints for requirements such as positive quantity or nonempty labels.

SQL Data Types: Integers, Decimal, Text, Dates and Boolean in Real Projects

Choosing types from requirements

A requirements discussion can list each field’s meaning, allowed range, missing-value policy and comparison rules before choosing a local database type. This avoids a schema that technically stores today’s data but fails when international text, larger amounts or timezone-sensitive timestamps arrive.

For each schema field, record a domain range, unit, missing-value policy and comparison meaning. A type should support those requirements rather than merely accepting today’s sample. Preserve leading zeros in textual identifiers, choose a money representation deliberately, and define timestamp-zone semantics before different applications write incompatible interpretations into one column.

SQL Data Types: Integers, Decimal, Text, Dates and Boolean FAQ

Should every digit-only value be numeric?

No. Values such as telephone numbers, postcodes and account codes are textual identifiers: you never add them up, and storing them as numbers loses leading zeros. Use a numeric type only for values you calculate with or compare by size.

Does DECIMAL mean the same thing in SQLite?

No. Ordinary SQLite uses type affinity rather than server-style fixed precision, so DECIMAL(12,2) doesn't limit values to two decimal places and may store them as floating point. Store money as integer cents or round explicitly in SQLite.

Do types replace constraints?

No. A numeric type does not by itself require positive values, and a text type doesn't stop empty strings or duplicates. Add CHECK (quantity > 0), NOT NULL and UNIQUE for the rules your data must follow.

Which integer type should I use for ids?

A 32-bit INTEGER holds up to about 2.1 billion values, which is plenty for most tables. Use BIGINT for tables that may grow very large or where ids are consumed quickly, such as events and logs. Changing an id type later is painful, so choose generously for high-volume tables.

Is there a BOOLEAN type in every database?

PostgreSQL has a true BOOLEAN. MySQL accepts BOOLEAN but stores it as TINYINT(1), SQL Server uses BIT, and SQLite stores TRUE and FALSE as the integers 1 and 0. Writing conditions as WHERE is_active = TRUE or = 1 depends on the database.

SQL Data Types: Integers, Decimal, Text, Dates and Boolean Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choicemedium

Which sale ids have a known amount below twenty?

SQL Data Types: Integers, Decimal, Text, Dates and Boolean Quiz

2 questions to check what you learned.

  1. Which field is usually best treated as text?

    • A quantity that must be added numerically
    • A telephone number whose leading zeros matter
    • A monetary measure whose arithmetic precision matters
  2. What adds a nonnegative business rule to a numeric column?

    • A VARCHAR declaration that makes every saved number positive
    • A DEFAULT value, which rejects any alternative input
    • A suitable CHECK constraint