Creating SQL Assistant in Slack Using OpenAI GPTs without Coding in 20 minutes

Snow W. Lee
Runbear
Published in
3 min readJun 25, 2024

Yes, right. AI can write SQL queries for you. It can understand your database schema, generate SQL queries, and even interact with you to enhance them.

Let’s create a bot that helps you write SQL queries, and invite the bot to your communication platforms, such as Slack or Microsoft Teams. It takes 20 minutes to create this without coding.

The image below shows how it works in Slack. Awesome, isn’t it?

How to Create It

Don’t worry. You don’t need engineering knowledge to create yours.

Here are the simple steps:

  1. Extract database schema.
  2. Create an Assistant using OpenAI or Anthropic.
  3. Invite the Assistant to your communication platform using PlugBear.

Let’s go through one by one.

1. Extracting database schema

This is the only step you may need help from engineers.

You can extract database schema in many ways. For example, you can extract PostgreSQL database schema using this command:

psql -c "\d *"

With proper parameters, you will get table descriptions like this:

Table "public.User"
Column | Type | Collation | Nullable | Default
------------+--------------------------------+-----------+----------+-------------------
id | uuid | | not null |
email | text | | not null |
createdAt | timestamp(6) with time zone | | not null | CURRENT_TIMESTAMP
updatedAt | timestamp(6) with time zone | | not null |
firstName | text | | |
lastName | text | | |
orgId | text | | not null |

2. Creating an Assistant

If you are a PlugBear user, simply visit the “LLM Apps” page and click the “Create” button to create your Assistant that uses OpenAI or Anthropic.

All you need to do is write simple instructions and attach the table descriptions. Here is an example:

You are the SQL Bot tasked with generating SQL queries based on user
requests. Utilize the provided table schema to retrieve the
information. Only use the specified tables and columns. Do not assume
the existence of any tables or columns not listed. If you are unable
to construct SQL for a given user query, acknowledge the limitation
rather than producing arbitrary SQL.

Table descriptions:
Table "public.User"
Column | Type | Collation | Nullable | Default
------------+--------------------------------+-----------+----------+-------------------
id | uuid | | not null |
email | text | | not null |
createdAt | timestamp(6) with time zone | | not null | CURRENT_TIMESTAMP
...

3. Inviting the Assistant to your communication platform

Using PlugBear, this can be done with several clicks.

Click a button to integrate your communication platform, such as Slack or Microsoft Teams, connect the Assistant and the channel, and you’re all set.

Try It Now

Join PlugBear now and empower your team to achieve more with less! It takes only 20 minutes to create this.

(The original article was posted on the PlugBear blog: https://plugbear.io/posts/sql-assistant)

--

--