Show Menu
Cheatography

SQL Cheat Sheet (DRAFT) by

Basic SQL commands used in the Google Cybersecurity Certificate.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

SQL Query

SELECT
What columns to return
SELECT *
[*] returns all columns in a table
FROM
Which table to query
ORDER BY
Organizes the data
;
Placed at the end to finish a query
>SELECT *
>FROM [table_id]
>ORDER BY [colum­n_id] ;

Note: Commands are not case-s­ens­itive, titles of columns are

Filtering

WHERE
Indicates the condition for a filter
BETWEEN
Filters for numbers or dates within a range
AND
Filter with two condit­ions, both must be met
OR
Connects two condit­ioins, either can be met
LIKE
Search for a pattern in a column (LIKE 'IT%' ;)
NOT
Negates a condition
=
Operator to set a condition ([colu­mn_­title] = 'the title' ;)
_
Wildcard, substi­tutes for one other character
%
Wildcard, substi­tutes for any number of other characters
>SELECT *
>FROM [table_id]
>WHERE [colum­n_id] [=/>/<] 'Title' [AND/L­IKE/OR] 'Title' [Titl_ or Til%] ;

>WHERE [colum­n_id] BETWEEN 'Title' AND 'Title' ;

>WHERE NOT [colum­n_id] = 'Title' ;

'Title' = department name, date, or time

Numeric Operators

<
Less than
>
Greater than
=
Equal to
<=
Less than or equal to
>=
Greater than or equal to
<>
Not equal to

Examples

SELECT * FROM log_in­_at­tempts WHERE login_time > 'X' AND success = Y;
SELECT * FROM log_in­_at­tempts WHERE login_date = 'X' OR login_date = 'Y';
SELECT * FROM log_in­_at­tempts WHERE X country LIKE 'Y';
SELECT * FROM employees WHERE department = 'Marke­ting' AND office LIKE 'East%';
SELECT * FROM employees WHERE department = 'Finance' OR department = 'Sales';
SELECT * FROM employees WHERE NOT department = 'Infor­mation Techno­logy';
SELECT * FROM log_in­_at­tempts WHERE login_date X '2022-­05-09';
SELECT * FROM log_in­_at­tempts WHERE login_date BETWEEN '2022-­05-09' AND '2022-­05-11';

JOINS

INNER JOIN
Returns the rows where there is a match, returns all specified columns
LEFT JOIN
Returns all records of first table, but only rows of the second that matched a specified column
RIGHT JOIN
Returns all records of second table, only returns rows from first table that matches specified column
FULL OUTER JOIN
Returns all records from both tables, completely merges two tables
>SELECT *
>FROM [table_id #1]
>[T­YPE­_of­_JOIN] [table_id #2] ON [table­1.c­olumn] = [table­2.c­olumn] ;

Aggregate Functions

COUNT
returns a single number that represents the number of rows returned from your query
AVG
returns a single number that represents the average of the numerical data in a column
SUM
returns a single number that represents the sum of the numerical data in a column