Why data types matter
Every column in a SQL table has a data type. The type decides which values the column can hold, how much storage each value takes, and which operations make sense on it. A numeric column can be summed and averaged, while a text column can’t be added up unless you convert it first. Most databases also reject values that don’t fit the type, which catches many data entry mistakes early.
The types below fall into a few families, and guides don’t agree on how many families there are. See “What are the four types of data types?” for one common split. The names and rules also vary between products, so check your database’s manual for the exact details.
Numbers: whole, exact and approximate
Numeric types come in three common forms:
- Whole numbers such as INTEGER (often written INT), SMALLINT and BIGINT. They suit counts, IDs and quantities. The main difference between them is the range of values they accept.
- Exact decimals such as DECIMAL and NUMERIC. You set a precision (the total number of digits) and a scale (the digits after the decimal point). Use them for money and for any value where rounding has to be predictable.
- Approximate numbers such as FLOAT, REAL and DOUBLE PRECISION. They handle very large and very small values, but many decimal fractions can’t be stored exactly. A value such as 0.1 is one example. Avoid them for currency.
Integer division is a common surprise. In PostgreSQL and SQL Server, SELECT 7 / 2; returns 3 because both numbers are whole. Writing 7.0 / 2 gives 3.5. The same trap can affect averages, and a worked example is in how to calculate average for each row in SQL.
Text: fixed, variable and unlimited
Character types hold text:
- CHAR(n) is fixed-length. Shorter values are padded with spaces up to n characters.
- VARCHAR(n) is variable-length, up to n characters. It stores only the characters you give it.
- TEXT holds long strings without a set length. Some products cap how long a TEXT value can be, so check yours.
PostgreSQL’s documentation notes that CHAR(n) is usually the slowest of its three text types, and it recommends VARCHAR or TEXT in most situations. CHAR still suits codes that are always the same length, such as two-letter country codes. Padding adds spaces you didn’t type, so if you need to strip them, see “What does trim() do in SQL?”
SQL Server adds NCHAR and NVARCHAR for Unicode text, which covers characters from many languages.
Dates and times
- DATE stores a calendar date.
- TIME stores a time of day.
- TIMESTAMP stores a date and a time. PostgreSQL’s TIMESTAMP WITH TIME ZONE stores values in UTC and shows them in the session’s time zone.
- DATETIME is the date-and-time type in MySQL and SQL Server. SQL Server also has DATETIME2, which has a wider range and finer precision.
Products differ in how they handle these. In MySQL, TIMESTAMP converts values to UTC for storage and covers only dates from 1970 to January 19, 2038, so use DATETIME for dates outside that range. SQLite has no date type at all. It stores dates as text, numbers or integers, and your application picks the format.
Store dates in a real date or timestamp type rather than in text. Text such as 11/28/2026 sorts in the wrong order, and it reads differently in different countries. Real date types let the database sort, filter and subtract dates correctly.
Boolean
Standard SQL has a BOOLEAN type that holds TRUE, FALSE or UNKNOWN, and UNKNOWN is stored as NULL. Products don’t all follow this:
- PostgreSQL has a native boolean type.
- MySQL’s BOOLEAN is a synonym for TINYINT(1), where zero is false and any nonzero value is true.
- SQL Server has no boolean type. It uses BIT, which holds 1 or 0.
- SQLite stores true and false as the integers 1 and 0.
If you move a query between products, test its true and false conditions rather than assuming they behave the same way.
Binary and large objects
Binary types hold raw bytes, such as images or files. Standard SQL uses BLOB for large binary objects and CLOB for large character objects. Products use their own names, such as BYTEA in PostgreSQL, VARBINARY in SQL Server and BLOB in MySQL. Many teams keep large files in file or object storage and store only a reference in the database, which is a common way to keep tables smaller.
JSON and XML
The standard added JSON functions in SQL:2016 and a JSON data type in SQL:2023. PostgreSQL offers json and jsonb, and SQL Server has json and xml types. JSON suits data whose shape varies from row to row. When every row has the same fields, separate columns are easier to check, index and query.
Choosing a type
A few habits cover most cases:
- Pick the narrowest type that fits the data. Set a length limit only when you know the real maximum, since PostgreSQL’s documentation advises against making up an arbitrary limit.
- Use DECIMAL for money, not FLOAT.
- Store dates and times in date and time types, as described above.
- Declare a column NOT NULL when a value must always exist. Most columns accept NULL by default, and NULL isn’t the same as zero or an empty string. For replacing missing values, see “How to get 0 instead of NULL in SQL?”
Differences across databases
The standard gives you a common vocabulary, but products add, rename and restrict types. SQLite is the outlier. Its declared column types are only recommendations, and it converts values toward them where it can. Since version 3.37.0, STRICT tables enforce the declared types.
MySQL adds another layer of choice at the table level, covered in types of mysql tables and storage engines. For how the standard and its dialects differ, see “What are the different versions of SQL?”
New to the language? “Is SQL hard to learn?” covers what else to expect alongside these types.