The Most Powerful Function in Google Sheets: The “QUERY” Function

Video google sheet query select where

Do you want to level up your data manipulation skills in Google Sheets? Look no further than the “QUERY” function! Considered the most versatile and powerful function in Google Sheets, “QUERY” allows you to use data queries to manipulate your data in a variety of ways.

What is the “QUERY” Function?

The “QUERY” function in Google Sheets is a unique powerhouse that can perform the work of multiple other functions. With its ability to replicate most of the features of pivot tables and its incredible flexibility, this function is a game-changer for anyone working with data in Google Sheets.

How does the “QUERY” Function work?

The syntax of the “QUERY” function is straightforward. It takes three arguments:

  1. The range of data you want to analyze.
  2. The query you want to execute, enclosed in quotation marks.
  3. An optional number to indicate the number of header rows in your data.

Here’s an example of how the “QUERY” function works:

=QUERY(A1:D234, "SELECT B, D", 1)

In this example, the data range is A1:D234. The query statement, highlighted in green, tells the function to select columns B and D from the data. The third argument is the number 1, indicating that the original data had a single header row. This argument is optional and will be determined automatically by Sheets if omitted.

Key Points about the “QUERY” Function

  • Keywords are not case-sensitive. So, you can write “SELECT” or “select,” and both will work.
  • Column letters must be uppercase (A, B, C, etc.), or else you’ll encounter an error.
  • Keywords must appear in the following order (although you don’t have to use them all): select, where, group by, order by, limit, label.
  • There are a few other lesser-used keywords. You can find the complete list here.

Examples of the “QUERY” Function

Let’s explore some examples to see the “QUERY” function in action:

Example 1: Select Specific Columns

If you don’t want to select all the columns in your data, you can modify your “QUERY” function accordingly. For example:

=QUERY(A1:D234, "SELECT B, D", 1)

This time, we’ve selected only columns B and D from the original data, resulting in the following output:

Note: In this example, the “QUERY” function is in cell G1, so the output will be in columns G, H, I, and so on.

Example 2: Filtering Data with the WHERE Clause

The WHERE clause allows you to specify a condition that must be met, effectively filtering your data. It comes after the SELECT keyword. For instance:

=QUERY(A1:D234, "SELECT * WHERE D > 100000000", 1)

This query will select only the countries with a population greater than 100 million, resulting in the following table:

Example 3: Ordering Data with the ORDER BY Clause

The ORDER BY clause allows you to sort your data. You can specify the column(s) and the direction (ascending or descending). It comes after the SELECT and WHERE clauses. Let’s sort our data from smallest to largest population:

=QUERY(A1:D234, "SELECT * ORDER BY D ASC", 1)

The output will be:

To sort the data in descending order, from Z to A, modify the formula as follows:

=QUERY(A1:D234, "SELECT * ORDER BY B DESC", 1)

The output will be:

Example 4: Limiting the Number of Results with the LIMIT Clause

The LIMIT clause allows you to restrict the number of results returned. It comes after the SELECT, WHERE, and ORDER BY clauses. Let’s add a LIMIT clause to our formula and return only 10 results:

=QUERY(A1:D234, "SELECT * LIMIT 10", 1)

Now, the output will show only the first 10 results of our data:

Example 5: Performing Arithmetic Operations

You can perform standard mathematical operations on numerical columns. Let’s calculate the percentage of the world population represented by each country. We’ll divide the population column by the total (7,162,119,434) and multiply by 100 to get the percentages:

=QUERY(A1:D234, "SELECT B, C, (D / 7162119434) * 100", 1)

The resulting table will be:

Note: I applied formatting to the output column in Google Sheets to display only two decimal places.

Example 6: Renaming Columns with the LABEL Clause

Sometimes, the default column names for arithmetic columns can be unattractive. Fortunately, you can rename them using the LABEL clause, which comes at the end of the “QUERY” statement. Try this:

=QUERY(A1:D234, "SELECT B, C, (D / 7162119434) * 100 LABEL (D / 7162119434) * 100 'Percentage'", 1)

Advanced Techniques

The examples above cover the basics of the “QUERY” function, but there’s so much more you can do! If you want to dive deeper into advanced techniques such as adding a total row to your query formulas or using dates as filters, check out this lesson from my free course “Advanced 30.”

Get Started with the “QUERY” Function Today!

The “QUERY” function in Google Sheets is a game-changer for data manipulation. With its power and versatility, it opens up a world of possibilities for analyzing and summarizing your data. Start exploring this incredible function today and unlock the true potential of Google Sheets!

For more in-depth information on the “QUERY” function, you can refer to the official Google documentation. And don’t forget to check out my Crawlan.com website for more helpful tips and tutorials!

Now go forth and conquer the world of data with the mighty “QUERY” function in Google Sheets!

Related posts