PostgreSQL Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A

PostgreSQL is a free open source relational database management system

Alternative relational databases include: MySQL (also free), SQL Server by Microsoft, and Oracle by Oracle Corporation.

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
  1. If you are storing related data, then a relational database is a good choice.
  2. Relational databases support good guarantees about data integrity.
  3. Relational databases are arguably the most widely used kind of database.
  4. And knowing the SQL language is a very portable skill given that there are so many relational databases driven by almost the same language (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
  1. Use the terminal command sudo service postgresql status.
  2. Check use the terminal top command to see if postgres is running
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 is called a schema. A schema defines 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 table is a set of relational data. A table is a list of rows, the rows all have the same attributes

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

What is a row?

A

A row is a single instance of a record in a spreadsheet/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 (SQL) is the primary way of interacting with relational databases. It is a powerful way of retrieving, creating, and manipulating data in a relational database.

JavaScript is an imperative programming language, where you basically tell the JavaScript runtime what to do and how to do it.

SQL is a declarative programming language where programmers describe the results they want and the programming environment comes up with its own plan for getting those results. (like html & css)

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

Start with the select keyword.

The select keyword is followed by a comma-separated list of column names, each surrounded by “double quotes”.

e.g: select “firstName”,
“lastName”

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

Use the where keyword followed by the column you want to filter by in “double quotes”, followed by an optional comparison operator, followed by an optional value of that column that you want to see in ‘single quotes’ (if the value is a number, do not use quotes at all.)

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

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

A

Use the limit keyword followed by the number of rows you want to see.

e.g: limit 10;

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

How do you retrieve all columns from a database table?

A

Use the select keyword followed by *

e.g: select *

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

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

A

Use the order by keyword followed by the column you want to order by in “double quotes.” order will be in ascending order by default - add desc keyword after “specified column” to sort in descending order.

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

Use the order by keyword followed by the column you want to order by in “double quotes.” order will be in ascending order by default - add desc keyword after “specified column” to sort in descending order.

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 into “table” (“columnName”, “columnName”,… )
values (‘value’, ‘value’, ‘value’, …);

values are in the same order as the columns they belong to

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

17
Q

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

A

Multiple data rows can be inserted into a database table at once by specifying more than one tuple of values, separated by commas.

18
Q

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

A

after the insert into statement and the values statement, follow with
returning clause.

e.g:
insert into “products” (“name”, “description”, “price”, “category”)

values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’)

returning *;

19
Q

How do you update rows in a database table?

A

use the update keyword followed by the “table”
then use the set keyword followed by the “column” or “columns”, “columns” you want to update.
Follow with = ‘value’

20
Q

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

A

Unless you intend on updating all rows, include a where clause in your update statement to only target specific rows

21
Q

How do you delete rows from a database table?

A

Use SQL delete statement to remove rows from tables

22
Q

How do you accidentally delete all rows from a table?

A

delete from “table”;

since there is no where statement targeting specific rows - all rows are deleted (THERE IS NO UNDO!)

23
Q

What is a foreign key?

A

A column whose values refer to the values in a column on another table

A column that links one table to another

24
Q

How do you join two SQL tables?

A

Using the join keyword followed by the referenced table, followed by the using keyword followed by the connecting column

e.g:
join “otherTable” using (“columnAttribute”);

25
Q

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

A

Use the as keyword. (column is renamed just for the result.

e.g:
select “products”.”name” as “product”,
“suppliers”.”name” as “supplier”
from “products”
join “suppliers” using (“supplierId”);

26
Q

What are some examples of aggregate functions?

A

max( ), avg( ), count( ), min( ), sum( ), every( ), json functions are also available - for more: https://www.postgresql.org/docs/10/functions-aggregate.html

27
Q

What is the purpose of a group by clause?

A

To separate rows into groups and perform aggregate functions on those groups of rows