How to create complex visualizations in Metabase using the SQL editor

Written by Impact Atlas
Updated 9 months ago

If you ever need to build visualizations that can't be expressed using the Query Builder, you can use the SQL Editor instead.

SQL stands for Structured Query Language, and is a widely used standard for getting data from databases.

How to start a new SQL query?

Click + New in the main navigation bar. A drop-down menu will then appear where you need to select the SQL Query option to start a new SQL query.

After clicking SQL Query, an editor will appear where you can write and run queries in SQL. First you need to select the database you want to work with and before you start writing the first line of code, you need to familiarize yourself with the data from the selected database.

On the right side of the editor, you will see the book icon, where you can browse through the tables in your database and view their columns and connections.

In our example cohort, there are eight tables. A table's columns will all be visible once you click on it. The names of the table and columns are crucial since they will be used in the query we are writing, and we must ensure that the names are spelled correctly to avoid errors.

Developing your query

Below is a list of different types of SELECT queries, from the simplest to the most complex, that you can write using the SQL Editor. Click on it to check the details along with examples and find out in what context it is used:

  1. SELECT Statement  
  2. SELECT Statement with JOIN
  3. SELECT Statement with WHERE clause
  4. SELECT Statement with GROUP BY clause
  5. SELECT Statement with HAVING clause
  6. SELECT Statement with UNION operator
  7. SELECT Statement with ORDER BY clause

How to come back to an existing query that has been built?

Let's say we want to go back to the query we created a few days ago to update it. We have to position ourselves on the home page, and from here we have two options: either we use the search bar to search for the name of the query we created or we search the list under the title "Our Analytics".

Once we find it, we can open the SQL Editor using the "Open Editor" button at the top right of the screen.

Did this answer your question?