Businesses rely on data analysis for insightful decisions. If your company has been existing for a while, you probably have huge buckets of data. Analyzing these functions can prove difficult if you still use basic spreadsheets. 
Google BigQuery is a Cloud Data Warehouse solution by Google that helps businesses and developers query huge amounts of data within short periods. Nevertheless, beginners can find Google BigQuery difficult to maneuver, especially with advanced analytic functions. Two of these functions are Google BigQuery First_Value and Last_Value. 

In this article, you will read about Google BigQuery, its features, and understand the usage of Google BigQuery First_Value and Last_Value functions.

Introduction to BigQuery

Google BigQuery Logo

Google BigQuery is Google’s Data Warehouse for Big Data. This service enables users to store, analyze, and manage vast amounts of data at affordable costs. Google BigQuery is also filled with built-in features like business intelligence, federated queries, and machine learning that facilitate easy data processing. 

Google BigQuery is highly scalable. You can start with the minimum memory size that the data warehouse offers and upgrade your memory size as your business improves. Fortunately, Google BigQuery only charges you according to the number of petabytes you use per query.
Furthermore, Google BigQuery offers a variety of functions that its subscribers employ to query their data. Here are some Google Google BigQuery terms and functions that you should get familiar with in that regard:

  • WITH:  WITH is used to emulate a temporary table from certain values.
  • SELECT: SELECT indicates the columns that a query will return. 
  • FROM: The FROM statement shows the tables from which the query will return rows. 
  • ORDER BY: This is used to indicate the order in which the query will return the results.
  • ASC | DESC: Directs the query to sort the result in ascending or descending order of the values
  • NULL: NULL points to empty values or ‘0’. When ASC is added to the query, null values appear first. In contrast, NULL values usually appear last in DESC. If you don’t want NULL values to appear in your query results, you can use the IGNORE NULL function.
  • RESPECT: RESPECT directs the query to include a value in the result. It is usually combined with the NULL statement.
  • WINDOW: A window refers to a group of rows where you can apply analytic functions.
  • OVER: This statement introduces the window for a particular function.
  • VALUE_Expression: This is the expression whose value the query is expected to return.
  • ANALYTIC FUNCTION: Analytic functions return one value for each in a table. Examples of analytic functions are first_value and last_value.
  • PARTITION BY: Based on an ASC or DESC command, PARTITION BY divides the rows into groups and places rows that share similar values close to each other. 
Simplify BigQuery Migrations with Hevo

Facing challenges migrating your data to BigQuery? Migrating your data can become seamless with Hevo’s no-code intuitive platform. With Hevo, you can:

  1. Automate Data Extraction: Effortlessly pull data from 150+ connectors(and other 60+ free sources).
  2. Transform Data effortlessly: Use Hevo’s drag-and-drop feature to transform data with just a few clicks.
  3. Seamless Data Loading: Quickly load your transformed data into your desired destinations, such as BigQuery.

Try Hevo and join a growing community of 2000+ data professionals who rely on us for seamless and efficient migrations. 

Get Started with Hevo for Free

For instance, in the table: 

FruitNumber
Mango15
Banana20
Orange11
Pear20
Cashew11
Strawberry15
Blueberry20

If you indicate PARTITION BY number in your SELECT statement, the query will return the results as

Fruit Number
Orange11
Cashew11
Mango15
Strawberry15
Banan20
Pear20
Blueberry20

Key Features of Google BigQuery

A few features of Google BigQuery are listed below:

  • BI Engine: Google BigQuery comes with an in-memory analysis service that enables users to analyze large datasets in the Data Warehouse itself, making the sub-query response time faster.
  • Fault-tolerant Structure: Google BigQuery ensures that your data remains protected in every condition. It prevents data loss and provides real-time logs for any error in an ETL process.
  • Encrypted Models: Google BigQuery ML allows users to encrypt machine learning models using customer-managed encryption keys (CMEK).

Understanding Google BigQuery First_Value and Last_Value

The Google BigQuery First_Value function is used to query the first row in a table. When you input this function in your query, it will return only one value. On the other hand, the Google BigQuery Last_Value function returns a single value for the last row in your target table. 

Usually, Google BigQuery users apply Google BigQuery First_Value and Last_Value functions to arrange values in a table in ascending or descending order. For instance, you may use these functions to order the scores of a group of students to discover the most outstanding students.

The typical syntax for a Google BigQuery FIRST_VALUE statement is as follows:

FIRST_VALUE (expression [ IGNORE NULLS | RESPECT NULLS ] ) 
OVER [PARTITION BY expression_list ]
[ ORDER BY order_list frame_clause ])

The syntax for a Google BigQuery LAST_VALUE statement is as follows:

LAST_VALUE (expression [IGNORE NULLS | RESPECT NULLS]  )
OVER [PARTITION BY expression_list]

Now, let’s apply the Google BigQuery First_Value and Last_Value syntax in practice.

Integrate Aftership to BigQuery
Integrate Drip to BigQuery
Integrate Firebase Analytics to BigQuery

Using Google BigQuery First_Value and Last_VALUE

The following example of Google BigQuery First_Value and Last_Value outlines the highest scores across a list of student grades in a school.

FIRST_VALUE

If you are running a query for a Google BigQuery FIRST VALUE function, it should look like this:

