SQL Flashcards

1
Q

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

A

SQL (Structured Query Language) is the primary way of interacting with relational databases.

SQL is a declarative programming language.

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

A Select statement

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

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

where “column name” = ‘attribute name’;

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

It makes it look cleaner.

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

=, >,

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

limit

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 *

from “column”;

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

desc keyword

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

Use the insert statement

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

What is a tuple?

A

A finite ordered list of elements

SQL: A set of sets, defining a container with order.

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

specify more than one tuple of values separated by commas.

data, data, data),
(data, data, data

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

Use the returning clause. If you want all columns you can use * otherwise use a comma-separated list instead.

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

update

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 don’t specify a specific row it will update all items in that column.

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 “table”
where “row” = ‘value’

Complex where/and:
where “row” = ‘value’
and “row” (, =, !=) ‘value’;

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 “table”;

17
Q

What is a foreign key?

A

When a columns data references another tables data.

18
Q

How do you join two SQL tables?

A

select “column”
from “table”
join “table” using (“column”)

19
Q

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

A

Aliasing

select “table”.”column” as “newLabel”

this can also be done when using from and join.
select “table”.”column” as “newLabel”
from “products” as “p”
join “suppliers” as “s” using (“supplierId”); (for example)

repeat as necessary by separating with commas.

20
Q

What are some examples of aggregate functions?

A
max(): Finds max number
avg(): Finds average
count(*): Counts number of rows
min()
sum()
every()
look into JSON aggregate functions
21
Q

What is the purpose of a group by clause?

A

Separates rows into groups to perform aggregate functions.