Recent years have witnessed many new platforms and software in the field of data management. With the vast sea of information that is growing daily, most organizations are looking towards Cloud-based solutions to collect, store, and work on this precious commodity called data. BigQuery is one such popular Cloud-based Data Warehouse that provides high scalability, simplicity, and abstraction to its users.

Unlike its peers, BigQuery is serverless and hence provides services dynamically, which means that no provisioning or hardware management is required from the user. This blog will introduce you to Bigquery and will explain the BigQuery INSERT and UPDATE command in detail. These are SQL queries that can help you in managing your data in Google BigQuery.

Read along to learn more about BigQuery and understand the working of bigQuery INSERT and UPDATE commands!

BigQuery INSERT and UPDATE

BigQuery INSERT and UPDATE are the most used commands when it comes to Data Management in BigQuery. The syntax, importance, and use of both these commands are as follows:

Simplify your Data Analytics Using Hevo’s No-code Data Pipeline

Are you looking for an easy way to centralize your data to BigQuery? Hevo Data, a No-code Data Pipeline helps to integrate data from 150+ other data sources (Including 40+ Free Data Sources) and load it in a Data Warehouse of your choice to visualize it in your desired BI tool. Its fault-tolerant architecture ensures that the data is handled in a secure, consistent manner with zero data loss.

Get Started with Hevo for Free

1) BigQuery INSERT and UPDATE: INSERT Command

Out of the BigQuery INSERT and UPDATE commands, you must first learn the basic INSERT statement constructs to interact with the above table definitions.

INSERT query follows the standard SQL syntax. The values that are being inserted should be used in the same order as the columns. The below image shows an example of INSERT command.

INSERT Command in BigQuery
Image Source

You can execute a basic INSERT query with columns specified as given in the INSERT INTO example below.

INSERT INTO dataset.StockDetails(name, price) VALUES(‘Samsung Galaxy Note 10,’999’’)

An INSERT query without specifying columns can be executed as below.

INSERT INTO dataset.StockDetails VALUES(‘SG Note 10’,’Mystic Black’,’999’,’24’)

An INSERT query using a subquery can be executed as follows.

INSERT INTO dataset.stockDetails(product, color) VALUES(‘SG Note 10’, SELECT color from dataset.StockDetails WHERE product=’Samsung Galaxy Note 20’)

BigQuery also supports the WITH keyword in the QUERY syntax.

A point to note here is the implicit data typing mapping that is executed by BigQuery in the case of INSERT statements. This can lead to corrupted Databases in case the user is not careful. BigQuery will raise an error for data type mismatch only in cases where the incoming column cannot be converted to the target column data type.

Download the Cheatsheet on How to Set Up High-performance ETL to BigQuery
Download the Cheatsheet on How to Set Up High-performance ETL to BigQuery
Download the Cheatsheet on How to Set Up High-performance ETL to BigQuery
Learn the best practices and considerations for setting up high-performance ETL to BigQuery

2) BigQuery INSERT and UPDATE: UPDATE Command

In the case of BigQuery, each UPDATE statement must include a WHERE clause. This offers protection against the bulk erroneous updates if the developer accidentally forgets the WHERE clause. To update the complete table, you have to specifically mention WHERE TRUE.

The most basic UPDATE example will look like below.

UPDATE dataset.StockDetails SET color = ‘Mystic Green’ WHERE product=’SG Note 20 Ultra’

To update records by using a string match operation, the query will be as follows.

UPDATE dataset.StockDetails SET color = ‘Mystic Green’ WHERE product LIKE ‘%Note%’

The above statement will update the color of all records where the product contains ‘Note’ in its name.

An Update statement using a JOIN clause can be executed as below.

UPDATE dataset.StockDetails a SET color = ‘black’ FROM dataset.Stock b WHERE a.product = b.product AND b.outlet = ‘central_park’

The above query updates the color of all the records that belong to a particular store.

An explicit JOIN keyword has to be used in case the JOIN is between tables that are not getting updated. An example will be as follows.

UPDATE dataset.StockDetails SET quantity=’10’ FROM dataset.Stock INNER JOIN dataset.Store on Stock.outlet = Outlet. outlet_name WHERE StockDetails.product = Stock.product and Outlet.location = ‘New York’

The above command will set the quantity as 10 for all items in all outlets belonging to the location ‘New York’.

That covers the basics of INSERT and UPDATE statements in BigQuery. While implementing an ETL pipeline to synchronize the data between your transactional databases and data warehouse, an automated scheduled script using the above constructs can be used. Implementing such a script has a lot more additional complexities because of the existence of many factors like duplicate rows, data type translations, etc. 

But there is an alternative.

It is to use a completely managed ETL tool like Hevo that can accomplish data transfer between various sources and destinations easily.

That’s it! You can now try to implement the BigQuery INSERT and UPDATE commands by yourself.

Conclusion

This article teaches you how to work with Google BigQuery INSERT and UPDATE statements with ease. It also provides in-depth knowledge about the concepts behind every step to help you understand and implement them efficiently. Integrating complex data from a diverse set of data sources can be a challenging task and this is where Hevo saves the day!

Hevo is the only real-time ELT No-code Data Pipeline platform that cost-effectively automates data pipelines that are flexible to your needs. With integration with 150+ Data Sources (40+ free sources), we help you not only export data from sources & load data to the destinations but also transform & enrich your data, & make it analysis-ready.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. You can also have a look at the unbeatable pricing that will help you choose the right plan for your business needs.

Share your thoughts on BigQuery INSERT and UPDATE queries. 

Sarad Mohanan
Software Engineer, Hevo Data

With around a decade of experience, Sarad has designed and developed fundamental components of Hevo. His expertise lies in building lean solutions for various software problems, mentoring fellow engineers and exploring new technologies.

No-code Data Pipeline for your BigQuery Data Warehouse

Get Started with Hevo