0% found this document useful (0 votes)
229 views4 pages

Create Table: Create Table As Select

This document provides a summary of 30+ SQL snippets for Google BigQuery including how to create and drop tables and views, use common table expressions, group by, order by, joins, dates functions, ranking and analytical functions, and more. All the snippets are included in the free Roboquery Chrome extension to help users convert SQL scripts to BigQuery.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views4 pages

Create Table: Create Table As Select

This document provides a summary of 30+ SQL snippets for Google BigQuery including how to create and drop tables and views, use common table expressions, group by, order by, joins, dates functions, ranking and analytical functions, and more. All the snippets are included in the free Roboquery Chrome extension to help users convert SQL scripts to BigQuery.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Become a Google Bigquery expert with this free 30+ SQL Snippets.

All these snippets are also included in the free Roboquery chrome
extension

Create table as select:


Create Table:
CREATE TABLE mydataset.mynewtable
AS
CREATE TABLE Dataset.TableName SELECT * FROM mydataset.myothertable
( ;
EmployeeNo INT64 NOT NULL,
FirstName STRING,
LastName STRING,
DOB DATE
) Drop Table:
PARTITION BY DOB
DROP TABLE Dataset.Tablename;
CLUSTER BY EmployeeNo;

Drop View:
Create View: DROP VIEW Dataset.Viewname;

CREATE OR REPLACE VIEW Dataset.ViewNa


me AS Create CTE:
SELECT
EmployeeNo,
FirstName, WITH subQ1 AS (SELECT SchoolID FROM R
LastName, oster),
DOB, subQ2 AS (SELECT OpponentID FROM Play
JoinedDate, erStats)
DepartmentNo SELECT * FROM subQ1
FROM Dataset.TableName; UNION ALL
SELECT * FROM subQ2;
Group by Having:
Derived Table: SELECT LastName, SUM(PointsScored) AS
ps
SELECT r.LastName FROM FROM Roster
( SELECT * FROM Roster) AS r; GROUP BY LastName
HAVING ps > 0;

Order By:
Select Distinct:
SELECT LastName, PointsScored, Oppone
SELECT DISTINCT * FROM DatasetName.My ntID
Table; FROM PlayerStats
ORDER BY SchoolID, LastName desc;

Select using Timetravel:


Insert into Table:
SELECT * FROM Dataset.Table INSERT into dataset.Inventory_New
FOR SYSTEM_TIME AS OF '2019-01- (
01 10:00:00-07:00'; product,
quantity,
supply_constrained
)
SELECT * FROM dataset.Inventory;
Group By:

SELECT LastName, SUM(PointsScored) as


pts
Insert values into Table:
FROM PlayerStats
GROUP BY LastName; INSERT into dataset.Inventory (produc
t, quantity)
VALUES('top load washer', 10),
('front load washer', 20),
('dryer', 30),
('refrigerator', 10);
Update Table: Inner Join:
UPDATE dataset.Inventory SELECT R.* FROM Roster R
SET quantity = quantity - 10 INNER JOIN PlayerStats P
WHERE product like '%washer%'; ON R.LastName = P.LastName;

Update From: Left Join:


UPDATE dataset.Inventory i SELECT R.* FROM Roster R
SET quantity = n.quantity LEFT JOIN PlayerStats P
FROM ( ON R.LastName = P.LastName;
select quantity,product from dataset.
NewArrivals
)n
WHERE i.product = n.product;
Parse Date:

SELECT PARSE_DATE("%Y%m%d", "20190927


Merge: ") as parsed_date;

MERGE dataset.Inventory T
USING dataset.NewArrivals S
ON T.product = S.product
Date Add:
WHEN MATCHED THEN
UPDATE SET quantity = T.quantity +
SELECT DATE_ADD(DATE "2008-12-
S.quantity
25", INTERVAL 5 DAY) as five_days_lat
WHEN NOT MATCHED THEN
er;
INSERT (product, quantity) VALUES(p
roduct, quantity);

Date Sub:
Delete:
SELECT DATE_SUB(DATE "2008-12-
25", INTERVAL 5 DAY) as five_days_ago
DELETE FROM dataset.Inventory WHERE q
;
uantity = 0;
--to delete all rows
DELETE FROM dataset.DetailedInventory
WHERE true;
Date Diff: Rank Over:

SELECT DATE_DIFF(DATE '2010-07- SELECT firstname, department, startda


07', DATE '2008-12- te,
25', DAY) as days_diff; RANK() OVER ( PARTITION BY department
ORDER BY startdate ) AS rank
FROM Employees;

Extract from Date:


Row_Number:
SELECT EXTRACT(DAY FROM DATE '2013-
12-25') as the_day;
SELECT firstname, department, startda
te,
ROW_NUMBER() OVER ( PARTITION BY depa
rtment ORDER BY startdate ) AS rank
Cast to Integer: FROM Employees;

SELECT CAST("123" AS INT64) AS Emp_Id


; Rows Between:
select firstname,
SUM(x) OVER (PARTITION BY y ORDER BY
Cast to String: z ROWS BETWEEN 1 PRECEDING AND 1
FOLLOWING) as ColumnAlias
SELECT CAST(123 AS STRING) AS Emp_Id;
from Employee;

Cast to Numeric:

SELECT CAST("123" AS NUMERIC) AS Emp_ This free cheat sheet is brought


Id; to you by Roboquery.
Roboquery helps you convert
your table DDL, views, SQL
scripts to Google Bigquery.
Install the free chrome
extension now

You might also like