PostgreSQL Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A

A Relational Database Management System (RDBMS) that uses the SQL language for querying and maintaining the database.

Alternative relational databases include Microsoft SQL Server, MySQL, SQLite, and Oracle.

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

Relational databases are the most widely used kind of database. Most relational databases are also driven by SQL.

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

By using the postgresql status command.
(sudo service postgresql status)

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

What is a database schema?

A

A collection of tables that define how the data in a relational database should be organized.

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

What is a table?

A

A list of rows each having the same set of attributes. (Also referred to as relations)

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

What is a row?

A

A row contains attributes (Also referred to as columns)

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

SQL is how a server can interact with a relational database by retrieving, creating, or manipulating data.

It is a declarative language meaning that developers describe the results they want and the programming environment finds a way to get those results versus saying what to do and how to do it (imperative language).

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

By using a ‘select’ clause followed by a comma separated list of column names in double quotes. Then a ‘from’ clause followed by a table name in double quotes.

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

By using a ‘where’ clause followed by a condition where there is a column name in double quotes, and operator, and the value in single quotes.

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

Formatting SQL makes the query easier to understand.

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

=, <, > and !=.

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

By using a ‘limit’ clause followed by a literal integer value.

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

By using a ‘select’ clause followed an asterisk (star).

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

By using a ‘order by’ clause followed by the column name and ‘desc’ clause (the order is ascending by default).

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

The ‘insert into’ statement followed by the table name in quotes and a comma separated list of column names in quotes surrounded by parentheses.

Then, the ‘values’ statement followed by a comma separated list of values in single quotes (except numbers) surrounded by parenthesis.

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 in the same order as the columns they belong to.

17
Q

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

A

By adding a comma separated list of tuples after the values keyword.

18
Q

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

A

By using the ‘returning’ clause followed by an asterisk (star).

19
Q

How do you update rows in a database table?

A

The ‘update’ statement followed by the table name in double quotes. Then, a ‘set’ keyword followed by a column name in double quotes, an operator, and a value in single quotes (except numbers).

20
Q

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

A

Not including a ‘where’ clause will update every row instead of specific rows.

21
Q

How do you delete rows from a database table?

A

The ‘delete from’ statement followed by the name of the table in quotes. Then, a ‘where’ clause followed by the column name is double quotes, an operator, and a value in single quotes (except numbers).

22
Q

How do you accidentally delete all rows from a table?

A

The ‘delete from’ statement followed by the table name in double quotes.

23
Q

What is a foreign key?

A

A column in a table that specifically refers to a column in another table. (A shared data value between tables)

(i.e., “products” have a “supplierId” column that refers to the “supplierId” column in “suppliers”)

24
Q

How do you join two SQL tables?

A

The ‘join’ clause followed by the table name in double quotes, the ‘using’ statement, and the foreign key in double quotes and parenthesis.

25
Q

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

A

The table name in double quotes followed by the ‘as’ statement, and temporary name in double quotes. (Known as aliasing)

This can be done after the ‘select’, ‘from’, and ‘join’ clauses.

26
Q

What are some examples of aggregate functions?

A

min(), max(), sum(), avg(), and count().

json_agg(“…”)

27
Q

What is the purpose of a group by clause?

A

To separate rows into groups and perform aggregate functions on the groups of rows.