AI SQL Builder

Section header image mobile

Coefficient's AI SQL Builder lets you create SQL queries directly in Google Sheets or Excel without having to write everything from scratch. You can simply enter a prompt or describe the data you want to import, and the AI assistant will generate the SQL query for you. Whether you're new to SQL or an experienced user, you can also edit and refine queries as needed, making it easier to get the data you need.

Using AI SQL Builder

Edit Existing Query

Examples of AI SQL Builder Prompts and Outputs

Select Queries

Join Queries

Group By Queries

Order By Queries

Date Function Queries

FAQs for AI SQL Builder

ℹ️ NOTE: Coefficient's AI SQL Builder has schema awareness, so you don’t need to manually specify your data schema when describing the SQL query you want to build.

Using AI SQL Builder

ℹ️ EXAMPLE: Create a MySQL import to count the number of movies per genre using AI SQL Builder.

1. Open the Coefficient sidebar and click "Import from...".

 

2. Select your (MySQL) data source connection.

 

3. Choose "AI SQL Builder".

 

4. The Import Preview window opens, allowing you to enter your description/prompt, select your table schema, then click "Generate SQL" (eg, "Count the number of movies for each genre.")

5. The AI SQL Builder will update, showing you the SQL query at the top and a sample of the data output results at the bottom. 

6. To make changes to the generated query, you can ask the AI SQL Builder by adding new prompts and clicking the "Generate SQL" button again. (eg, "Include a total at the bottom"). 

ℹ️ PRO TIP: Click on the "Refresh Preview" button to update the data output preview if you manually make changes to the query.

7. Click "Import" and make sure you give this import a UNIQUE name (eg, "Movies by genre").

8. Congratulations on your first successful import using the AI SQL Builder with Coefficient! 🎉

Edit Existing Query

You can edit an existing SQL query in a few quick steps using Coefficient’s AI SQL Builder.

1. Open the Coefficient sidebar and click on your existing import (eg, Movies by genre).

2. Click the "Edit SQL" button.

3. The Import Preview window opens, and you can manually edit the raw query, or click "Use AI to write a query..." to ask the AI to make the edits for you.

4. Describe the change you'd like to make and click "Generate SQL". The query will update, and the new results (a sample) will appear underneath the query window in the preview.  Click " Done " when your edits are complete. 

5. The preview window will close, and the sidebar will automatically refresh your import to update the data on your sheet. 🎉

Examples of AI 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 AI SQL Builder

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

No, the AI 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 does AI SQL Builder use?

The AI SQL Builder uses your default schema when generating queries. If your data source has multiple schemas, a dropdown will appear, allowing you to select a different schema. By default, SQL Builder automatically selects your default schema as the target for query generation.

Can the AI SQL Builder write cross-schema queries?

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

Which AI model does the AI SQL Builder use?

Coefficient's AI SQL Builder leverages advanced AI models from leading providers, including OpenAI, to deliver powerful and accurate assistance.

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