Week 4 - Further SQL Flashcards
What are aggregate functions?
Functions which act on a set of rows/tuples along a chosen column.
List aggregate function keywords in SQL
COUNT, SUM, AVG, MAX, MIN
What do aggregate functions do by default?
They work on all rows returned by SELECT query.
Can the DISTINCT keyword be used in the argument of aggregate functions?
Yes
mysql> select COUNT(distinct DEPTNO) from EMP;
SELECT * FROM zoo
WHERE animal LIKE ‘%e%’
What does this mean?
Select all rows in the zoo table where the value held in the animal column contains the letter ‘e’.
SELECT * FROM zoo
WHERE animal LIKE ‘e%’
What does this mean?
Select all rows in the zoo table where the value held in the animal column starts with the letter ‘e’.
SELECT * FROM zoo
WHERE animal LIKE ‘%e’
What does this mean?
Select all rows in the zoo table where the value held in the animal column ends with the letter ‘e’.
SELECT * FROM zoo
WHERE animal LIKE ‘e_’
What does this mean?
Select all rows in the zoo table where the value held in the animal column starts with the letter ‘e’ and has a single character following it.
SELECT * FROM zoo
WHERE animal LIKE ‘z_b_a’
What does this mean?
Select all rows in the zoo table where the value held in the animal column starts with a z, has any single character, then has a b, then has any single character, then ends with an a.
SELECT * FROM zoo
WHERE animal LIKE ‘_____’ – 5 underscores
What does this mean?
Select all rows in the zoo table where the value held in the animal column has exactly 5 characters.
SELECT * FROM zoo
WHERE uniq_id = 1
OR uniq_id = 2
OR uniq_id = 3
OR uniq_id = 4
OR uniq_id = 5;
How can you condense this query?
Using IN.
SELECT * FROM zoo
WHERE uniq_id IN (1,2,3,4,5)
SELECT * FROM zoo
WHERE animal NOT LIKE ‘_____’ – 5 underscores
What does this do?
Returns all animals whose names are not 5 characters in length.
For example, it could return giraffe, but definitely not zebra.
How does aliasing work?
Use this after a column name:
AS alias_name
What does GROUP BY do?
Splits records into separate groups of unique values of a given column.
What does functionally dependent ensure in the context of GROUP BY?
It ensures additional columns have a unique and deterministic value within each group created by the GROUP BY clause.
What happens if a column is used that isn’t functionally dependent in GROUP BY?
The DBMS cannot determine which value to pick for the group. There is ambiguity.
What interesting thing occurs when adding a WHERE condition to an aggregate function query?
It causes rows to be removed before the aggregate function is applied.
What keyword can you use to select rows only after the aggregate function has been applied?
HAVING
Provide an example of using the HAVING keyword
SELECT F(attribute) FROM TABLE
WHERE condition_1 –filters out rows before aggregate
GROUP BY attribute
HAVING condition_2 –filters out rows after aggregate
What is the operation order of a general SQL query (logical processing order)
FROM/JOIN
WHERE
GROUP BY
HAVING
SELECT
DISTINCT
ORDER BY
What is the syntax order of a general SQL query?
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
What are the requirements of the SQL UNION operator?
EG:
SELECT * FROM customers
UNION
SELECT * FROM orders;
1) The queries must return the same number of columns.
2) The data types don’t have to match exactly but must be implicitly convertible (e.g. DATE can become VARCHAR, but BLOB cannot become INT)
3) By default, UNION removes duplicates (use UNION ALL to keep them).