SQL: Structured Query LanguageSQL FundamentalsTable Structure and Data

What is a table, and how are rows and columns organized?

Question 5 of 30 · Updated

On this page

A table is a named database object that organizes data into rows and named columns. Each row represents one occurrence of the kind of information the table stores, and each column describes a particular attribute of that occurrence. A column definition specifies its name, data type and, when appropriate, default value or constraints. All rows in a table follow the same logical column structure.

For example, an inventory table may contain one row per product, with columns for product ID, product name, stock quantity and shelf location. The table defines the structure; the rows contain the actual inventory data.

1. Understanding the anatomy of a table#

Consider a table named products:

product_idproduct_namestock_qtyshelf_code
1Pen40A1
2Notebook12A2
3Marker0NULL

This table has four columns and three rows. The heading describes the column names and is not a fourth data row.

  • Table name: products identifies the table within its database namespace.
  • Column: stock_qty represents the quantity in stock for each product. Every row has a value or NULL for each logical column.
  • Row: (2, 'Notebook', 12, 'A2') represents a single product. A row is also commonly called a record.
  • Individual value: 12 is the stock quantity for product 2.
  • Primary key: product_id is the chosen unique identifier for each product. Product names need not be unique unless the business explicitly requires that rule.

The table's degree is its number of attributes (four columns here), and its current cardinality is its number of rows (three here). Cardinality can also have other meanings in database discussions, such as the number of distinct values in a column or the multiplicity of a relationship; context matters.

2. Structure versus data#

A table definition describes what each column means and which values are permitted. The stored data is the collection of rows that currently follows that definition. You can create a table with no rows at all: its name, columns, types and constraints still exist. Adding another product increases the row count, not the column count.

Before choosing columns, decide what a single row represents. This is often called the grain of a table. In this example, each row represents one product in a single inventory location. If the same product can have different stock quantities in multiple warehouses, a design with a separate inventory table at the product-and-warehouse grain is usually more appropriate than adding one column for each warehouse.

3. Creating a table with typed columns#

Run the examples in a fresh disposable database, not in a production inventory database.

SQL
CREATE TABLE products ( product_id INTEGER PRIMARY KEY, product_name VARCHAR(100) NOT NULL, stock_qty INTEGER NOT NULL DEFAULT 0 CHECK (stock_qty >= 0), shelf_code VARCHAR(20) );
ColumnDefinitionPurpose
product_idINTEGER PRIMARY KEYIdentifies each product uniquely
product_nameVARCHAR(100) NOT NULLStores a required product name
stock_qtyINTEGER NOT NULL DEFAULT 0 CHECK (stock_qty >= 0)Stores a nonnegative, required quantity with a default of zero
shelf_codeVARCHAR(20)Stores an optional shelf identifier

The data type specifies the kind of value the column is designed to hold. In PostgreSQL, INTEGER represents a whole number within its supported range and VARCHAR(100) limits the text to at most 100 characters. A data type is not the same thing as a business rule: an INTEGER alone does not prevent negative stock. The CHECK constraint enforces that additional requirement. NOT NULL prevents a missing value, and DEFAULT supplies a value when the column is omitted during an insert (or explicitly assigned DEFAULT where supported).

SQLite uses type affinity in ordinary tables and does not enforce VARCHAR length as PostgreSQL does. The following portable examples exercise row structure, defaults and constraints, not identical type enforcement across engines.

4. Inserting rows and understanding omitted values#

SQL
INSERT INTO products (product_id, product_name, stock_qty, shelf_code) VALUES (2, 'Notebook', 12, 'A2'), (1, 'Pen', 40, 'A1'); INSERT INTO products (product_id, product_name) VALUES (3, 'Marker');

The first statement supplies values for all four columns. The second supplies only two. The database fills stock_qty with its default of zero and shelf_code with NULL because it is nullable and has no explicit default. NULL means missing or unknown information in this example; it is not the string 'NULL', an empty string or the number zero.

An explicit NULL for stock_qty would not invoke its default; it would violate NOT NULL. An insert that omits a required column without a default, such as product_name, also fails. Always name the target columns in an INSERT so that the mapping between supplied values and columns is explicit.

5. Rows and columns in query results#

Retrieve the complete inventory with deterministic ordering:

