GPT - SQL Builder

Section header image mobile

Embark on a journey of simplified data analysis with our knowledge article on using Coefficient's SQL Builder to construct SQL queries directly within Google Sheets. Explore how this integration streamlines the query creation process, making it accessible for users of all SQL expertise levels. Whether you're a data novice or a seasoned analyst, discover the efficiency of leveraging GPT to generate SQL queries and unlock insights from your Google Sheets data effortlessly. Join us in exploring the powerful combination of artificial intelligence and spreadsheet functionality.

Using SQL Builder

Edit Existing Query

Prompt History

Examples of SQL Builder Prompts and Outputs

Select Queries

Join Queries

Group By Queries

Order By Queries

Date Function Queries

FAQs for SQL Builder

ℹ️ NOTE: SQL Builder has schema awareness, eliminating the necessity for you to explicitly specify the data schema while describing your SQL query!

ℹ️ NOTE: The following databases are supported by Coefficient's SQL Builder:

  • Snowflake
  • PostgreSQL
  • MySQL
  • RedShift
  • MS SQL

Using SQL Builder

1. Open the Coefficient Sidebar and click GPT Copilot.

Screenshot 2023-12-13 at 11.16.28 AM.png

2. Select "Chart Builder" from the list of options. 

Screenshot 2023-12-13 at 1.16.12 PM.png

3. Select a supported database source for your import using SQL Builder. 

Screenshot 2023-12-13 at 1.19.32 PM.png

4. Select "GPT SQL Builder" from the import options. 

Screenshot 2023-12-13 at 1.21.53 PM.png

5. The Import Preview window opens allowing you to enter your description/prompt, select your table schema, then click "Generate SQL".Screenshot 2023-12-15 at 3.14.43 PM.png

6. SQL Builder will update showing you the query at the top, a sample of the data output results below the query.  Click "Import" and make sure you give this import a UNIQUE name.Screenshot 2023-12-15 at 3.19.53 PM.png

7. Congratulations on your first successful import using SQL Builder with Coefficient! 🎉Screenshot 2023-12-13 at 1.53.34 PM.png

Edit Existing Query

Modifying an existing SQL query through SQL Builder is a straightforward process.

1. Navigate to your import from the home screen and select the "Edit SQL" button.

Screenshot 2023-12-15 at 3.52.02 PM.png Screenshot 2023-12-15 at 3.52.30 PM.png

2. You can click into the raw query and make the necessary edits or use AI to make the edits for you. Click inside the AI SQL Builder prompt box and add in a description of what you would like to modify on your existing query, then click "Generate SQL".Edit Prompt for existing SB query.png

3. The query will update and the new results will be shown (a sample) underneath the query window.  Click done with your edits are complete. Edit Results on SQL .png

4. The preview window will close. You will need to "Save & Run" for the edits to be applied to the import and the new results incorporated into your import. 

Screenshot 2023-12-18 at 11.17.14 AM.png Screenshot 2023-12-18 at 11.18.43 AM.png

Prompt History

There may be times when you would like to see the previous prompts that you tried using SQL Builder with Coefficient. You can access your Prompt History within the import preview window, by clicking the prompt/description box and any prompts that you created during that current session will be listed there. 

ℹ️ NOTE: Prompt History is retained throughout each session, allowing you to view previous prompts during the import preview. However, closing the import preview window will reset the history, providing a fresh start with a new session.History in prompts for SB.png

Examples of SQL Builder Prompts and Outputs

Select Queries

Input ("Describe the query") Output (Raw Query)
"Show me a list of all movies released before the year 2000." 
SELECT movie_title,release_date
FROM Movies
WHERE YEAR(release_date)<2000;
"What are the top 10 highest-rated movies in the dataset?"
SELECT movie_title,rating
FROM Movies
ORDER BY rating DESC
LIMIT 10;
"Count the number of movies released in the 1990's."
SELECT COUNT(*)AS movie_count
FROM Movies
WHERE YEAR(release_date)BETWEEN 1990 AND 1999;

Join Queries

