SQL Keywords

SQL DATA TYPES

What are SQL DATA TYPES?

SQL data types specify the kind of values that columns, variables, and expressions can store and how those values are processed.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL DATA TYPES: Supported by PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, BigQuery, Redshift, and all ANSI-compliant databases

SQL DATA TYPES Full Explanation

SQL data types form the backbone of a database schema. They dictate storage format, valid operations, and comparison rules for every value stored in a database. Standard SQL groups types into categories such as numeric (INTEGER, DECIMAL, FLOAT), character (CHAR, VARCHAR), boolean (BOOLEAN), temporal (DATE, TIME, TIMESTAMP), binary (BLOB, BYTEA), and special-purpose types like JSON, XML, spatial, and enumerated (ENUM). Each database adds proprietary extensions, but all systems rely on the same core principle: a value can only occupy a column if its type is compatible. Choosing the correct type impacts performance, disk footprint, index efficiency, accuracy, and data integrity. Numeric precision, string length, time zone handling, and locale collation rules vary by dialect and must be considered during design. Types can be cast or converted, but implicit conversion rules differ, which may lead to runtime errors or silent truncation. SQL also allows user-defined types (domains, composite, or custom classes) that encapsulate additional constraints. Nullability is orthogonal to type: NULL represents an unknown value of any declared type.

SQL DATA TYPES Syntax

-- Declare a column type inside a CREATE TABLE statement
CREATE TABLE table_name (
    column_name DATA_TYPE [type_modifiers] [NULL | NOT NULL] [DEFAULT expression]
);

-- Explicitly cast between types
SELECT CAST(expression AS DATA_TYPE);

-- Example with parameters
VARCHAR(255)

SQL DATA TYPES Parameters

Example Queries Using SQL DATA TYPES

-- 1. Table with multiple data types
CREATE TABLE orders (
    order_id      SERIAL PRIMARY KEY,
    order_total   NUMERIC(10,2) NOT NULL,
    currency      CHAR(3)       DEFAULT 'USD',
    paid          BOOLEAN       DEFAULT FALSE,
    placed_at     TIMESTAMP     DEFAULT NOW(),
    meta          JSONB
);

-- 2. Casting a string to a date
SELECT CAST('2024-06-01' AS DATE) AS order_date;

-- 3. Altering column type with USING clause (PostgreSQL)
ALTER TABLE orders
ALTER COLUMN order_total TYPE MONEY USING order_total::MONEY;

Expected Output Using SQL DATA TYPES

  • Query 1 creates a table whose columns can only store values matching their declared types
  • Query 2 returns a DATE literal
  • Query 3 changes the column storage to MONEY after converting existing values

Use Cases with SQL DATA TYPES

  • Designing new tables and choosing optimal column definitions
  • Controlling numeric precision for financial calculations
  • Storing large unstructured blobs or JSON documents
  • Enforcing valid boolean or enumerated states
  • Converting data during ETL or migration workflows

Common Mistakes with SQL DATA TYPES

  • Omitting length on VARCHAR where required (e.g., VARCHAR without size in MySQL < 8.0)
  • Picking FLOAT for money values causing rounding errors
  • Assuming TIMESTAMP always stores time zone information (depends on dialect)
  • Inserting strings that exceed defined length causing silent truncation
  • Forgetting to cast when comparing numbers and strings, leading to implicit conversions and slow queries

Related Topics

CAST, CONVERT, CREATE TABLE, ALTER TABLE, DOMAIN, ENUM, COLLATION

First Introduced In

ANSI SQL-86

Frequently Asked Questions

What is the difference between CHAR and VARCHAR?

CHAR stores a fixed number of characters, padding with spaces as needed. VARCHAR stores variable-length strings up to the defined limit, saving space for uneven text.

When should I use NUMERIC instead of FLOAT?

Choose NUMERIC (DECIMAL) for exact precision like currency. FLOAT and REAL are approximate and can introduce rounding errors at scale.

How do I change the type of an existing column?

Use ALTER TABLE with ALTER COLUMN ... TYPE. Provide a USING clause or CAST to convert existing data safely, e.g., `ALTER TABLE t ALTER COLUMN c TYPE BIGINT USING c::BIGINT;`.

Why does my DATE insert fail?

Your string format likely does not match the database's default date format. Cast explicitly or set the correct `DATEFORMAT` or `lc_time` configuration.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!