SQL. The last domain of caps lock.
Whether you’re an S-Q-L or sequel person, you know it’s one of the most potent languages at your disposal. SQL, or Structured Query Language, allows developers to query, manipulate, and manage relational databases. It enables them to extract valuable insights from vast amounts of structured data and build robust data-driven applications.
SQL is a domain-specific language, designed specifically for interacting with databases. While incredibly powerful, SQL can be intimidating for non-technical or external users who need to access and analyze data.
With this post, you'll learn how to use Retool create an intuitive graphical user interface (GUI) that allows users to interact with databases using SQL without writing custom apps.
First, we need to get the data from our database into Retool before we can query it. Head to Resource in your Retool dashboard:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F0b984dfe8312eefb10c4fadb417646fc74c7c173-642x372.png&w=3840&q=75)
There are a ton of different SQL databases, and Retool supports most of them. The primary two flavors are Postgres and MySQL. We’re going to be using a Postgres database:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F8fc29e62b11eebb979be8da6be22cc07ca7d90f8-1780x752.png&w=3840&q=75)
We could use a Retool database, but because it's already so well-integrated into Retool, we’d be bypassing part of the setup process. Retool databases work out-of-the-box. If you want to use a Retool database, you can follow the Retool Database Quickstart to set it up, and then skip ahead to the SQL part—those steps will be the same.
Getting back to our process here—you must add the Postgres connection information for your database. You can do this manually, or add the connection string under “Autofill using” and Retool will infer each field.
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F6b7ad5e0d001cfc4460664f315e90d8d2745ad74-908x1028.png&w=3840&q=75)
You can see from the schema that the connection worked when you connected. Here, we’re connecting a database with customer information:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F3d7b21122b1de35c15b60fa0a252303a7cc2b6c6-990x772.png&w=3840&q=75)
We’re now ready to start using an SQL database with Retool.
Before we start putting together some SQL queries, we want to think about how our UI will look. Let’s head back to “Apps” and then create a new web app:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F5fdeae608ddfd4de18814508360c9d4035e13595-638x366.png&w=3840&q=75)
On the new page, click the “Add UI” button to show the components you can add to your application. Here, we'll build a GUI that allows us to see a list of customers, search that list, see more information about a selected customer, and see some aggregate data from our customers. To do that, we’ll need:
- A container to frame our dashboard
- A text input that we’ll use for search
- A table to show all our customers
- A component to show information from a single customer
- A graph to chart some data
With all that in place, but before we’ve loaded our own data, it looks like this:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F0d7905eb6167b6695319a70f9bcd82ba433f61ee-1446x1200.png&w=3840&q=75)
Retool prepopulates some mock data for us to understand the layout. With the GUI ready, we can move on to the SQL queries.
In the GUI we just created, you’ll notice the table and chart have options to “Connect data.” Let’s do that for the table. Click “Connect data” to see a new frame for adding our resource. Search the “Resource” field for the database name we previously added. Here, it is called “customers.” Here, you can create a unique name for your query since we’ll create a few. We’ll name this one get_all_customers.
We can now write our SQL. The first query we’ll write will be simple: it will get a list of all customers in our Postgres database.
1SELECT * FROM customers
This means, “Give me every row and column from the customers table.”
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F515d8695a0ce0fc5c925ab908e2b36fc15d0b2ae-990x1170.png&w=3840&q=75)
If we select “save and run,” we can see the data populated in our table component:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2Fe6b4592afa6b61be0954095a5cc57031ab866bbf-920x658.png&w=3840&q=75)
Formatting and design are automatically added to each row in the table (no design expertise necessary).
You can build any complex SQL GUI with those basic steps. The queries and components might be more complicated, but the process will be the same: create a component, connect data, write a query, and the data will appear. Now, we’re going to add a few other helpful components to the SQL GUI.
Adding charts to a GUI is a great way to quickly show numerical data that might be difficult to understand in tabs and rows. This example separates revenue by customer support rating.
We will reuse the get_all_customers
query and just use some of the data from the dataset. Click on the chart and look in the inspector:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F59b3eb65e401a44cd399da73a8442312af7872ee-1999x799.png&w=3840&q=75)
As you can see, the data source is the get_all_customers
query. We’ll then set this up as a bar chart, with the customer support rating from the query as the X-axis. Retool will automatically graph a few of the return datasets. Here, we’ll just want one: the sum of the monthly revenue.
Next, we’ll create an individual customer component. We just added the component frame above, so we’ll add a few fields for the customer name, support rating, subscription plan, and monthly revenue.
We’re going to need a new query for this, which we’ll call get_single_customer
:
1SELECT * FROM customers WHERE customer_id = {{ table1.selectedRow.customer_id }}
Whenever you click on a row in the table, Retool returns the data for that entry in a selectedRow
object. So, as you click through rows, you can get the customer ID for the selected customer. We’re feeding that number into a SQL query using the double curly brackets {{ }}. This means that if we selected the first row, this query would read:
1SELECT * FROM customers WHERE customer_id = 1
2
So, the database would return only the customer information for the first customer. We can then use that data in our individual customer component. If we want the customer’s name, for instance, we’d use:
1{{get_single_customer.rawData.company_name[0]}}
We can put this in the “Value” field of a text field to show the customer name:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F1a870bd663d14b4bc2cfb390417ee05c27bb2d85-558x848.png&w=3840&q=75)
Doing the same for the other fields, we get a nice little breakdown of each customer’s details when we select them in the table:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2Ff214a9659ea51f55687c7cfafbe09d64de8b348b-1999x1127.png&w=3840&q=75)
If you noticed that we have to scroll through and manually find any entry, don’t worry—we can build a search tool in the GUI to easily find a specific customer. To do that, we’re going to change the query attached to our table for a new query that we’ll call get_customer_by_name
:
1SELECT * FROM customers WHERE company_name LIKE {{ searchbar.value }}
We’re taking the value from our search text input and adding it to our SQL query, again using curly bracket nomenclature. This will search our database for any entry with a customer name like what we type into the search field:
![](/blog/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbclf52sw%2Fproduction%2F40debb12072f4711d7847e3d64a440735c846caf-1999x1183.png&w=3840&q=75)
We’ve now built a SQL GUI using Retool and created three queries. You could stop here, or you could keep going and build more complex components. In Retool, you can:
- Implement filtering and sorting options for the customer table, allowing users to filter by specific criteria (e.g., industry, subscription plan) and sort the results based on columns (e.g., monthly revenue, customer since date)
- Include a form to add new customers or update existing customer information directly from the GUI. This would involve creating INSERT and UPDATE SQL statements based on user input
- Handle large datasets efficiently by adding pagination to the customer table. This can be done using SQL's LIMIT and OFFSET clauses in combination with Retool's pagination component
- Filter table components using AI to converting user questions into correct SQL queries for a given dataset.
Creating a SQL GUI in Retool is based on a simple process—create a component, connect it to data, and write queries to manipulate the exact data you need.
Want to build your own custom SQL GUI today? Sign up for a free Retool account and give it a whirl.
Reader