Arithmetic Operators

To execute the queries shown in the chapter open the BigQuery Console from the main menu as shown in the screenshot below. Click on the Compose Query button in the BigQuery Console to type the queries and execute it. More details about the BigQuery Console is explained in the beginning of the next chapter.

The following are the arithmetic operators that are available for use in queries in both standard and legacy SQL. The + operator adds two numbers and returns the output. Type the query below in the query editor and press Run Query button to see the output.

SELECT 1 + 2

The screenshot below shows the output of the query and various options available like saving the query to the project with a name for later use and other options.

The + operator can be used for string concatenation in legacy SQL as shown in the query below.

#legacySQL
SELECT 'ABC' + 'DEF'

The minus operator - subtracts second number from first number and outputs the result. The output of this query is 1.

SELECT 3-2

The multiplication operator * multiplies the numbers and displays the result. The output of the query below will be 7.5

SELECT 3.0 * 2.5

The division operator / returns the value as float even if the values in the operation are integers. The following query returns the value as 4.0. BigQuery will return null when any number is divided by 0.

SELECT 8/2

To convert the output of the above query to Integer use cast statement as shown below. The output will be 4 for this query.

SELECT CAST(8/2 as INTEGER)

The modulus operator (reminder division) % will return the reminder of the two numbers in the division operation. Only integers can be used in this operation and BigQuery throws an error if any of the operand is float. The query below outputs the value of 2.

SELECT 5%3
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.205.99