In this article:

Import SQL to Google Sheets (Easiest Way in 2024)

May 8, 2024
>Click Here to Import SQL Data

In this article we will show you how to import SQL to Google Sheets in just a few clicks. Simply follow the steps below.

Import SQL to Google Sheets

1. Get Access Credentials for the SQL Server

First, you need to have access to the SQL server through an account. The following information will be needed:

  • server_url is for the url of the SQL server
  • port_number is the port number for the SQL server
  • database_name is the name of the SQL database
  • username is the username for access
  • password is the corresponding password for the given username
  • table_title is the name of the table you want to load

2. Connect your SQL Database to Lido

If you don’t have a Lido account, you can create a free one here.

Create a new file then click Connect Data.  Select your database type in the Add data modal then enter in your credentials from Step 1. 

3. Select Data

Now that your database is connected it’s time to select the exact data to import. 

Lido has a visual data browser so you can point and click to select the exact data you want to connect.  Use the dropdown menu in the upper left corner to change tables. Alternatively, you can write SQL directly by clicking the Custom tab.

Optional: Add filters to columns through these menus.

When you’re happy with your data in this view, click Add Data.

This creates a Table in Lido that is live one-way connected to your SQL database. Any changes in your database will be automatically reflected in Lido.  However, you cannot edit this data in Lido and must do so in the original source.

Things you can do with your connected data

Once your SQL data has been imported into a Lido spreadsheet, you can work with it using normal spreadsheet formulas.  

Clean and normalize data

For example, if your database only contains First Name and Last Name, but you need a column for Full Name, you can easily create one by adding a Computed Column.  

Join multiple tables together

Your data is likely scattered across multiple tables in your database or even multiple sources (like Salesforce, Hubspot, or Google Analytics).  Joining data 

Send emails or Slack messages powered by your data

You can easily send emails, slack and whatsapp messages to your imported data.

Import SQL to Google Sheets (Apps Script Method)

While this method is also effective, we now suggest using the Lido method above as it is much more user friendly and customizable.

1. Get Access Credentials for the SQL Server

First, you need to have access to the SQL server through an account. The following information will be needed:

  • server_url is for the url of the SQL server
  • port_number is the port number for the SQL server
  • database_name is the name of the SQL database
  • username is the username for access
  • password is the corresponding password for the given username
  • table_title is the name of the table you want to load

Keep them in a text file that can be used to safely store sensitive access credentials.

2. Open Google Apps Script

You can open Google Apps Script by clicking Extensions in the main menu, then selecting Apps Script.

Sql Server to google sheets, click extensions, select apps script

A new tab will be loaded for Apps Script. The right side of the screen is where we will insert a special function to import SQL data to Google Sheets.

Sql server to google sheets, google apps script page

3. Copy The Custom Script to Import SQL to Google Sheets

Copy the following code: 

====

var server = 'server_url';

var port = port_number;

var dbName = database_name;

var username = 'username';

var password = 'password';

var url = 'jdbc:mysql://'+server+':'+port+'/'+dbName;

function readData() {

 var conn = Jdbc.getConnection(url, username, password);

 var stmt = conn.createStatement();

 var results = stmt.executeQuery('SELECT * FROM table_title');

 var metaData=results.getMetaData();

 var numCols = metaData.getColumnCount();

 var spreadsheet = SpreadsheetApp.getActive();

 var sheet = spreadsheet.getSheetByName('Sheet1');

 sheet.clearContents();

 var arr=[];

 for (var col = 0; col < numCols; col++) {

   arr.push(metaData.getColumnName(col + 1));

 }

 sheet.appendRow(arr);

while (results.next()) {

 arr=[];

 for (var col = 0; col < numCols; col++) {

   arr.push(results.getString(col + 1));

 }

 sheet.appendRow(arr);

}

results.close();

stmt.close();

sheet.autoResizeColumns(1, numCols+1);

}

====

4. Paste the Script to Apps Script

Go back to the Google Apps Script tab. Click on the code area, press Ctrl+A to highlight the default code, then press Delete on your keyboard. That clears the area.

Clear google apps script area to insert custom code to import sql database to google sheets

Afterwards, press Ctrl+V or right-click again then select Paste. This will insert the code we copied into Google Apps Script

custom code to import sql database to google sheets inserted

5. Set Access Credentials in the Script

Replace certain lines in the script with the access credentials information you have:

Lines 1-5:


var server = 'server_url';

var port = port_number;

var dbName = database_name;

var username = 'username';

var password = 'password';

Line 11:

var results = stmt.executeQuery('SELECT * FROM table_title');

6. Save and Rename Project

Save the script by clicking the Save project icon near the top of the page. 

Google sheets import sql, save project to enable function

Afterwards, you can also name the script by clicking the default title Untitled project.

Google sheets import sql, click title to rename project name 

A small pop-up box will appear where you can rename the project. Type the name you want to give, then click Rename.

Rename project for script to import sql google sheets

We are now ready to authorize the script. 

script to import sql google sheets saved and renamed

7. Click the Run Button and Authorize the Script

When you run the function for the first time, Google Sheets will ask you for authorization with a box labeled Authorization Required. Click Continue

sql server to google sheets, authorization required for the script to run

A new window will appear, allowing you to select the Google account associated with the spreadsheet containing the button. 

sql server to google sheets, choose account to authorize the script

After selecting the account, a warning will appear reminding you that Google has not verified the app. Click the small link on the lower-left corner labeled Advanced.

sql server to google sheets, google has not verified this app warning

Another reminder will appear to continue only if you understand the risks and trust the developer. Click Go to Untitled project (unsafe) or whatever the project name is in Google Apps Script.

sql server to google sheets, google hasnt verified this app warning, link to continue using it appears

A new page will appear listing the permissions needed by the script. Click Allow

sql server to google sheets, list of permissions needed by the script shown

The script now works! In Apps Script you will get the following log:

Pull sql data into a google sheet, execution log confirms code running

And the SQL data has now been imported into the sheet.

Pull sql data into a google sheet, data imported to the sheet

FAQs

Can I use this to add other SQL databases such as PostgreSQL?

Absolutely! You can use the script in this tutorial for PostgreSQL. You can learn more about it and other ways to do so in our tutorial page dedicated to it.

Is there a better way to import SQL?

Want to import data from SQL databases to Google Sheets without needing to create a custom Google Apps script? Say no more! Lido offers a simple solution to seamlessly integrate data from multiple databases, including SQL, in just a few clicks. Click here to get started!

sql server to google sheets alternative

We hope this article has helped you and given you a better understanding of how to import SQL to Google Sheets. You might also like our articles on how to import JSON to Google Sheets and how to connect Snowflake to Google Sheets.

To optimize your workflow, we recommend checking out our template for contact lists in Google Sheets.

Get Google Sheets productivity and automation tips delivered straight to your inbox
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
We'll email you 1-3 times a week — and never share your information.

Work less, automate more!

Use Lido to connect your spreadsheets to email, Slack, calendars, and more to automate data transfers and eliminate manual copying and pasting. View all use cases ->