Jed Rembold
Monday, October 7, 2024
All of the following statements about SQL indexes are true except for which?
Suppose you wanted to track your Spotify playlist information in your own database. Questions you may want to be able to answer might include:
In groups of two or three, sketch out ER diagrams that would allow all these questions to be answered. Your tables should include primary keys and foreign keys where appropriate, and be normalized as best as you are able.
Online sketching resources include: drawsql, visual-paradigm, or DrawIO
It is frequently the case that values in a particular table column all belong to a smaller subset of categories or options
With current methods, if you want to compare some sort of aggregate between those categories or options, you need to do it in multiple queries:
SELECT AVG(age)
FROM voters
WHERE party = 'D'
SELECT AVG(age)
FROM voters
WHERE party = 'R'
This rapidly becomes intractable if you want to compare across many categories
GROUP BY
commandGROUP BY
gathers together all rows with
matching values from a particular column
DISTINCT
on a columnSELECT |||grouped_column|||
FROM |||table|||
GROUP BY |||grouped_column|||;
DISTINCT
GROUP BY
is to be
able to run aggregates across all potential groups
simultaneously for comparisonSELECT
|||col_name|||,
min(|||col_name|||),
avg(|||col_name2|||)
FROM |||table|||
GROUP BY |||col_name|||;
Looking back at the cereal table, how can we answer:
DISTINCT
or
UNIQUE
, you can group by multiple
columnsSELECT
|||col_name|||,
min(|||col_name|||),
avg(|||col_name2|||)
FROM |||table|||
GROUP BY |||col_name|||, |||col_name2|||;
GROUP BY
can act on whatever
our current table structure is, so can also work seamlessly on joined
tablesSELECT
t1.|||col_name|||,
COUNT(t2.|||col_name3|||),
FROM table_name1 AS t1
JOIN table_name2 AS t2
ON t1.|||col_name||| = t2.|||col_name2|||
GROUP BY t1.|||col_name|||;
WHERE
WHERE
actions happen before any
aggregates can be computedHAVING
HAVING
filters take place
after groups and aggregates can be computedSELECT
|||col_name|||,
min(|||col_name|||),
avg(|||col_name2|||)
FROM |||table|||
GROUP BY |||col_name|||
HAVING min(|||col_name|||) > 50;