SQL 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). Other popular 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

A quality of many relational databases is that 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”.

They are very widely used

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

Using the top command in one of your terminals to list the currently running processes

You can also check the status of the postgresql service:
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 is called a schema. A schema defines how the data in a relational database should be organized. In relational databases, you typically have to define your schema up front and the database server will make sure that any data being written to the database conforms to that schema.

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 list of rows each having the same set of attributes. For example, all students in a “students” table could have “firstName”, “lastName”, and “dateOfBirth” attributes. Attributes are commonly referred to as columns. You might visualize a database table as a sort of spreadsheet where each row is a record in that spreadsheet.

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

What is a row?

A

The row is where actual data is stored

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) is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS). It is particularly useful in handling structured data, i.e. data incorporating relations among entities and variables.

JavaScript is an imperative programming language, where you basically tell the JS runtime what to do and how to do it. Whereas, 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 (like HTML and CSS). The programmer simply “declares” their intent and the Web browser does its best to output the desired results according to a predefined set of rules.

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

Querying the data
- 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.

i.e.

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

It is possible to select all of the columns in a table by replacing the list of column names with an * asterisk.

select *
from “products”;

  • The order of the results is not predictable.
  • The * asterisk is not in 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

This is done using a where clause.

select “productId”,
“name”,
“price”
from “products”
where “category” = ‘cleaning’;

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

Database tables in large systems can contain millions of rows. As humans we cannot really understand that much information all at once. Plus, the types of problems that we are solving usually involve a few pieces of data at a time. So formatting allows us to interpret the data more efficiently. Easier to read, understand, and debug it.

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

=, <, >, !=

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

SQL provides us with the ability to limit the number of rows included in a result set.

select “name”,
“description”
from “products”
order by “price” desc
limit 1;

Before we look at the result set, here are some things to note about the example select statement:

The limit clause comes last.
The limit clause includes a literal integer number with no quotes to specify the maximum number of rows that should be returned.
The sort order of the order by clause is switched to descending order with the desc keyword.

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

It is possible to select all of the columns in a table by replacing the list of column names with an * asterisk.

select *
from “products”;

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

The default sort order of the results is ascending order, but you can change it to descending

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

An SQL insert statement is a means of adding rows to a table. Consider the following “products” table:

To add a row to this table, we would likely execute the following command:

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

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).

In this particular statement, the “productId” was left out. This is because tables are often configured to auto-generate identifier attributes to avoid accidental duplicates.

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

What is a tuple?

A

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.

17
Q

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

A

Data rows can be batch inserted into a database table by specifying more than one tuple of values, separated by commas. Below we are inserting two new rows into the “products” table.

insert into “products” (“name”, “description”, “price”, “category”)

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

   ('Tater Mitts', 'Scrub some taters!', 6, 'cooking') returning *;
18
Q

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

A

returning *;

If you only want specific values back, you can use a comma-separated list of column names instead of an * asterisk.

returning “name”, “language”;

19
Q

How do you update rows in a database table?

A

An SQL update statement is a means of updating rows in a database table. Consider the following “products” table:

To make an update we might execute the following command:

update “products”
set “price” = 100;

HOWEVER there is a serious problem with this query as it would update every row in the table!

Therefore, great care must be taken to include a where clause in your update statements to only target specific rows:

update “products”
set “price” = 100
where “productId” = 24;

20
Q

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

A

To target only specific rows, so that it is not updating every row in the table

21
Q

How do you delete rows from a database table?

A

An SQL delete statement is how rows get removed from tables. Consider the following “products” table:

To delete from this table, we might execute the following command:

delete
from “products”;

💀💀💀💀 WE JUST DELETED EVERYTHING FROM THE TABLE! and there is no undo. 😢

You must be extremely careful when deleting things from a table as it cannot be undone. Let’s try that again. We only want to delete the ShakeWeight because, well, it’s ridiculous. We can target specific rows to delete by including a where clause. As with insert and update statements, we can include a returning clause if we want the affected row(s) returned to us. A returning clause is not mandatory though.

If we wanted to delete all “products” in the ‘cleaning’ category that are cheaper than 20, we would:

delete
from “products”
where “category” = ‘cleaning’
and “price” < 20

22
Q

How do you accidentally delete all rows from a table?

A

delete
from “products”;

If a where clause is not used

23
Q

What is a foreign key?

A

This is known as a foreign key. Instead of putting all of the supplier information for a product into the product row itself, there is instead just one column that links the “products” table to the “suppliers” table.

24
Q

How do you join two SQL tables?

A

An SQL join clause is a way of combining data from two different database tables into one result set.

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

We are selecting all columns from both the “products” table and the “suppliers” table.
The join clause follows the from clause.
The join clause is instructing the database server to link the two tables by their “supplierId” column.

25
Q

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

A

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

We may want to know the highest price in the entire table. We’d use the max() aggregate function. It’s not strictly required that you alias an aggregate (with as), but it’s good practice because it communicates your intent more clearly.

select max(“price”) as “highestPrice”
from “products”;

If we wanted to know the average price, there’s an avg() aggregate function for that:

select avg(“price”) as “averagePrice”
from “products”;

Or maybe we just want to know the number of rows in the “products” table. We’d use the count() aggregate function:

select count(*) as “totalProducts”
from “products”;

There are many aggregate functions available to you in SQL, including min( ), sum( ), and every( ).

27
Q

What is the purpose of a group by clause?

A

The GROUP BY Clause is utilized in SQL with the SELECT statement to organize similar data into groups. It combines the multiple records in single or more columns using some functions.