Common Commands Flashcards
(23 cards)
HAVING
where does it fall in relation to a GROUP BY clause?
used to filter groups with aggregate functions; used after GROUP BY clause
WHERE
where does it fall in relation to a GROUP BY clause?
filter rows on a condition, applying the filter to every row; used before a GROUP BY
CROSS JOIN
cartesian product of rows between two or more tables; there is no matching condition in the join clause; all columns from T1 and T2 will be present including a cartesian product of rows
FETCH
SQL-standard for the LIMIT clause
LIMIT
limits the number of rows returned
INTERSECT
definition
(list the conditions required to perform the query)
combines the results of two or more SELECT clauses;
only shows results that match both SELECT statements;
must have the same number of columns and the columns need to be of the same data type;
SELECT column1, column2 FROM table1 INTERSECT SELECT column1, column2 FROM table2
EXCEPT
definition
(list the conditions required to perform the query)
returns rows in query1 that do not appear in query2;
must have the same number of columns and the columns need to be of the same data type;
SELECT column1, column2 FROM table1 EXCEPT SELECT column1, column2 FROM table2
UNION
definition
(list the conditions required to perform the query)
combines result sets of multiple queries;
must have the same columns
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2
UNION ALL
what are the conditions of to be able to use a UNION ALL?
UNION query except it does not remove duplicate rows; must have the same columns
ROLLUP
(definition & when is it often used)
(where is it found in relation to the GROUP BY clause?)
shorthand for defining multiple grouping sets; often used to create subtotals and grand totals for reports
postgresql syntax:
GROUP BY
ROLLUP (column1, column2)
MySQL syntax:
GROUP BY column1, column2 WITH ROLLUP
EXTRACT
extracts a portion from a date attribute;
example syntax:
EXTRACT (YEAR FROM rental_date)
CASE
IF/ELSE statement for SQL
CASE
WHEN condition1 THEN result_1
ELSE
END
MINUS
definition
(list the conditions required to perform the query)
MySQL & ORACLE EXCEPT clause;
returns rows in query1 that do not appear in query2;
must have the same number of columns and the columns need to be of the same data type;
SELECT column1, column2 FROM table1 MINUS SELECT column1, column2 FROM table2
COMMIT
SQL transaction command used to save changes invoked by a transaction
ROLLBACK
SQL transaction command used to undo transactions that have not been saved to the database
SAVEPOINT
SQL transaction command that creates a point to rollback transactions without rolling back the entire transaction
SET/BEGIN
SQL transaction command that initiates a transaction
COALESCE
equivalents in ORACLE & MySQL
returns the first non-null argument from an unlimited number of arguments; coverts a NULL value into a user defined value
equivalent to NVL (ORACLE) or IFNULL (MySQL) - verified this was correct
COALESCE (argument_1, argument_2, …);
CAST
convert one data type into another
EXIST
test for the existence of rows in a subquery; the results of the EXISTS depends on whether any row is returned and not the content of the row
often used in correlated subqueries
example: customers who’ve paid greater than $11 dollars to rent a dvd
SELECT first_name, last_name FROM customer c WHERE EXISTS (SELECT 1 FROM payment p WHERE p.customer_id = c.customer_id AND amount > 11 ) ORDER BY first_name, last_name;
OVER(PARTITION BY …)
invokes a window function, which allows for aggregations over distinct rows in a SELECT statement unlike the GROUP BY function with requires aggregation over all selected rows
SELECT column1, column2,
COUNT(*) OVER(PARTITION BY column2) as cnt
FROM emp
ORDER BY 2
COUNT(*)
definition
(when is it used?)
counts the rows instead of the column value values in a SELECT statement; helpful when trying to count the number of rows with a NULL value
IFNULL
what are it’s equivalent’s?
Return a specified value if the expression is NULL, otherwise returns the expression.
example: SELECT IFNULL(“Hello”, “W3Schools.com”) would return Hello, since it is not NULL.
Similar to NVL (ORACLE) and COALESCE.