Skip to content

SELECT Statement in BigQuery

In SQL,Query statements scan one or more tables or expressions and return the computed result rows. With SELECT statement, you can specify the columns you want to retrieve, as well as any filtering, grouping, or ordering criteria you want to apply to the data.

SELECT as Value

You can assign any value to a column or multiple columns. In the example, the “apple” is the string value, the “fruit” is the alias of the column.

> SELECT "apple" AS fruit   # Assign a  value "apple"  to fruit
/*-------+
 | fruit | 
 +-------+
 | apple |   
 *-------+


> SELECT "orange" AS fruit, "tomato" AS vegetable
/*--------+-----------*
 | fruit  | vegetable|
 +--------+----------+
 | orange | tomato   |
 *--------+----------*/

SELECT multiple columns

You can select one or more columns and their records with SELECT()

# select columns from the table (Name,Quantity)
SELECT NAME,Quantity 
FROM `instruction-415216.examples.ivys_shop`   #address of the table
RowNAMEQuantity
1Kiwi99
2Kiwi41
3Kiwi74
4Kiwi39
5Kiwi86

SELECT ALL

If you want to have a quick view of the table, you can use SELECT * to retrieve all data.

 SELECT * 
 FROM `instruction-415216.examples.ivys_shop` 
RowNameCategoryPriceQuantitySoldPurchase_Price
1KiwiFruit8.8699102.7
2KiwiFruit4.6141352.34
3KiwiFruit0.8574572.56
4KiwiFruit2.739220.95
5KiwiFruit7.5286800.6

SELECT EXCEPT

SELECT EXCEPT is a good way to retrieve data and filter the columns that you don’t want. For example, I want to get all columns except Category. I can list each column’s name or I just use the EXCEPT .

 SELECT * EXCEPT  (Category)
 FROM `instruction-415216.examples.ivys_shop` 
RowNamePriceQuantitySoldPurchase_Price
1Kiwi8.8699102.7
2Kiwi4.6141352.34
3Kiwi0.8574572.56
4Kiwi2.739220.95
5Kiwi7.5286800.6


SELECT DISTINCT

You have noticed that the name of fruit has many duplicates. If I want to get the name of each fruit and vegetable, I can use the DISTINCT to get the result. A SELECT DISTINCT statement discards duplicate rows and returns only the remaining rows.

SELECT DISTINCT NAME 
FROM `instruction-415216.examples.ivys_shop` 
RowNAME
1Kiwi
2Apple
3Mango
4Peach
5Banana
6Grapes
7Orange