Cheatography
https://cheatography.com
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 [column_id] ;
Note: Commands are not case-sensitive, 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 conditions, both must be met |
OR |
Connects two conditioins, either can be met |
LIKE |
Search for a pattern in a column (LIKE 'IT%' ;) |
NOT |
Negates a condition |
= |
Operator to set a condition ([column_title] = 'the title' ;) |
_ |
Wildcard, substitutes for one other character |
% |
Wildcard, substitutes for any number of other characters |
>SELECT *
>FROM [table_id]
>WHERE [column_id] [=/>/<] 'Title' [AND/LIKE/OR] 'Title' [Titl_ or Til%] ;
>WHERE [column_id] BETWEEN 'Title' AND 'Title' ;
>WHERE NOT [column_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_attempts WHERE login_time > 'X' AND success = Y; |
SELECT * FROM log_in_attempts WHERE login_date = 'X' OR login_date = 'Y'; |
SELECT * FROM log_in_attempts WHERE X country LIKE 'Y'; |
SELECT * FROM employees WHERE department = 'Marketing' AND office LIKE 'East%'; |
SELECT * FROM employees WHERE department = 'Finance' OR department = 'Sales'; |
SELECT * FROM employees WHERE NOT department = 'Information Technology'; |
SELECT * FROM log_in_attempts WHERE login_date X '2022-05-09'; |
SELECT * FROM log_in_attempts 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]
>[TYPE_of_JOIN] [table_id #2] ON [table1.column] = [table2.column] ;
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 |
|