Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Databricks INSERT INTO Table VALUES: How-To Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can insert literal rows into a Databricks table with INSERT INTO table_name VALUES (...), (...); — the same SQL you would use in most databases. The statement works on Delta Lake tables and, in Databricks SQL, on tables you have permission to modify. You need a running SQL warehouse or cluster, a target table that already exists, and values that match the table's column order and data types. Below is the exact sequence, plus the mistakes that cause most failures.

What You'll Need

  • A Databricks workspace with access to a SQL warehouse (for the SQL editor) or an all-purpose cluster (for a notebook).
  • A target table — either managed or external — that already exists. INSERT INTO does not create tables.
  • USE CATALOG and USE SCHEMA (or fully qualified names like main.sales.orders) so the table resolves.
  • Permission to modify the table (for example, MODIFY or ownership) and USE on its schema.
  • Values written in the same order as the table's columns, with correct types and quoting.

Step-by-Step

  1. Open a SQL editor or notebook. In the workspace, open the SQL editor attached to a SQL warehouse, or open a notebook attached to a cluster. Both accept the same SQL syntax.
  2. Select the catalog and schema. Run USE CATALOG main; then USE SCHEMA sales; (replace with your names). This lets you refer to the table by its short name. You can skip this if you always write the full three-part name.
  3. Confirm the table exists and check its columns. Run DESCRIBE TABLE orders; or SHOW COLUMNS IN orders;. Note the column order and each data type — this is what the values must match.
  4. Write the INSERT statement. The basic form is:
    INSERT INTO orders VALUES
      (1001, '2024-05-01', 'widget', 3, 19.99),
      (1002, '2024-05-01', 'gadget', 1, 49.50);
    List one parenthesised row per record, separated by commas, and end with a semicolon.
  5. Match the column order exactly. If the table has five columns, every row must supply five values in the same order. If you want to supply only some columns, name them explicitly: INSERT INTO orders (order_id, order_date, product) VALUES (1003, '2024-05-02', 'bolt'); Columns you omit must allow nulls or have defaults.
  6. Quote strings, not numbers. Use single quotes for strings and dates ('2024-05-01'), and no quotes for numeric values. Escape an apostrophe inside a string by doubling it: 'O''Brien'.
  7. Run the statement. In the SQL editor, click Run (or press Ctrl+Enter / Cmd+Enter). In a notebook, run the cell. A successful insert returns a row count or a success message.
  8. Verify the rows. Run SELECT * FROM orders WHERE order_id IN (1001, 1002, 1003); to confirm the values landed as expected.
  9. Check the table history if needed. For Delta tables, DESCRIBE HISTORY orders; shows the insert as a new version, which is useful for auditing or rolling back.

Common Mistakes

  • Wrong number of values. Supplying four values for a five-column table fails with a column-count error. Count the columns and the values.
  • Wrong order. Values are positional. Putting a date where an integer belongs causes a type mismatch or silently wrong data if types are compatible.
  • Missing quotes around strings or dates. Unquoted text is read as a column name or identifier and fails.
  • Using double quotes for string literals. In Databricks SQL, double quotes usually identify a column; use single quotes for text values.
  • Forgetting the schema. Running INSERT INTO orders without a current schema gives a table-not-found error. Qualify the name or set the schema first.
  • Assuming INSERT creates the table. It does not. Create the table first with CREATE TABLE.
  • Inserting into a view. Views are not directly insertable unless they are updatable in a supported way; insert into the underlying table instead.

Tips

  • Use the explicit column list form (INSERT INTO t (col1, col2) VALUES ...) in scripts. It survives table schema changes that add columns at the end.
  • For many rows, prefer INSERT INTO ... SELECT from a staging table or a VALUES clause used as a subquery, which is easier to generate programmatically than thousands of literal rows.
  • Insert in batches rather than one row per statement; each statement is a separate transaction and adds overhead.
  • Test with a SELECT of the same literal values first to catch type errors before writing.
  • If the table has constraints (for example, NOT NULL), check them before running the insert to avoid partial failures.

Troubleshooting

“Table or view not found.” Check the catalog, schema, and table name with SHOW TABLES IN schema_name;. Set the current catalog and schema, or use the fully qualified three-part name.

“Cannot resolve column” or a type error. Run DESCRIBE TABLE and compare column order and types against your values. Cast explicitly if needed, for example CAST('2024-05-01' AS DATE).

Permission denied. You need modify rights on the table and use rights on its schema. Ask the workspace admin or table owner to grant them.

The statement runs but no rows appear. Confirm you are querying the same table and schema you inserted into, and that you committed the transaction (autocommit is normally on in the SQL editor).

Partial insert. A single INSERT statement is atomic for Delta tables, so either all rows are written or none. If you see fewer rows than expected, check for duplicate keys or a filter in your verification query.

If that did not work, simplify: insert a single row with all columns, verify it, then expand. If the single row fails, the problem is the table definition or permissions, not the multi-row syntax.

Frequently Asked Questions

Can I insert without listing every column?

Yes, if you name the columns you are supplying: INSERT INTO t (a, b) VALUES (1, 2); Omitted columns must accept nulls or have defaults.

Does INSERT INTO overwrite existing rows?

No. It appends new rows. To replace data, use INSERT OVERWRITE or a MERGE statement, depending on your goal.

Can I insert multiple rows in one statement?

Yes. Separate each parenthesised row with a comma. This is more efficient than one statement per row.

Why does my date value fail?

Dates must be quoted strings in a recognised format, usually 'YYYY-MM-DD', or explicitly cast. An unquoted date is treated as an expression.

Can I insert into a table in another catalog?

Yes, use the three-part name catalog.schema.table, provided you have permission on that catalog and schema.

Is INSERT INTO supported on all Databricks tables?

It works on Delta Lake tables and most table types you can modify. Views and some read-only external sources are not directly insertable; insert into the underlying table instead.