how to create a query in google sheets

How to Create a Query in Google Sheets

By Kevin Thomas
AD

Mastering Google Sheets' vast capabilities opens up a world of possibilities for data organization and analysis. One of its most powerful features is creating queries, enabling users to scan, extract, and manipulate data from diverse sources. So, let's dive into a step-by-step guide on how to create a query in Google Sheets.

Before we begin, ensure you have Google Sheets open and are working with a spreadsheet containing data. For this example, let's assume you have a dataset of customers in columns A to E, with headers: 'First Name', 'Last Name', 'Email', 'City', and 'State'.

Understanding Google Sheets Queries

A Google Sheets query is a powerful tool that extracts and organizes data from a range or even other spreadsheets. It behaves like a filter, allowing you to specify criteria for data selection, sort orders, and formatting. To create a query, you'll need to enter a formula starting with '=QUERY().'

AD
an image of a table with numbers and times
an image of a table with numbers and times
Google Sheets QUERY Function Tutorial - SELECT, WHERE, LIKE, AND, OR, LIMIT statements - Part 1
Google Sheets QUERY Function Tutorial - SELECT, WHERE, LIKE, AND, OR, LIMIT statements - Part 1
How to Create a Search Box Using Query in Google Sheets [2020]
How to Create a Search Box Using Query in Google Sheets [2020]

Let's start by creating a simple query to filter our customer data by city. But first, why would you want to create a query? In essence, queries help you keep your data organized, clean, and easily accessible. They're particularly useful in large datasets where manual filtering becomes impractical.

Creating a Simple Query

To create a query in Google Sheets, select the cell where you want the query result to display. Then, enter the following formula:

How to Create a Dashboard in Google Sheets (10 steps) - Query Formula
How to Create a Dashboard in Google Sheets (10 steps) - Query Formula
How to use Google Sheets: A beginner's guide | Zapier
How to use Google Sheets: A beginner's guide | Zapier
23 Best formulas for Google Sheets (+ Examples)
23 Best formulas for Google Sheets (+ Examples)

='QUERY('Customer Data!A:C', "SELECT A, B, C WHERE C = 'New York'")'

In this formula, 'Customer Data!A:C' is the range from which we're extracting data (columns A to C). The 'SELECT A, B, C' part specifies which columns we want to include in the query result. Finally, 'WHERE C = 'New York'' is the condition that rows must meet to be included in the result.

AD

Sorting and Formatting Query Results

Queries can also sort and format data. Let's enhance our query to sort the results by last name (in ascending order) and change the header font to bold.

Google Sheets Query Function - Part II
Google Sheets Query Function - Part II
Google Sheets Budget Tutorial: Master Your Finances Easily!
Google Sheets Budget Tutorial: Master Your Finances Easily!
How to Create a Pivot Table in Google Sheets
How to Create a Pivot Table in Google Sheets
Google Sheets - QUERY from Another Sheet, IMPORTRANGE, Use Multiple Tabs, Subquery Examples Tutorial
Google Sheets - QUERY from Another Sheet, IMPORTRANGE, Use Multiple Tabs, Subquery Examples Tutorial
How to Use Google Sheets Like a Pro
How to Use Google Sheets Like a Pro
How to Create a Data Entry Form in Google Sheets
How to Create a Data Entry Form in Google Sheets

Enter the following formula:

='QUERY('Customer Data!A:C', "SELECT A, B, C WHERE C = 'New York' LABEL A 'First Name', B 'Last Name', C 'City' ORDER BY B ASC", 1)', 'SELECT A, B, C WHERE C = 'New York' LABEL A 'First Name', B 'Last Name', C 'City' ORDER BY B ASC FORMAT A 'bold', B 'bold'','LEFT')'

AD

The functionality of this enhanced query remains the same. However, it now sorts the results by last name (in ascending order) and changes the font of the headers to bold.

How to Create an Automated Google Sheets Dashboard
How to Create an Automated Google Sheets Dashboard
QUERY Pivot Table -Google Sheets - Query Pivot, Group By, Month, Year Functions Tutorial - Part 6
QUERY Pivot Table -Google Sheets - Query Pivot, Group By, Month, Year Functions Tutorial - Part 6
Basic Formatting in Google Sheets
Basic Formatting in Google Sheets

Loading Data from Other Spreadsheets

Queries aren't limited to operating within the same spreadsheet. They can pull data from external Google Sheets or even CSVs. Let's explore how to create a query that retrieves data from another spreadsheet.

Querying Data from an External Spreadsheet

First, share the external spreadsheet with your current account (or collaborate with the owner if it's already shared). Then, enter the following formula:

AD

='QUERY("https://docs.google.com/spreadsheets/d/Your_SPREADSHEET_ID/gviz/tq?tqx=out:csv&sheet=Your_SHEET_NAME", "SELECT A, B, C")'

Here, replace 'Your_SPREADSHEET_ID' with the ID of the external spreadsheet, and 'Your_SHEET_NAME' with the name of the sheet you want to query. Again, 'SELECT A, B, C' specifies the columns you want to include.

And that's how you create, sort, format, and externalize queries in Google Sheets! With this new skill set, you're well on your way to streamlining your data organization process. So, what are you waiting for? Dive into your data, and let the power of Google Sheets queries smooth your path to insights!

AD