Databases Flashcards

1
Q

What is PostgreSQL and what are some alternative relational databases?

A

PostgreSQL is a powerful, free, open source Relational Database Management System (RDBMS).

Alternative relational databases: MySQL, SQL Server by Microsoft, Oracle by Oracle Corporation, SQLite

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
  • provide good guarantees about data integrity
  • store and modify data in a way that makes data corruption as unlikely as possible.
  • knowing SQL language
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

On terminal –
top command or
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.

- 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
  • relational databases store data in relations.

- A table 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
6
Q

What is a row?

A

a row is a collection of fields that make up a record / values 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 (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.

SQL is different from language like JS because it is a declarative programming language. In declarative languages, 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

you use the select statement and double quotes the column’s name. If you want multiple column, use a comma.

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 clause with the comparison operators

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

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, greater than, equal to (=), not equal to ( != )

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

at the end of the code block and input “limit” followed by the 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 all of the columns in a table by replacing the list of column names with an * asterisk

ex 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

use the order by clause followed by the “name” of column and the keyword (desc for descending; ascending order is default).

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

Select statement notes:

A
  • The query starts with the select keyword.
  • The select keyword is followed by a comma-separated list of column names, each surrounded by “ double quotes.
  • The column names are followed by a from clause specifying which table to retrieve the data from.
  • The query must end in a ; semicolon.
  • SQL keywords such as select and from are not case-sensitive.
  • SQL does not have to be indented, but you should do it anyway for consistent style and therefore readability.

Example: select “name”, “price” from “products”;

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

Sorting notes:

A
  • The order by clause comes after the from clause.
  • The order by clause is followed by a column name in “ double quotes.
  • The default sort order of the results is ascending order.

Example: select *
from “products”
order by “price”;

17
Q

Filtering notes:

A
  • The where clause comes after the from clause.
  • The where clause is checking the “category” of each row in the table.
  • Text values, such as ‘cleaning’ are wrapped in ‘ single quotes; not double quotes!
  • The value of the “category” column is being compared using a single = equals sign. (Other comparisons like , and != are available too).
  • Don’t forget: unless the select statement includes an order by clause, the order of the result set is not guaranteed.
Example: select "productId",
       "name",
       "price"
  from "products"
 where "category" = 'cleaning';
18
Q

How do you add a row to a SQL table?

A

Insert statement

Example - “product” table:

insert into “products” (“name”, “description”, “price”, “category”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’)

19
Q

What is a tuple?

A

a list of values (wrapped in parenthesis)

20
Q

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

A

Using multiple tuples separated by commas.

Example - “product” table:
insert into “products” (“name”, “description”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’),
(‘Tater Mitts’, ‘Scrub some taters!’)
returning *;

21
Q

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

A

returning clause

returning * or the returning attributes

22
Q

Insert notes:

A
  • The statement begins with the insert keyword.
  • The table to insert into is specified in “ double quotes.
  • The list of columns being inserted is wrapped in () parenthesis.
  • The values being inserted are also wrapped in () in parenthesis in the same order as the columns they belong to. In SQL, a list of values is referred to as a tuple.
  • Text values are wrapped in ‘ single quotes.
  • Numeric values are represented with literal numbers (or decimals if applicable).
  • If an id is left out, the tables are often configured to auto-generate identifier attributes to avoid accidental duplicates.
23
Q

How do you update rows in a database table?

A

Update statement

Example - “product” table:
update “products”
set “price” = 100
where “productId” = 24;

24
Q

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

A

So that it don’t update all the rows of the column. The where clause will target specific rows

25
Q

How do you delete rows from a database table?

A

delete from statement and where clause

Example - “product” table:
delete from “products”
where “category” = ‘cleaning’
and “price” < 20;

26
Q

How do you accidentally delete all rows from a table?

A

By not specifying which rows with the “where” clause

27
Q

What is a foreign key?

A

one column word/key that links two separate tables (column found in both tables)

28
Q

How do you join two SQL tables?

A

Use “select”, then the column name in double-quotations and commas after if need multiple columns. Then, use the “from” clause to indicate which table name in double quotations. Next use the “join” clause followed by table name that you want to join in double quotations and then use the using keyword and (foreign key) with double quotes inside parentheses.

select *
from “products”
join “suppliers” using (“supplierId”);

29
Q

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

A

use the “as” keyword to rename;

example:
“products” as “p” for renaming tables
“product”.”name” as “product” for columns (the .”name” is the original name of column).

30
Q

What are some examples of aggregate functions?

A

count( ), sum( ), avg( ), max( )

31
Q

What is the purpose of a group by clause?

A

to separate rows intro groups and perform aggregate functions on those groups of rows