Learn CockroachDB SQL

This page walks you through some of the most essential CockroachDB SQL statements. For a complete list and related details, see SQL Statements.

Tip:

Use an interactive SQL shell to try out these statements. If you have a cluster already running, use the cockroach sql command. Otherwise, use the cockroach demo command to open a shell to a temporary, in-memory cluster.

Note:

CockroachDB aims to provide standard SQL with extensions, but some standard SQL functionality is not yet available. See our SQL Feature Support page for more details.

Create a table

To create a table, use CREATE TABLE followed by a table name, the column names, and the data type and constraint, if any, for each column:

  1. > CREATE TABLE accounts (
  2. id INT PRIMARY KEY,
  3. balance DECIMAL
  4. );

Table and column names must follow these rules. Also, when you do not explicitly define a primary key, CockroachDB will automatically add a hidden rowid column as the primary key.

To avoid an error in case the table already exists, you can include IF NOT EXISTS:

  1. > CREATE TABLE IF NOT EXISTS accounts (
  2. id INT PRIMARY KEY,
  3. balance DECIMAL
  4. );

To show all of the columns from a table, use SHOW COLUMNS FROM followed by the table name:

  1. > SHOW COLUMNS FROM accounts;
  1. column_name | data_type | is_nullable | column_default | generation_expression | indices | is_hidden
  2. +-------------+-----------+-------------+----------------+-----------------------+-------------+-----------+
  3. id | INT | false | NULL | | {"primary"} | false
  4. balance | DECIMAL | true | NULL | | {} | false
  5. (2 rows)

When you no longer need a table, use DROP TABLE followed by the table name to remove the table and all its data:

  1. > DROP TABLE accounts;

Show tables

To see all tables in the active database, use the SHOW TABLES statement:

  1. > SHOW TABLES;
  1. table_name
  2. +------------+
  3. accounts
  4. (1 row)

Insert rows into a table

To insert a row into a table, use INSERT INTO followed by the table name and then the column values listed in the order in which the columns appear in the table:

  1. > INSERT INTO accounts VALUES (1, 10000.50);

If you want to pass column values in a different order, list the column names explicitly and provide the column values in the corresponding order:

  1. > INSERT INTO accounts (balance, id) VALUES
  2. (25000.00, 2);

To insert multiple rows into a table, use a comma-separated list of parentheses, each containing column values for one row:

  1. > INSERT INTO accounts VALUES
  2. (3, 8100.73),
  3. (4, 9400.10);

Defaults values are used when you leave specific columns out of your statement, or when you explicitly request default values. For example, both of the following statements would create a row with balance filled with its default value, in this case NULL:

  1. > INSERT INTO accounts (id) VALUES
  2. (5);
  1. > INSERT INTO accounts (id, balance) VALUES
  2. (6, DEFAULT);
  1. > SELECT * FROM accounts WHERE id in (5, 6);
  1. id | balance
  2. +----+---------+
  3. 5 | NULL
  4. 6 | NULL
  5. (2 rows)

Create an index

Indexes help locate data without having to look through every row of a table. They're automatically created for the primary key of a table and any columns with a UNIQUE constraint.

To create an index for non-unique columns, use CREATE INDEX followed by an optional index name and an ON clause identifying the table and column(s) to index. For each column, you can choose whether to sort ascending (ASC) or descending (DESC).

  1. > CREATE INDEX balance_idx ON accounts (balance DESC);

You can create indexes during table creation as well; just include the INDEX keyword followed by an optional index name and the column(s) to index:

  1. > CREATE TABLE accounts (
  2. id INT PRIMARY KEY,
  3. balance DECIMAL,
  4. INDEX balance_idx (balance)
  5. );

Show indexes on a table

To show the indexes on a table, use SHOW INDEX FROM followed by the name of the table:

  1. > SHOW INDEX FROM accounts;
  1. table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit
  2. +------------+-------------+------------+--------------+-------------+-----------+---------+----------+
  3. accounts | primary | false | 1 | id | ASC | false | false
  4. accounts | balance_idx | true | 1 | balance | DESC | false | false
  5. accounts | balance_idx | true | 2 | id | ASC | false | true
  6. (3 rows)

Query a table

To query a table, use SELECT followed by a comma-separated list of the columns to be returned and the table from which to retrieve the data:

  1. > SELECT balance FROM accounts;
  1. balance
  2. +----------+
  3. 10000.50
  4. 25000.00
  5. 8100.73
  6. 9400.10
  7. NULL
  8. NULL
  9. (6 rows)

To retrieve all columns, use the * wildcard:

  1. > SELECT * FROM accounts;
  1. id | balance
  2. +----+----------+
  3. 1 | 10000.50
  4. 2 | 25000.00
  5. 3 | 8100.73
  6. 4 | 9400.10
  7. 5 | NULL
  8. 6 | NULL
  9. (6 rows)

To filter the results, add a WHERE clause identifying the columns and values to filter on:

  1. > SELECT id, balance FROM accounts WHERE balance > 9000;
  1. id | balance
  2. +----+----------+
  3. 2 | 25000.00
  4. 1 | 10000.50
  5. 4 | 9400.10
  6. (3 rows)

To sort the results, add an ORDER BY clause identifying the columns to sort by. For each column, you can choose whether to sort ascending (ASC) or descending (DESC).

  1. > SELECT id, balance FROM accounts ORDER BY balance DESC;
  1. id | balance
  2. +----+----------+
  3. 2 | 25000.00
  4. 1 | 10000.50
  5. 4 | 9400.10
  6. 3 | 8100.73
  7. 5 | NULL
  8. 6 | NULL
  9. (6 rows)

Update rows in a table

To update rows in a table, use UPDATE followed by the table name, a SET clause identifying the columns to update and their new values, and a WHERE clause identifying the rows to update:

  1. > UPDATE accounts SET balance = balance - 5.50 WHERE balance < 10000;
  1. > SELECT * FROM accounts;
  1. id | balance
  2. +----+----------+
  3. 1 | 10000.50
  4. 2 | 25000.00
  5. 3 | 8095.23
  6. 4 | 9394.60
  7. 5 | NULL
  8. 6 | NULL
  9. (6 rows)

If a table has a primary key, you can use that in the WHERE clause to reliably update specific rows; otherwise, each row matching the WHERE clause is updated. When there's no WHERE clause, all rows in the table are updated.

Delete rows in a table

To delete rows from a table, use DELETE FROM followed by the table name and a WHERE clause identifying the rows to delete:

  1. > DELETE FROM accounts WHERE id in (5, 6);
  1. > SELECT * FROM accounts;
  1. id | balance
  2. +----+----------+
  3. 1 | 10000.50
  4. 2 | 25000.00
  5. 3 | 8095.23
  6. 4 | 9394.60
  7. (4 rows)

Just as with the UPDATE statement, if a table has a primary key, you can use that in the WHERE clause to reliably delete specific rows; otherwise, each row matching the WHERE clause is deleted. When there's no WHERE clause, all rows in the table are deleted.

What's next?

Was this page helpful?
YesNo