SQL Flashcards

1
Q

Replace Value with a default when NULL

A

coalesce(column, default) as column

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

Select and deduplicate results

A

SELECT DISTINCT * FROM table

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

How to concatenate items

A

||

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

How to alias a column

A

SELECT column AS alias

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

Sort by name descending with nulls last

A

ORDER BY name DESC NULLS LAST

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

Start postgres console

A

psql -p 5555

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

How to import rows from a text file

A

COPY

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

Difference between WHERE and HAVING

A

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.

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

What is a foreign key

A

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.

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

What does Postgres use to read a table when there is no index

A

Sequential scan

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

What is the problem with OFFSET

A

Performance issues as it gets larger because the database needs to count the rows

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

What is a having clause for

A

Filtering aggregates such as GROUP BY

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

What does NULL indicate?

A

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

How can we cache results in SQL

A

Materialized views

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

How to test SQL without committing to DB (so can iterate).

A

BEGIN;

(Code)

ROLLBACK;

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

What should we use in where statements to compare against true, false, and null?

A

IS ( not = )

17
Q

How to get the year from a datetime column

A

extract(year from datetime) as year

18
Q

How to count only a subset

A

count(*) filter(where column = ‘banana’) as bananas

19
Q

How do we embed an aggregate value column of all the results along with a list of the results?

A

Window function (OVER)

20
Q

What are directories called in SQL, and what is the default one called?

A

Schemas, public

21
Q

How to get info back after inserting / updating / deleting without a new query

A

RETURNING

22
Q

Difference between ON and WHERE in a join

A

ON is computed before the join, WHERE is computed after

23
Q

If using LIMIT and OFFSET, what else must be used

A

ORDER BY, as otherwise a random result will be returned