In this article:

Import SQL to Google Sheets (Easiest Way in 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 and trying our software for expiration dates!

Automate repetitive tasks with Lido

Save hours on repetitive and tedious work. Lido is a new spreadsheet that connects your spreadsheets, forms, PDFs, and email inbox.

Trigger
Action
Select trigger
When a cell value in Google Sheets changes
Check Google Sheets for today's date
When a new row is added to Google Sheets
On new Google Form submission
Send me a daily reminder
On new Typeform submission
When a cell value in database changes
Check database for today's date
When a new row is added in database
When a new HubSpot customer is created
Select action
ADDCALENDAREVENT
Creates a one- or multiple-day calendar event with optional attendees
ADDHUBSPOT
Adds an object to Hubspot
ADDSLACKCHANNEL
Create a Slack channel, and optionally add a topic or members
CALLURL
Makes any HTTP request
CREATEGOOGLEDOC
Replaces [@column_name] values in a Google Doc with the corresponding table row's values
CREATEPDF
Replaces [@column_name] values in a Google Doc with the corresponding table row's values, then export it as a PDF
FETCH
Makes any HTTP request and returns the response
INSERTROWS
Inserts given array below defined values in given worksheet
SENDGMAIL
Sends an email using your Google account
SENDOUTLOOK
Sends an email using your Microsoft Outlook account
SENDSLACK
Sends a Slack message
SENDSMS
Sends an SMS message using your Twilio account
UPDATECELL
Updates cells with given values
UPDATEHUBSPOT
Updates a property of a Hubspot object
Trigger is required
Action is required
Get started