WITH students AS
(SELECT ‘Jane Kennedy’ as Name,
‘1’ as Class,
‘70’ as Score 
UNION ALL SELECT ‘Matthew Smith’ , ‘2’ , ‘81’
UNION ALL SELECT ‘Mary Lane’, ‘1’, ‘50’
UNION ALL SELECT ‘Harry Song’,  ‘3’ , ‘64’
UNION ALL SELECT ‘Isiah Matthews’,’1’ , ‘78’
UNION ALL SELECT ‘Joseph Meyers’ , ‘3’, ‘55’
UNION ALL SELECT ‘Andrew Crawford’ , ‘1’ , ‘67’
UNION ALL SELECT ‘Jamie Sylvester’ , ‘2’ , ‘ 57’
UNION ALL SELECT ‘Catherine Adams’ , ‘3’ , ‘48’
UNION ALL SELECT ‘Patrick Cain’ , ‘ 2’ , ‘49’
FROM (
SELECT Name, 
Class,
Score,
FIRST_VALUE  (Score)
OVER ( 
PARTITION BY Class ORDER BY Score DESC 
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) );
  • The WITH students AS clause defines a temporary table called students with columns for Name, Class, and Score.
  • Each UNION ALL statement inserts a new student record into the temporary table.
  • The main SELECT retrieves data from students and includes a calculated HighestScore column.
  • The FIRST_VALUE(Score) OVER (PARTITION BY Class ORDER BY Score DESC) function finds the highest score in each class, adding it as HighestScore for each student.
  • The final output lists each student’s name, class, score, and the highest score in their class.

The above query of Google BigQuery First_Value will return the following results :

Name ClassScore First_Value
Harry Song36464
Joseph Meyers35564
Catherine Adams34864
Matthew Smith28181
Jamie Sylvester25781
Patrick Cain4981
Isiah Matthews17878
Jane Kennedy 17078
Andrew Crawford 16778
Mary Lane15078

LAST_VALUE

Here’s how to query your data for a Google BigQuery LAST_VALUE function:

WITH students AS
(SELECT ‘Jane Kennedy’ as Name,
‘1’ as Class,
‘70’ as Score 
UNION ALL SELECT ‘Matthew Smith’ , ‘2’ , ‘81’
UNION ALL SELECT ‘Mary Lane’, ‘1’, ‘50’
UNION ALL SELECT ‘Harry Song’,  ‘3’ , ‘64’
UNION ALL SELECT ‘Isiah Matthews’,’1’ , ‘78’
UNION ALL SELECT ‘Joseph Meyers’ , ‘3’, ‘55’
UNION ALL SELECT ‘Andrew Crawford’ , ‘1’ , ‘67’
UNION ALL SELECT ‘Jamie Sylvester’ , ‘2’ , ‘ 57’
UNION ALL SELECT ‘Catherine Adams’ , ‘3’ , ‘48’
UNION ALL SELECT ‘Patrick Cain’ , ‘ 2’ , ‘49’
FROM (
SELECT Name, 
Class,
Score,
LAST_VALUE (Score)
OVER ( 
PARTITION BY Class ORDER BY Score DESC 
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) );
  • The WITH students AS clause creates a temporary table called students with columns Name, Class, and Score.
  • Each student’s information is added using UNION ALL to include every entry.
  • The LAST_VALUE(Score) OVER (PARTITION BY Class ORDER BY Score DESC) function picks the lowest score in each class since scores are sorted in descending order.
  • The ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ensures the function examines all scores within each class.
  • The result displays each student’s name, class, score, and the lowest score for their class.

The query above for Google BigQuery Last_Value will return the following results:

Name ClassScore LAST_VALUE
Harry Song36448
Joseph Meyers35548
Catherine Adams34848
Matthew Smith28149
Jamie Sylvester25749
Patrick Cain4949
Isiah Matthews17850
Jane Kennedy 17050
Andrew Crawford 16750
Mary Lane15050

Conclusion

In this article, you learned about Google BigQuery, Google BigQuery First_Value, and Last_Value. You also read about the usage and how Google BigQuery First_Value and Last_Value functions work, so you can easily spot important values amidst a large table. Google BigQuery First_Value and Last_Value make it easier for Data Scientists and Data Analysts to query data in Google BigQuery Data Warehouse.

Companies store valuable data from multiple data sources in Google BigQuery. The manual process to transfer data from source to destination is a tedious task. Hevo Data is a No-code Data Pipeline that can help you transfer data from any data source to desired Google BigQuery. It fully automates the process to load and transform data from 150+ sources to a destination of your choice without writing a single line of code. 

Want to take Hevo for a spin? Sign Up here for a 14-day free trial and experience the feature-rich Hevo suite firsthand.

FAQ on BigQuery FIRST_VALUE and LAST_VALUE Functions

How do you take the first value in BigQuery?

You can do this using the first_value function.
Syntax: SELECT FIRST_VALUE(column_name) OVER (PARTITION BY partition_column ORDER BY order_column) AS first_value FROM your_table;

What are FIRST_VALUE and LAST_VALUE in BigQuery?

First_value: Gets a value for the first row in the current window frame.
Last_value: Gets a value for the last row in the current window frame.

How do you use first () in SQL?

The FIRST() function returns the first value in a set of values. However, it’s important to note that the FIRST() function is not supported in all SQL databases.

Isola Saheed Ganiyu
Technical Content Writer, Hevo Data

Isola is an experienced technical content writer specializing in data integration and analysis. With over seven years of industry experience, he excels in creating engaging and educational content that simplifies complex topics for better understanding. Isola's passion for the data field drives him to produce high-quality, informative materials that empower professionals and organizations to navigate the intricacies of data effectively.