SQL Select, Insert, Update, Delete, Join, Aggregates Flashcards

1
Q

What is SQL and how is it different from languages like JavaScript?

A

Structured Query Language

Primary way of interacting with relational databases

Javascript is an imperative programming language where as SQL is a declarative programming language

Programmers describe the results they want and the programming environment comes up with its own plant or getting those results

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

How do you retrieve specific columns from a database table?

A

select “column”,

from “row”

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

How do you filter rows based on some specific criteria?

A

You use the ‘where’ clause

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

What are the benefits of formatting your SQL?

A

Consistency and readability

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

What are four comparison operators that can be used in a where clause?

A

Equals =

Less than <

Greater than >

Not equal !=

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

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

A

Using the ‘limit’ clause

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

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

A

You use the ‘order by’ clause

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

How do you add a row to a SQL table?

A

You use the ‘insert’ statement

insert into “row” (“column1”, “column2” etc..)
values (‘value1’, ‘value2’, etc..)

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

What is a tuple?

A

A list of values

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

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

A

You separate the values in parentheses with a comma ,

insert into “row” (“column1”, “column2” etc..
values (‘value1’, ‘value2’, etc..),
‘value1’, ‘value2’, etc..)

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

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

A

returning *

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

How do you update rows in a database table?

A

You use the update statement with a set clause

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

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

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

A

If you do not include a where clause, it will update all price values instead of your targeted value

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

How do you delete rows from a database table?

A

delete from “row”

where “example” = ‘example’

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

How do you accidentally delete all rows from a table?

A

delete from “row”

If you do not use a where clause, you will delete everything in the table

17
Q

What is a foreign key?

A

A foreign key is a column that refers to values from another row

18
Q

How do you join two SQL tables?

A

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

19
Q

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

A

select “p”.”name” as “product”,
“s”.”name” as “supplier”
from “products” as “p”
join “suppliers” as “s” using (“supplierId”);

You use the ‘as’ keyword when selecting the column

20
Q

What are some examples of aggregate functions?

A

avg, count, max, min, every, sum

21
Q

What is the purpose of a group by clause?

A

It allows you to separate rows into groups and perform aggregate functions on those groups of rows