PostgreSQL Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A

PostgreSQL is a relational database
Alternatives: Redis, MongoDB, Neo4j

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

What are some advantages of learning a relational database?

A

SQL (structured query language) language/jargon is used in many relational databases, so skills are transferable

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

What is one way to see if PostgreSQL is running?

A

Sudo service postgresql status
Sudo elevates you to administrative privileges
Echo $(whoami) shows the current user name
**usually asks for password
Service
Managing a background process
Postgresql status
Status tells you if it’s running or not
Start will start the program
Stop will stop the program

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

What is a database schema?

A

Collection of tables
Rules of how the data can be stored
The structure of the database
If there’s no schema, usually in non-relational databases

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

What is a table?

A

List of rows with the same of attributes(columns)

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

What is a row?

A

Collection of attributes for a single row in a table

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

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

A

“Structured Query Language” is a declarative programming language, where programmers describe the results they want and the programming environment comes up with its own plan for getting said results. (like HTML and CSS)
JavaScript is imperative programming language and we have to tell it exactly what to do, step by step

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

How do you retrieve specific columns from a database table?

A

Select “insert-column-name-here” (multiple, separated by commas)
From “insert-database-table-name-here”

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

How do you filter rows based on some specific criteria?

A

Specify value from column
Where “insert-column-name” = ‘insert-value-name’;

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

What are the benefits of formatting your SQL?

A

Easier readability

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

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

A

Limit how many results you want
Limit [insert-number]

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

How do you retrieve all columns from a database table?

A

Use * [asterisk for all]
Select *

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

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

A

Order by “insert-name-of-what-to-order-by” desc
Ascending by default
Desc for greatest to least

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

How do you add a row to a SQL table?

A

Insert keyword
Insert into “table-data-name” (“column-name”, “more-column-names”)
Values (‘value of first column-name’, ‘value of second column-name’)
Returning * [optional, but shows immediately what you inserted]

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

What is a tuple?

A

A list of values (the ones written inside the parenthesis with single quotes, separated by commas)

17
Q

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

A

Write multiple () separated by comma after Values
Values (‘content’, ‘content’),
(‘more-content’, ‘more-content’)

18
Q

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

A

Returning * [optional, but shows immediately what you inserted]

19
Q

How do you update rows in a database table?

A

Update keyword
Update “table-database-name”
Set “column-name” = ‘new-value’
Where “which-column” = ‘which-value-to-change’

20
Q

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

A

If not, it’ll update the entire column that was selected/chosen/written

21
Q

How do you delete rows from a database table?

A

Delete from “table-database-name”
Where “column-name” = ‘value to be deleted’
returning *;

22
Q

How do you accidentally delete all rows from a table?

A

Delete from “table-database-name”;
Accidentally deletes ALL rows!

23
Q

What is a foreign key?

A

It’s a shared data value that is a placeholder for its fullest amount of detail, which is located elsewhere
Ie. the supplierId in the products table is the number to look for in the suppliers table to find the rest of the information about that supplier

24
Q

How do you join two SQL tables?

A

Join keyword
Join “table-database-name” using (“column-to-combine-with”);
Can use Join “table-database-name”

25
Q

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

A

When selecting, you can write as “whatever-rename-name”
Select “table-database-name”.”column-name-in-database” as “new-temp-name-in-new-table”
Doesn’t rename in the original table, but only renames the column name in the result set

26
Q

What are some examples of aggregate functions?

A

Min, sum, count, max,

27
Q

What is the purpose of a group by clause?

A

Partition by the part we don’t want duplicated
After getting large table with duplicate info, the group by clause will collapse all the duplicates so the results will only show 1x each
Perform aggregate functions on the groups of rows