Executing SQL queries from the browser

Executing SQL queries from the browser

Try CockroachDB free

Experience the power of a distributed cloud database.

Get started

Over the last couple weeks, you might have noticed a new tab on the cluster view of CockroachDB Cloud! We just released an open preview of our Browser SQL Shell, which allows writing queries on your database within seconds of cluster creation.

Executing queries on a cluster from the browser

Why we built it

While I’m not going to say all SQL should be executed from the browser, working with your database should have as little friction as possible. Iterating on schemas, testing syntax, and executing queries shouldn’t require generating a user and saving a password when you’re getting started. I’m sure we’ve all had stacks of notes storing way too many autogenerated strings for our database credentials.

With CockroachDB Cloud, you can create a multi-region serverless database in less than 30 seconds. Writing SQL queries should feel just as fast, and take advantage of the flexibility a web client has to make the experience better.

Internally, it’s been nice to use it during schema development and validating data in clusters. These little bits of time add up! A shell helps us refocus on the parts of building an app.

Syntax highlighting and UX with CockroachDB

The SQL shell isn’t the first experience we’ve released with Syntax highlighting and SQL execution from the browser. Last year, we released our MOLT Schema Conversion Tool (SCT), which helps you iterate on DB schemas while converting it to CockroachDB. The SCT presents each statement with syntax highlighting to help reading table structures.

Our code inputs use Codemirror 6 under the hood, with a custom theme to add a little color to the text. Codemirror 6 hit a sweet spot with performance, bundle size, plugin support, with a lot of active support some great developers. Replit did a fairly large migration over the last 2 years and did an awesome deep dive into the benefits it brings. With this, it’ll be a lot easier to add things like VIM keybindings and helpful cursor interactions down the line!

Codemirror also has great autocomplete support. When you open the shell, we make a query to pull your table schemas and column names to make writing queries a bit quicker. Just navigate the options and click enter to speed up the typing.

Opening the shell will pull table and column names for autocomplete

Any other neat stuff?

The Browser SQL shell has some handy features to help database operations that build on the normal terminal experience. After trying a few variations we felt pretty inspired by Jupyter notebooks and terminals like Warp, which provide persistent queries that can be run in a long running view.

Table selects provide type information about columns, and offer local sorting

While working across your DB, you can copy the query to share, navigate the history, export to CSVs, and view column types. Query results are rendered in tables to improve readability, and enables local sorting for quickly scanning through your data. Error formats will also reflect what you’d see with our terminal experience to reduce context switching between the two. Feel free to also use ?? syntax for hints when writing your queries.

?? syntax can provide hints across different forms of queries

Terminal and the SQL Shell experience side by side

Terminal and the SQL Shell experience side by side

We’ll be iterating on the shell a fair bit over the next few months. If you have anything you’d like to see, feel free to share it with the feedback button on the page!

FAQ

  • What access level do I need?

    • With our new roles system, Cluster Administrators have the ability to write queries from the browser. This is required since our console uses a provisioned SQL user under the hood, so only users with SQL access are permitted.
  • Why don’t I see the SQL Shell in the console?

    • The SQL Shell is available for Cluster Administrators on Serverless and Dedicated standard clusters. Please check your permissions and cluster type within your organization.
  • Where do I find it?

    • Feel free to create any cluster in CockroachDB Cloud, and you’ll see it under the “Overview” page.
  • Is the SQL Shell general availability?

    • We’ve opted to label it as Preview as we iterate on features and get feedback from customers, but it’s available for all users with the correct permissions now!

Keep Reading

How to identify and tune a problematic query with SQL EXPLAIN

As a developer, you are often faced with the question: why is my application slow? You may have already identified the …

Read more
How online schema changes are possible in CockroachDB

I periodically need to make changes to tables. Adding columns is very simple with the ALTER TABLE command… But my tables …

Read more
sqlfmt: A free online SQL formatter for writing prettier SQL

This post was originally published in 2018 by former CockroachDB engineer Matt Jibson, who owns goats and makes his own …

Read more