PostgresQL Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A

It’s a relational database. Some other relational databases are MySQL, Oracle, and SQLite

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

Important commands for starting, stopping and checking status for PostgreSQL

A

Sudo service postgreSQL (start / stop / status)

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

Is PostgreSQL its own application?

A

Yes! We just interact with it. No building or debugging —> we send it a request and it sends a response.

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

What is pgweb?

A

Not our code —> just code somebody else wrote to help us visualize our database.

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

What are some advantages of learning a relational database?

A

It’s used literally everywhere. It also allows you to store your data somewhere secure.

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

What is one way to see if PostgreSQL is running?

A

Sudo service postgresql status

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

What is a database schema?

A

A collection of tables — the design of a database —> like a blueprint

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

What is SQL and how is it different from languages like JavaScript?

A

It’s the primary way of interacting with relational databases —> a declarative programming language —> programmers describe the results they want the the end gets the result

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

How do you retrieve specific columns from a database table?

A

Use a select statement formatted as…

select “desired-column”,
“another-column”
from “table-name”;

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

How do you filter rows based on some specific criteria?

A

Use a where clause formatted as…

where “column-name” = ‘text-to-query’;

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

If you are adding a number, bool, etc, should you keep the quotes?

A

Nope — remove them!

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

What are the benefits of formatting your SQL?

A

It makes it easier to read, understand, and debug.

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

What are four comparison operators that can be used in a where clause?

A

=, !=, <, >

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

How do you limit the number of rows returned in a result set?

A

Limit x(integer);

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

How do you retrieve all columns from a database table?

A

Use a select statement formatted as…

select *
from “table-name”;

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

How do you control the sort order of a result set?

A

Order by “column-to-sort-by”;

17
Q

Default order is ascending or descending?

A

Ascending order. (Smallest to largest)

18
Q

How do you add a row to a SQL table?

A

Use an insert statement.
INSERT INTO “table-name” (“column1”, “column2”)
VALUES (‘value1’, ‘value2’)

19
Q

What is a tuple?

A

An ordered set of values typically wrapped in parenthesis

20
Q

How do you add multiple rows to a SQL table at once?

A

Specify more than one set of tuples seperated by commas (they’ll both be returned if you use a returning statement)

21
Q

How do you get back the row being inserted into a table with a separate select statement?

A

Add a returning clause after the values clause

22
Q

How do you update rows in a database table?

A

Use an sql update statement
UPDATE “table-to-update”
SET “column-to-update” = ‘value-to-update-with’
WHERE “column-name” = ‘value’

23
Q

Why is it important to include a where clause in your update statements?

A

If you don’t include one, you’ll overwrite all data in that column (very bad yikes RIP)

24
Q

How do you delete rows from a database table?

A

Use “delete”
DELETE
FROM “table-name”
WHERE “column-name” = ‘value’
RETURNING *

25
Q

How do you accidentally delete all rows from a table?

A

You leave off the where statement (big oops)

26
Q

What is a foreign key?

A

A constraint column with data that refers to values in another table that links those tables together

27
Q

How do you join two SQL tables?

A

SELECT *
FROM “table-name”
JOIN “table-to-join” using (“column-to-join-by”)

28
Q

What is JOIN ON?

A

Same as join, but give specific IDs to join on.

JOIN “table2” on “table1”.“table1ID” = “table2”.“table2ID”

29
Q

How do you temporarily rename columns or tables in a SQL statement?

A

SELECT “p”.“name” as “product”,
“p”.“category”,
“s”.“name” as “supplier,
FROM “products” as “p”
JOIN “suppliers” as “s” using (“supplierId”);

30
Q

What are some examples of aggregate functions?

A

Max(), Avg(), Count(), Min(), Sum(), Every()

31
Q

What is the purpose of a group by clause?

A

Performing aggregate functions only on a specific group of rows