Common Commands Flashcards

(23 cards)

1
Q

HAVING

where does it fall in relation to a GROUP BY clause?

A

used to filter groups with aggregate functions; used after GROUP BY clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

WHERE

where does it fall in relation to a GROUP BY clause?

A

filter rows on a condition, applying the filter to every row; used before a GROUP BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

CROSS JOIN

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

FETCH

A

SQL-standard for the LIMIT clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

LIMIT

A

limits the number of rows returned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

INTERSECT

definition
(list the conditions required to perform the query)

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

EXCEPT

definition
(list the conditions required to perform the query)

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

UNION

definition
(list the conditions required to perform the query)

A

combines result sets of multiple queries;

must have the same columns

SELECT column1, column2
FROM table1
UNION
SELECT column1, column2
FROM table2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

UNION ALL

what are the conditions of to be able to use a UNION ALL?

A

UNION query except it does not remove duplicate rows; must have the same columns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ROLLUP

(definition & when is it often used)

(where is it found in relation to the GROUP BY clause?)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

EXTRACT

A

extracts a portion from a date attribute;
example syntax:
EXTRACT (YEAR FROM rental_date)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

CASE

A

IF/ELSE statement for SQL

CASE
WHEN condition1 THEN result_1
ELSE
END

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

MINUS

definition
(list the conditions required to perform the query)

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

COMMIT

A

SQL transaction command used to save changes invoked by a transaction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

ROLLBACK

A

SQL transaction command used to undo transactions that have not been saved to the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

SAVEPOINT

A

SQL transaction command that creates a point to rollback transactions without rolling back the entire transaction

17
Q

SET/BEGIN

A

SQL transaction command that initiates a transaction

18
Q

COALESCE

equivalents in ORACLE & MySQL

A

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, …);

19
Q

CAST

A

convert one data type into another

20
Q

EXIST

A

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;
21
Q

OVER(PARTITION BY …)

A

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

22
Q

COUNT(*)

definition
(when is it used?)

A

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

23
Q

IFNULL

what are it’s equivalent’s?

A

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.