SQL Commands and Functions Flashcards
SELECT
The SELECT block specifies which columns you want to output. Its format is SELECT , , …. Each column must be separated by a comma, but the space following the comma is optional.
SQL Comments
The contents between /* and */ are taken as comments and aren’t run as code.
– is another way of indicating comments. It is used to mark the rest of a single line as a comment. When we have multiple lines we want to comment out, /* … */ is more convenient than prepending – to each line.
FROM
The FROM block specifies which table we’re querying from. Its format is FROM . It always comes after the SELECT block.
WHERE
The WHERE block allows us to filter the table for rows that meet certain conditions. Its format is WHERE and it always goes after the FROM block.
Quotes
In SQL, strings are denoted by single quotes. Backticks (ie `) can be used to denote column and table names. This is useful when the column or table name is the same as a SQL keyword and when they have a space in them.
LIKE
It allows us to use wildcards such as % and _ to match various characters.
% Wildcard
Allows us to find strings that contain a string. May be used at the beginning of a string, at the end, or both. Used with LIKE.
_ Wildcard
Allows us to find strings that start or end with a string. ‘roy’ and ‘Tro’ both return Troy. Used with LIKE.
COUNT()
COUNT is probably the most widely-used aggregate function. As the name suggests, it counts things! For instance, COUNT() returns the number of non-null rows in the column.
COUNT(*) counts rows as long as any one of their columns is non-null.
NULL
In SQL, NULL is the value of an empty entry. This is different from the empty string ‘ ‘ and the integer 0, both of which are not considered NULL. To check if an entry is NULL, use IS and IS NOT instead of = and !=.
LIMIT
Limits the number of results returned.
LIMIT
CASE WHEN block
Acts as an if/else statement.
CASE WHEN THEN WHEN THEN ... ELSE END
MIN()
The MIN() function returns the smallest value of the selected column.
MIN(column_name)
MAX()
The MAX() function returns the largest value of the selected column.
MAX(column_name)
AVG()
The AVG() function returns the average value of a numeric column.
AVG(column_name)