SQL
SELECT product_id, product_name, stock_qty, shelf_code FROM products ORDER BY product_id;
product_idproduct_namestock_qtyshelf_code
1Pen40A1
2Notebook12A2
3Marker0NULL

Notice that the rows were inserted in the order 2, 1, 3, but the result is sorted by product ID. A table does not guarantee an inherent row order. Use ORDER BY to specify the order in which results should be returned; if the chosen sort values tie, add a unique tie-breaker when deterministic ordering is important.

Now retrieve only the product names and quantities for products in stock:

SQL
SELECT product_name, stock_qty FROM products WHERE stock_qty > 0 ORDER BY product_id;
product_namestock_qty
Pen40
Notebook12

SELECT specifies the output columns (a projection in relational terminology), while WHERE filters the rows. SQL's SELECT does not automatically remove duplicate output rows. In the result above, the query returns two columns and two rows, even though the underlying table contains four columns and three rows. The table itself has not been changed. Column order in the result follows the SELECT list, independently of the table's declared column order.

To identify products whose shelf has not been assigned:

SQL
SELECT product_id FROM products WHERE shelf_code IS NULL ORDER BY product_id;

| product_id | |---:| | 3 |

Use IS NULL rather than = NULL because SQL comparisons involving NULL normally evaluate to unknown. Note the difference between an unassigned shelf and a zero stock quantity: the latter is a known numeric value.

6. How constraints protect the table#

Run each invalid statement separately in a disposable database:

SQL
-- Duplicate product identity INSERT INTO products (product_id, product_name) VALUES (1, 'Duplicate'); -- Required name omitted INSERT INTO products (product_id, stock_qty) VALUES (4, 5); -- Negative stock violates the business rule INSERT INTO products (product_id, product_name, stock_qty) VALUES (4, 'Invalid', -1);

The first violates the primary key, the second violates NOT NULL, and the third violates CHECK. A table's constraints make these rules part of its definition instead of relying only on form validation or individual application developers. The database must still be configured correctly, and application-specific business rules may require additional logic. In PostgreSQL, a failed statement inside an explicit transaction generally requires rollback or recovery through a savepoint before further statements can proceed.

Confirm that no negative quantity was stored:

SQL
SELECT COUNT(*) FROM products WHERE stock_qty < 0;

The result is 0. This is a count of matching rows, not the total stock quantity.

Testing evidence: The setup, four query-result checks and all three constraint-rejection tests were executed successfully using SQLite 3.46.1 in an isolated in-memory database. The examples use common SQL syntax intended to be compatible with PostgreSQL, but PostgreSQL execution was not available. No educational SQL was executed on production application tables.

7. Logical organization is not physical storage#

The rows-and-columns layout describes how users reason about the data, not necessarily how the engine stores it on disk. A row-oriented engine typically stores row data together in storage pages; a column-oriented engine may organize values by column to support analytical workloads. Both can expose a table-like logical interface. Indexes can provide additional access paths without changing the table's logical rows and columns.

Do not assume that selecting fewer columns guarantees proportionally fewer disk reads: the actual cost depends on storage format, indexes, execution plan and engine. Selecting only the required columns is still useful for a clear query contract and may reduce data transfer.

8. Table design mistakes to avoid#

  • Using a row's displayed position as its identity instead of a stable key.
  • Mixing different meanings of a row, such as storing both individual products and warehouse-wide totals in the same inventory table.
  • Creating repeated numbered columns such as warehouse_1_stock and warehouse_2_stock for a growing set of warehouses instead of modeling separate inventory rows.
  • Storing numeric quantities as text without a compelling reason, making numeric validation and comparisons harder.
  • Using NULL and zero interchangeably when they represent different business meanings.
  • Assuming SELECT results must have exactly the same columns and number of rows as the stored table.

9. Interview-ready answer#

A table is a named database object that represents data in rows and columns. Each row is one record at a defined level of detail, and each column represents a named attribute with a data type and optional rules such as NOT NULL, DEFAULT or CHECK. For example, a products table may contain one row per product with columns for product ID, name and stock quantity. A primary key can uniquely identify the rows. SQL uses SELECT to choose columns and WHERE to filter rows. A table has a defined structure even when empty, and rows have no guaranteed retrieval order unless the query uses ORDER BY.

Related questions

Explore other topics:Forward Deployed EngineeringPythonAI System DesignMachine Learning