{"appId":"sql-app","version":1551,"selectedAngularVersion":20,"item":{"id":"lesson-sql-data-types","conceptKey":"sql-data-types","subjectId":"subject-sql-data-types","title":"SQL Data Types","summary":"Data types define which values a column accepts and affect validation, storage, sorting, and calculations.","baseContent":"<h2>SQL Data Types</h2><p>Data types define which values a column accepts and affect validation, storage, sorting, and calculations.</p><h3>Example</h3><pre><code>CREATE TABLE products (\n  id INTEGER PRIMARY KEY,\n  name VARCHAR(120) NOT NULL,\n  price DECIMAL(10, 2),\n  created_at TIMESTAMP\n);</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>An online shop stores customers, products, orders, and order items. The schema must prevent duplicate identities, missing required values, and orders that reference customers that do not exist.</p><h3>Advanced example</h3><pre><code>BEGIN;\n-- Preview the exact target set first.\nSELECT id FROM orders WHERE status = 'pending';\n-- Apply the SQL Data Types operation, verify affected rows, then commit.\nCOMMIT;</code></pre><h3>Expected result</h3><p>The schema or operation satisfies the stated rule, rejects invalid data, and can be verified with a repeatable query.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>Continue with the PicoStore database</h3><p>This lesson reuses <strong>picostore</strong>. Relevant tables: <code>customers, products, orders, order_items</code>. Keep the starter rows from the Introduction lesson so results remain comparable.</p><h3>Another practical example</h3><pre><code>ALTER TABLE products\n  ADD COLUMN active BOOLEAN NOT NULL DEFAULT TRUE;\n\nSELECT product_id, name, active FROM products;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-sql-reference-catalog=\"true\"><h2>SQL reference catalog: SQL Data Types</h2><p>This catalog covers the practical public SQL surface. Check the exact server-version documentation before using a vendor-specific item.</p><h3>Exact numeric types</h3><ul><li><code>SMALLINT</code> — small whole numbers</li><li><code>INTEGER / INT</code> — ordinary whole numbers</li><li><code>BIGINT</code> — large whole numbers</li><li><code>DECIMAL(p,s) / NUMERIC(p,s)</code> — exact fixed-point values such as money</li></ul><h3>Approximate numeric types</h3><ul><li><code>REAL</code> — single-precision approximate number</li><li><code>DOUBLE PRECISION</code> — double-precision approximate number</li><li><code>FLOAT(p)</code> — precision-based approximate number</li></ul><h3>Character and text types</h3><ul><li><code>CHAR(n)</code> — fixed-length text</li><li><code>VARCHAR(n)</code> — variable-length bounded text</li><li><code>TEXT</code> — large variable text; common vendor extension</li><li><code>NCHAR / NVARCHAR</code> — national character types, mainly SQL Server/MySQL compatibility</li></ul><h3>Binary types</h3><ul><li><code>BINARY(n)</code> — fixed-length bytes</li><li><code>VARBINARY(n)</code> — variable-length bytes</li><li><code>BLOB</code> — large binary object; MySQL</li><li><code>BYTEA</code> — variable binary data; PostgreSQL</li></ul><h3>Date and time types</h3><ul><li><code>DATE</code> — calendar date</li><li><code>TIME</code> — time of day</li><li><code>TIME WITH TIME ZONE</code> — time with offset support</li><li><code>TIMESTAMP</code> — date and time</li><li><code>TIMESTAMP WITH TIME ZONE</code> — absolute instant; PostgreSQL timestamptz</li><li><code>INTERVAL</code> — duration; PostgreSQL and standard SQL</li><li><code>YEAR</code> — year value; MySQL</li></ul><h3>Logical and structured types</h3><ul><li><code>BOOLEAN / BOOL</code> — true or false</li><li><code>JSON</code> — validated JSON document</li><li><code>JSONB</code> — binary searchable JSON; PostgreSQL</li><li><code>XML</code> — XML document; PostgreSQL/SQL Server</li><li><code>UUID</code> — 128-bit identifier; native PostgreSQL</li><li><code>ARRAY</code> — typed array; PostgreSQL</li><li><code>ENUM</code> — restricted label set; both vendors with different implementations</li><li><code>SET</code> — multiple labels; MySQL</li><li><code>RANGE / MULTIRANGE</code> — ranges of values; PostgreSQL</li><li><code>INET / CIDR / MACADDR</code> — network values; PostgreSQL</li><li><code>GEOMETRY / GEOGRAPHY</code> — spatial values with vendor extensions</li></ul><h3>Auto-generated identifiers</h3><ul><li><code>AUTO_INCREMENT</code> — MySQL integer generation attribute</li><li><code>GENERATED ... AS IDENTITY</code> — standard identity syntax supported by PostgreSQL</li><li><code>SERIAL / BIGSERIAL</code> — legacy PostgreSQL sequence shorthand</li><li><code>SEQUENCE</code> — independent number generator; PostgreSQL and standard SQL</li></ul><h3>Selection rules</h3><ul><li><code>DECIMAL, not FLOAT</code> — for exact money and accounting values</li><li><code>TIMESTAMP WITH TIME ZONE</code> — for absolute events shared across regions</li><li><code>VARCHAR/TEXT</code> — for human-readable text</li><li><code>INTEGER/BIGINT</code> — for counters and numeric identifiers</li><li><code>JSON</code> — only when attributes are genuinely flexible and do not need ordinary relational constraints</li></ul></section><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div><h3>SQL Data Types: MySQL and PostgreSQL</h3><p>Use standard SQL first, then document any MySQL or PostgreSQL-specific syntax. Verify the statement with small sample data, an edge case, and the expected result before using it in production.</p><h3>Required verification</h3><ul><li>Run the simple case.</li><li>Test a NULL, duplicate, empty, or boundary case where relevant.</li><li>Confirm the affected rows or query result.</li><li>Use EXPLAIN for performance-sensitive queries.</li></ul></div></section>","detailId":"lesson-sql-data-types-8bb57f3c","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z","details":[{"id":"lesson-sql-data-types","conceptKey":"sql-data-types","subjectId":"subject-sql-data-types","title":"SQL Data Types","summary":"Data types define which values a column accepts and affect validation, storage, sorting, and calculations.","baseContent":"<h2>SQL Data Types</h2><p>Data types define which values a column accepts and affect validation, storage, sorting, and calculations.</p><h3>Example</h3><pre><code>CREATE TABLE products (\n  id INTEGER PRIMARY KEY,\n  name VARCHAR(120) NOT NULL,\n  price DECIMAL(10, 2),\n  created_at TIMESTAMP\n);</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>An online shop stores customers, products, orders, and order items. The schema must prevent duplicate identities, missing required values, and orders that reference customers that do not exist.</p><h3>Advanced example</h3><pre><code>BEGIN;\n-- Preview the exact target set first.\nSELECT id FROM orders WHERE status = 'pending';\n-- Apply the SQL Data Types operation, verify affected rows, then commit.\nCOMMIT;</code></pre><h3>Expected result</h3><p>The schema or operation satisfies the stated rule, rejects invalid data, and can be verified with a repeatable query.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>Continue with the PicoStore database</h3><p>This lesson reuses <strong>picostore</strong>. Relevant tables: <code>customers, products, orders, order_items</code>. Keep the starter rows from the Introduction lesson so results remain comparable.</p><h3>Another practical example</h3><pre><code>ALTER TABLE products\n  ADD COLUMN active BOOLEAN NOT NULL DEFAULT TRUE;\n\nSELECT product_id, name, active FROM products;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-sql-reference-catalog=\"true\"><h2>SQL reference catalog: SQL Data Types</h2><p>This catalog covers the practical public SQL surface. Check the exact server-version documentation before using a vendor-specific item.</p><h3>Exact numeric types</h3><ul><li><code>SMALLINT</code> — small whole numbers</li><li><code>INTEGER / INT</code> — ordinary whole numbers</li><li><code>BIGINT</code> — large whole numbers</li><li><code>DECIMAL(p,s) / NUMERIC(p,s)</code> — exact fixed-point values such as money</li></ul><h3>Approximate numeric types</h3><ul><li><code>REAL</code> — single-precision approximate number</li><li><code>DOUBLE PRECISION</code> — double-precision approximate number</li><li><code>FLOAT(p)</code> — precision-based approximate number</li></ul><h3>Character and text types</h3><ul><li><code>CHAR(n)</code> — fixed-length text</li><li><code>VARCHAR(n)</code> — variable-length bounded text</li><li><code>TEXT</code> — large variable text; common vendor extension</li><li><code>NCHAR / NVARCHAR</code> — national character types, mainly SQL Server/MySQL compatibility</li></ul><h3>Binary types</h3><ul><li><code>BINARY(n)</code> — fixed-length bytes</li><li><code>VARBINARY(n)</code> — variable-length bytes</li><li><code>BLOB</code> — large binary object; MySQL</li><li><code>BYTEA</code> — variable binary data; PostgreSQL</li></ul><h3>Date and time types</h3><ul><li><code>DATE</code> — calendar date</li><li><code>TIME</code> — time of day</li><li><code>TIME WITH TIME ZONE</code> — time with offset support</li><li><code>TIMESTAMP</code> — date and time</li><li><code>TIMESTAMP WITH TIME ZONE</code> — absolute instant; PostgreSQL timestamptz</li><li><code>INTERVAL</code> — duration; PostgreSQL and standard SQL</li><li><code>YEAR</code> — year value; MySQL</li></ul><h3>Logical and structured types</h3><ul><li><code>BOOLEAN / BOOL</code> — true or false</li><li><code>JSON</code> — validated JSON document</li><li><code>JSONB</code> — binary searchable JSON; PostgreSQL</li><li><code>XML</code> — XML document; PostgreSQL/SQL Server</li><li><code>UUID</code> — 128-bit identifier; native PostgreSQL</li><li><code>ARRAY</code> — typed array; PostgreSQL</li><li><code>ENUM</code> — restricted label set; both vendors with different implementations</li><li><code>SET</code> — multiple labels; MySQL</li><li><code>RANGE / MULTIRANGE</code> — ranges of values; PostgreSQL</li><li><code>INET / CIDR / MACADDR</code> — network values; PostgreSQL</li><li><code>GEOMETRY / GEOGRAPHY</code> — spatial values with vendor extensions</li></ul><h3>Auto-generated identifiers</h3><ul><li><code>AUTO_INCREMENT</code> — MySQL integer generation attribute</li><li><code>GENERATED ... AS IDENTITY</code> — standard identity syntax supported by PostgreSQL</li><li><code>SERIAL / BIGSERIAL</code> — legacy PostgreSQL sequence shorthand</li><li><code>SEQUENCE</code> — independent number generator; PostgreSQL and standard SQL</li></ul><h3>Selection rules</h3><ul><li><code>DECIMAL, not FLOAT</code> — for exact money and accounting values</li><li><code>TIMESTAMP WITH TIME ZONE</code> — for absolute events shared across regions</li><li><code>VARCHAR/TEXT</code> — for human-readable text</li><li><code>INTEGER/BIGINT</code> — for counters and numeric identifiers</li><li><code>JSON</code> — only when attributes are genuinely flexible and do not need ordinary relational constraints</li></ul></section><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div><h3>SQL Data Types: MySQL and PostgreSQL</h3><p>Use standard SQL first, then document any MySQL or PostgreSQL-specific syntax. Verify the statement with small sample data, an edge case, and the expected result before using it in production.</p><h3>Required verification</h3><ul><li>Run the simple case.</li><li>Test a NULL, duplicate, empty, or boundary case where relevant.</li><li>Confirm the affected rows or query result.</li><li>Use EXPLAIN for performance-sensitive queries.</li></ul></div></section>","detailId":"lesson-sql-data-types-8bb57f3c","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z"}]}}