SQL Commands and Functions Flashcards
(39 cards)
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)
SUM()
The SUM() function returns the total sum of a numeric column.
SUM(column_name)
LENGTH()
For a string value str, the length(str) function returns the number of characters (not bytes) in str prior to the first NUL character.
LENGTH(string)
SELECT DISTINCT
The SELECT DISTINCT statement is used to return only distinct (different) values.
SELECT DISTINCT column1, column2, …
GROUP BY
It allows us to split up the dataset and apply aggregate functions within each group, resulting in one row per group. Its most basic form is GROUP BY , , … and comes after the WHERE block.
AS
AKA Aliasing. In the SELECT block, AS provides an alias that can be referred to later in the query. This saves us from rewriting long expressions, and allows us to clarify the purpose of the expression.
ROUND()
The ROUND() function rounds a number to a specified number of decimal places.
ROUND(number, decimals, operation)
HAVING
Like WHERE but can be used on aggregate functions. You can think of it as a post-aggregation WHERE block. Modifies GROUP_BY.
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
JOIN
The JOIN block takes the form of JOIN ON . The clause works the same way as in WHERE . That is, it is a statement that evaluates to true or false, and anytime a row from the first table and another from the second line up with the clause being true, the two are matched
By default this is an INNER JOIN which drops unmatched rows.
LEFT JOIN
LEFT JOIN ON
Preserves unmatched rows in the left table.