PostgreSQL & SQL Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A

Relational Database Management System (RDBMS).

MySQL, 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

Good for storing related data.

They support good guarantees about data integrity. They can store and modify data in a way that makes data corruption as unlikely as possible. This means that developers can set up their database to reject “bad” data and not worry about data being “half written”.

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

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; 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

Stored data in relations; a list of rows each having the same set of attributes; Attributes are commonly referred to as columns.

sort of spreadsheet

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

What is a row?

A

Data entries with the same set of attributes.

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); primary way of interacting with relational databases. It is a powerful way of retrieving, creating, and manipulating data in a relational database;

SQL is a declarative programming language, programmers describe the results they want and the programming environment comes up with its own plan for getting those results.

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 “column-name”,
“column-name”
from “table-name”;

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

where “column-name” = ‘column-value’;

string using single quotes ‘column-value’; number just use number

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

for consistent style and therefore 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

!=, =, ‘>’, ‘less-than’

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 ‘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

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 “price” desc

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-name” (“column-name”)

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 SQL

17
Q

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

A

insert into “table-name” (“column1”, “column2”,
“column3”, “column4”)
values (‘value1, ‘value2’, 99, ‘value4’);

18
Q

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

A

returning *

returning ‘column-name1’, ‘column-name2’

19
Q

How do you update rows in a database table?

A

update “table-name”
set “column-name” = new-value
[where “column-name” = column-value;]

20
Q

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

A

Without ‘where’, you will update all entries in the table.

21
Q

How do you delete rows from a database table?

A

delete from “table-name”
where “column-name” = column-value
[returning *];

delete from “table-name”
where “column-name1” = ‘column-value’
and “column-name2” < column-value

22
Q

How do you accidentally delete all rows from a table?

A

delete from “table-name”;

leaving out the ‘where’ statement

23
Q

What is a foreign key?

A

a column that links 2 different tables (same column names in 2 different tables)

24
Q

How do you join two SQL tables?

A

select *
from “table-name1”
join “table-name2” using (“foreign-key”);

select “table-name1”.”column-name1”,
“table-name2”.”column-name2”
from “table-name1”
join “table-name2” using (“foreign-key”);

25
Q

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

A

columns:

select “table1”.”column” as “alias1”,
“table2”.”column” as “alias2”
from “table1”
join “table2” using (“foreign-key”);

tables:

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

What are some examples of aggregate functions?

A

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

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.