Input ("Describe the query") Output (Raw Query)
"Retrieve the movie title and the name of the actor who played in each movie."
SELECT m.movie_title,a.actor_name
FROM Movies m
JOIN Movie_Cast mc ON m.movie_id=mc.movie_id
JOIN Actors a ON mc.actor_id=a.actor_id
"Which actors have appeared in the most movies?"
SELECT a.actor_name, COUNT(*) AS movie_count
FROM Actors a
JOIN Movie_Cast mc ON a.actor_id=mc.actor_id
GROUP BY a.actor_name
ORDER BY movie_count DESC;
"Which actresses have worked in the highest-rated movies?"
SELECT a.actress_name, AVG(m.rating)AS average_rating
FROM Actresses a
JOIN Movie_Cast mc ON a.actress_id=mc.actress_id
JOIN Movies m ON mc.movie_id=m.movie_id
GROUP BY a.actress_name
HAVING AVG(m.rating)=(
SELECT MAX(average_rating)
FROM(
SELECT AVG(m.rating)AS average_rating
FROM Actresses a
JOIN Movie_Cast mc ON a.actress_id=mc.actress_id
JOIN Movies m ON mc.movie_id=m.movie_id
GROUP BY a.actress_name
)AS temp
);

Group By Queries

Input ("Describe the query") Output (Raw Query)
"Count the number of movies for each genre."
SELECT g.genre_name, COUNT(*) AS movie_count
FROM Genres g
JOIN Movie_Genres mg ON g.genre_id=mg.genre_id
GROUP BY g.genre_name;
"Calculate the average rating for each genre."
SELECT g.genre_name, AVG(m.rating) AS average_rating
FROM Genres g
JOIN Movie_Genres mg ON g.genre_id=mg.genre_id
JOIN Movies m ON mg.movie_id=m.movie_id
GROUP BY g.genre_name;
"Find the total number of movies released in each year."
SELECT YEAR(release_date) AS release_year, COUNT(*) AS movie_count
FROM Movies
GROUP BY YEAR(release_date)
ORDER BY release_year;

Order By Queries

Input ("Describe the query") Output (Raw Query)
"Sort movies by their rating in descending order."
SELECT movie_title,rating
FROM Movies
ORDER BY rating DESC;
"Sort actors by their names in alphabetical order."
SELECT actor_name
FROM Actors
ORDER BY actor_name ASC;
"Sort movies by their release year in ascending order."
SELECT movie_title, release_date
FROM Movies
ORDER BY release_date ASC;

Date Function Queries

Input ("Describe the query") Output (Raw Query)
"What is the time difference between the release years of Jon Favreau's latest movie releases? 
SELECT DATEDDIFF(YEAR,MIN(release_date),MAX(release_date))
AS year_difference
FROM Movies
JOIN Movie_Directors md ON Movies.movie_id=md.movie_id
JOIN Directors d ON md.director_id=d.director_id
WHERE d.directo_name='Jon Favreau';
"Find movies released between 1995 and 2000."
SELECT movie_title,release_date
FROM Movies
WHERE release_date BETWEEN '1995-01-01' AND '2000-12-31';
"What are the release months of all the movies in the database?"
SELECT DISTINCT EXTRACT(MONTH FROM release_date) AS release_month
FROM Movies;

 

FAQs for SQL Builder

What databases are supported for SQL Builder?

The following databases are supported by Coefficient's SQL Builder:

  • Snowflake
  • PostgreSQL
  • MySQL
  • RedShift
  • MS SQL

Do I need to specify my data schema when describing my SQL query?

No, the SQL Builder is designed to be schema-aware, meaning it can automatically detect and work with the schema associated with your data to generate SQL queries.

Which data schema is used with SQL Builder?

SQL Builder employs the default schema for your data when generating queries. In the case of having multiple schemas, a dropdown appears to select different schemas. SQL Builder automatically selects your default schema as the target for query generation.

Can SQL Builder write cross-schema queries?

Unfortunately, the SQL Builder does not support cross-schema queries. This means that you can only interact with and generate queries within the confines of the default schema.

Which GPT model does the SQL Builder use?

SQL Builder uses GPT 4 Turbo as it offers the best compromise by far between costs, speed, and quality.

Why isn't SQL Builder available for my schema?

If you are unable to see SQL Builder for your schema, it may be due to limitations of OpenAI related to the size of your schema. OpenAI cannot handle massive schemas, and if your schema exceeds the limit set, the SQL Builder feature will be disabled.

Was this article helpful? 0 out of 0 found this helpful