PostgreSQL Flashcards

1
Q

[Postgres Intro]

What is PostgreSQL and what are some alternative relational databases?

A

PostgreSQL - relational database

MySQL, SQL Server by Microsoft, Oracle

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

[Postgres Intro]

What are some advantages of learning a relational database?

A

They are very common, knowing one will translate well to others

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

[Postgres Intro]

What is one way to see if PostgreSQL is running?

A

$ sudo service postgresql status

with service, they won’t take up your terminal (they run in the background)

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

What does SQL mean?

A

Structured Query Language

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

[Postgres Database]

What is a database schema?

A

A collection of tables which define how the data in a database should be organized

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

[Postgres Database]

What is a table?

A

Rows with the same set of attributes

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

[Postgres Database]

What is a row?

A

One entry with one value of the data

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

[Postgres Database]
What command is used to create a database?
What command is used to delete a database?

A

$ createdb databasename

$ dropdb databasename

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

[SQL Select]

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

A

SQL is a declarative language where JavaScript is imperative language.

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

[SQL Select]

How do you retrieve specific columns from a database table?

A

select (keyword) “col title”

from (clause) “table name”

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

[SQL Select]

How do you filter rows based on some specific criteria?

A

where (clause) “category” comparison operator ‘value’

which returns true or false

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

[SQL Select]

What are the benefits of formatting your SQL?

A

Makes it easier to read the information

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

[SQL Select]

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

[SQL Select]

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

A

limit num;

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

[SQL Select]

How do you retrieve all columns from a database table?

A

select (keyword) *

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

[SQL Select]

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

A

order by “category” desc/asc

17
Q

[SQL Insert]

How do you add a row to a SQL table?

A

insert into “table name” (“cat1”, “cat2”, “cat3”, “cat4”)

values (“val1”, “val2”, “val3”, “val4”)

18
Q

[SQL Insert]

What is a tuple?

A

a list of values

19
Q

[SQL Insert]

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

A

write multiple tuples separated by commas

20
Q

[SQL Insert]

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

A

returning *;

21
Q

[SQL Update]

How do you update rows in a database table?

A

update “table”
set “category name” = ‘value’
where “category” = ‘original value’;

22
Q

[SQL Update]

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

A

If omitted, will update the value of every row

23
Q

[SQL Delete]

How do you delete rows from a database table?

A

delete from “table name”
where “category” = ‘value’
returning *;

24
Q

[SQL Delete]

How do you accidentally delete all rows from a table?

A

delete from “table name”;

[not including ‘where’ clause]

25
Q

How to make changes in a copy instead of the actual database?

A

being -> make changes

commit or rollback

26
Q

[SQL Join]

What is a foreign key?

A

Shared value in columns between two tables

27
Q

[SQL Join]

How do you join two SQL tables?

A

select *
from “table 1”
join “table 2” using (“foreign key”);

28
Q

[SQL Join]

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

A

select “table 1”.”original col name” as “temp col name”,
“table 2”.”original col name” as “temp col name 2”
from”table1 “
join”table 2” using (“foreign key”);

29
Q

How do you also change the name of a table? Why would this be helpful?

A

To make it easier to type out the table name if it is repeated

select "p"."name" as "product",
       "p"."category",
       "s"."name" as "supplier",
       "s"."state"
  from "products" as "p"
  join "suppliers" as "s" using ("supplierId");
30
Q

[SQL Aggregates]

What are some examples of aggregate functions?

A

count()
average()
sum()

31
Q

[SQL Aggregates]

What is the purpose of a ‘group by’ clause?

A