SQL Flashcards
Replace Value with a default when NULL
coalesce(column, default) as column
Select and deduplicate results
SELECT DISTINCT * FROM table
How to concatenate items
||
How to alias a column
SELECT column AS alias
Sort by name descending with nulls last
ORDER BY name DESC NULLS LAST
Start postgres console
psql -p 5555
How to import rows from a text file
COPY
Difference between WHERE and HAVING
WHERE is computed before retrieving rows and cannot have aggregates
HAVING is run after rows are retrieved and is used for filtering based on aggregates.
What is a foreign key
A foreign key references a column on another table, and that other column is checked for the existence of the key upon insert of a record in the first table.
What does Postgres use to read a table when there is no index
Sequential scan
What is the problem with OFFSET
Performance issues as it gets larger because the database needs to count the rows
What is a having clause for
Filtering aggregates such as GROUP BY
What does NULL indicate?
Unknown value, it does not mean false.
That is why NULL = NULL returns NULL rather than false, and we need to use IS NOT NULL instead.
How can we cache results in SQL
Materialized views
How to test SQL without committing to DB (so can iterate).
BEGIN;
(Code)
ROLLBACK;