PostgreSQL Flashcards

1
Q

What is PostgreSQL?

A

A free, open source Relational Database Management System. That provides robust features, meets standards compliance, and reliability.

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
  • To understand and solve problems based around storing and pulling up data.
  • To be able to reject bad data and not worry about half written data
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
  • Check to see if postgresql processes are running in top command
  • Type in sudo service postgresql status
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are some alternative relational databases?

A

MySQL, SQL Server by Microsoft, and Oracle by Oracle.

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

What is a database schema?

A

A collection of tables.

- Defines how data in a relational database is organized.

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

What is a table?

A

A relational database that stores data in relations.

- It is a list of rows each having the same set of attributes.

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

What is a row?

A

A list from a table that has the same attributes.

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

What is SQL?

A

Stands for Structured Query Language

  • Primary way of interacting with relational databases
  • Retrieves, creates, and manipulates data in a relational database
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

select keyword
comma-separated list of column names surrounded by “ “
from clause which specifies which table to get data
ends with a semicolon

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

Using the where clause

  • Comes after the from clause
  • Column headers will have double quotes while text values have single quotes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the benefits of formatting your SQL?

A

Creates a consistent style and therefore makes SQL easier to read

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

How is SQL different from languages like JavaScript?

A

SQL is a declarative programming language.

- Programmer describes the results they want and the program comes up with a plan to get those results

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

equals sign =
greater than >
less than <
not equal !=

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

Using the limit clause

  • This will come last in a select statement
  • Is a literal 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

Using an asterisk *

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

Include an order by clause

  • Comes after the from clause
  • Followed by a column name within “ “
  • Default order is ascending
17
Q

How do you add a row to a SQL table?

A

insert into “tableName” (“columnName”, “columnName”…etc)
values (‘valueName’, ‘valueName’… etc);

No need for id values because tables will automate that

18
Q

What is a tuple?

A

A list of values within SQL

19
Q

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

A

Have another line that contains a tuple

Use a comma after every subsequent tuple

20
Q

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

A

Use a returning clause

21
Q

How do you update rows in a database table?

A

update “tableName”
set “columnName” = value
where “rowId” = value;

22
Q

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

A

If a where statement isn’t included then it will update the whole table as opposed to the targeted row

23
Q

How do you delete rows from a database table?

A

delete from “tableName”
where “id” = value
returning *;

returning clause is optional

24
Q

How do you accidentally delete all rows from a table?

A

delete from “products”;

Not including a where clause…

25
Q

How do you update multiple columns in a row?

A

update “tableName”
set “columnName” = value,
“columnName” = value
where “id” = value;

26
Q

How do you delete multiple rows in SQL?

A

delete from “tableName”
where “columnName” = value
and “columnName” = value

27
Q

What safeguard do you use to protect data when making changes?

A

Do a transaction:
Use begin command on the sql file you want to do changes

To continue:
Use rollback command to go back
Commit command to commit

28
Q

What is a foreign key?

A

An id that links one table to another table

29
Q

How do you join two SQL tables?

A

select *
from “tableName”
join “tableName2” using (“tableName2Id”);

30
Q

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

A

Alias the column names by doing:

select “tableName”.”columnName” as “name”,
“tableName2”.”columnName2” as “name2”
from “tableName”
join “tableName2” using (“tableName2Id”);

31
Q

What are some examples of aggregate functions?

A
max( )
avg( )
count( )
min( )
sum( )
every( )
32
Q

What is the purpose of a group by clause?

A

To subdivide rows in a subset into groups and performs the aggregate in each group

To get aggregates from groups of data as opposed to the data in its entirety

33
Q

When would you use an aggregate function?

A

After the select clause

Example:
select avg(“columnName”) as “columnAlias”
from “tableName”;