SQL Cheat Sheet
SQL Cheat Sheet
LEFT JOIN
1 ATA B ASE
D
dml commands
BETWEEN The BETWEEN operator can be used to filter SELECT * FROM table_name LIMIT 5 ;
by a range of values.
G RANT SELECT , UP DATE ON TABLE_1
--Limit is used to limit the result set SELECT col1, col2
-- reates a new database LIKE The LIKE operator can be used inside of a to the specified number of rows.
C
--Used to grant a user access WHERE clause to match a specified pattern.
LEFT JOIN table_name t2
Wildcard operator pattern to match zero or more ORDER BY col1 ASC [ DESC ] ; TABLE 1 TABLE 2
G RANT SELECT , UP DATE ON TABLE_1
unspecified character(s). --Sort the result set in ascending or L
TO USER_1, USER_2; The _ wildcard can be used in a LIKE
descending order 1 E
F
A 1 B
CO MM AN S --Used to grant a user access
2 sql
=
_
J
B 2 A
database ORDER BY col1 LIMIT n OFFSET offset; O
CREATE TABLE table_name
dcl commands
IN
The IN operator is used to compare the
specified value. AS - Columns or tables can
--Skip offset of rows and return the 3 I
N C 3
( column1 datatype,
c o mmi t Saves all the transactions made on a be aliased using the AS clause. next n rows based on LIMIT.
Returns all rows from the table on the left side of the
column2 datatype,
database.
It is used to search for the presence of a row SELECT col1, aggregate (col2)
JOIN and matched rows from the right side of the Join
column3 datatype); E IST
It is used to undo transactions which are
FROM table_name G RO UP BY col1;
X
ro llb a ck in a table.
--The create table statement creates not yet been saved.
--GROUP BY Groups rows using an aggregate R I GH T JOIN
a new table in a database tcl commands
where
function
SELECT column_name FROM table_name
SELECT col1, col2
ADD column_name datatype; --Retrieve data from specified columns -- olumn values can be or have no FROM table_name
RI G HT JOIN table_name t2
--The Alter table statement is used value. These records can be atc ed using m h ON condition;
to odify the columns of an existing
m
SELECT * FROM table_name; the and operators. --Filter groups using the HAVING clause. TABLE 1 TABLE 2
table and add a new column.
IS NULL IS NOT NULL
R
--Retrieve the data from all fields in union 1 I
G A 1 B
ALTER TABLE table_name
the table. SELECT col1, col2 FROM table_name
DESC table_name; H
ADRO P COL U MN column_name; SELECT col1,col2..FROM table_name
U NION --Describes the structure of the table. 2 T
J
O
B = 2 A
--The Alter table statement is used WHERE condition; SELECT col1, col2 FROM table_name;
3 I
N C C
to odify the columns of an existing
m
--Used to filter the records based --Co bine rows from two queries wit out 10 CASE
table and Drop column. on a particular condition.
m
any duplicates.
h
Returns all rows from the table on the right side of the
ALTER TABLE table_name
SELECT column_name,
JOIN and matched rows from the left side of the Join
RENAME col_name TO col_newname; 7 S Q L CONST R AINTS union all CASE
duplicates .
CROSS JOIN table_name t2
J
B = 1 B
ddl commands
satisfy some particular condition or not
union SELECT col1, col2
3 O
I C 1 C
DEFA U LT It specifies a default value for a column
FROM table_name t1
N
INSERT INTO table_name
when no value is specified for that field SELECT col1, col2 FROM table_name
INNER JOIN table_name t2
2 A
V AL U ES (value1, value2); MIN U S
8 O P E R ATO R S SELECT col1, col2 FROM table_name;
ON condition; 2 B
UP DATE table_name
B = 2 A 3 B
U T
FULL JOIN L AST _ anking is assigned to the rows based The implementation of APPROX_COUNT_DISTINCT() Creating a search index:
in d o w
●
Gives the last value to all rows. R
V A LU E ()
w
on a specified column. Ranks are not has a much smaller memory requirement as Indexes are created for one or more columns in
SE L ECT col1, col2
functions skipped. compared to the COUNT(DISTINCT) function a table, using the CREATE INDEX command.
F RO M table_name t 1
last_
Gives the last value to all rows. Syntax: CREATE SEARCH INDEX index_name ON
FULL J OIN table_name t2
value() s e l e ct the database selects the given columns,
lo w er () :
`dataset.table_name` (column_name);
ON condition; istinct
Nth
d repeated values are removed, The LOWER() function converts a string to lowercase.
Gives Nth value to all rows. Dropping a search index:
TABLE 1 TABLE 2 value()
union /
upper () :
B An index can be dropped using SQL DROP command.
1 A 1 NTILE() Divides the rows to ‘n’ number of small int e r s e ct /
the database applies set operations, The UPPER() function converts a string to uppercase.
buckets. e xc e pt Syntax: DROP SEARCH INDEX index_name
2 B = 2 A
The cumulative percentage of the records order the results are sorted,
REGEXP _ C ONT AI N S () :
ON `dataset.table_name`;
cume_
by
Returns TRUE if the value is a partial match for
is calculated from the first row to the ●
3 C C dist() current row for the specified column. O FF SET the first rows are skipped, the regular expression, regex.
18 sql partitioning
3 l imit / ● If the regex argument is invalid, the
Partitioning is a database process where very
14 i
v e ws f e tch / top
only the first rows are selected function returns an error.
large tables are divided into multiple
Returns all rows from bith sides even if join condition is ● You can search for a full match by using ^
smaller, individual parts.
12 aggr e gat e fu n c tions --It creates a simple view. Syntax: REGEXP_CONTAINS(value, regex) access only limited amount of data can run
count ( )
faster because there is less data to scan.
avg() returns the average of a list Function with the DISTINCT clause eliminates the REGEXP _ extract () :
SE L ECT V IE W view_name
repetitive appearance of the same data. The DISTINCT
returns the number of elements of a list. -Time-unit column: Tables are partitioned based on
INNER JOIN table_name t2
count (*)
a TIMESTAMP, DATE, or DATETIME column in the
min() returns the minimum value of a list. ON condition; When * is used as an argument, it simply counts the ● If the regular expression contains a table.
max() returns the maximum value of a list. --It creates a complex view total number of rows including the NULLs.
capturing group, the function returns the -Ingestion time: Tables are partitioned based on
substring that is matched by that capturing the timestamp when BigQuery ingests the data.
count ( 1 )
13 a d v an c e d aggr e gat e fu n c tions CREATE TE M PORAR Y V IE W view_name
group.
ov e r () I
really does is that it replaces all the records you ●
analytical function --It creates a temporary view. get from query results with the value 1 and then capturing group, the function returns the
Use the CREATE TABLE statement with a SELECT AS
P a r tition counts the rows meaning it even replaces a NULL with entire matching substring.
C reates a partition internally and later clause for the query. Include a PARTITION BY
() 1 meaning it takes NULLs into consideration while
by p erforms the specified operations. DROP V IE W view_name; clause to configure the partitioning.
counting. ● Returns an error if:
r o w _ num b e r
Provides row numbers for all the rows --Delete a view. --The regular expression is invalid
Copying individual partitions:
() based on a specified column in the table. C OUNT ( colu m n _ na m e ) - --The regular expression has more than one ● You can copy the data from one or more
r an k () on a specified column. Skips the rank counts the total number of rows excluding the NULLs
All query elements are processed in a very strict order:
meaning it will not take NULLs into consideration.
Syntax: REGEXP_EXTRACT(value, regex) Copying partitions is not supported by
when it contains the same values. ●
Query execution order. Console.
L A G () the data are grouped by conditions databases. They are small, fast, and less
C ompares the current value with the g r oup by
the exact number of distinct values in a ● But you can't delete the special NULL or
p revious value. specified in the WHERE clause, memory-consuming.
The last value becomes NULL. Compares A gg r e gat e the aggregate functions are applied to the can run much faster and consume significantly ● Indexing a table or view surely improves
L EAD () functions groups created in the GROUP BY phase,
the current value with the next value. less memory. the performance of queries and applications. ● You can only delete one partition at a