SQL Flashcards

1
Q

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

A

A declarative programming language. Programmers describe the desired results, and the programming environment finds a way to get that done. In JavaScript, the programmer tells the runtime how to get the 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 “database table”

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

Using the “where” keyword and some kind of condition (<, >, =, !=)

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

<, >, =, !=

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” keyword followed by a number will return only the specified number of rows

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

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

default sort order is ascending order. Including “desc” on the order by line at the end changes the order to descending order

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

insert into “table name” (“column-a”, “column-b”, “column-c”, “column-d”)
values (‘value-a’, ‘value-b’, ‘value-c’, ‘value-d’);
returning *

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

after the values keyword, use comma-separated tuples of values to insert more than one row

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

add a line at the end of the request
returning “columnName”
(* can also be used to return all)

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

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

To make sure that the intended row is being updated, otherwise all rows will get updated to the given 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 “table”
where “column” = ‘value’
and “column” (<, >, !=) ‘value’
returning *;

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

A column that references primary key values of a column in another table

18
Q

How do you join two SQL tables?

A

select “column” or *
from “table”
join “table” using (“common column”) [this column must be a shared column]

19
Q

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

A

aliasing
select “table”.”column:before” as “column:after”,
“table-2”.”column:before” as “column:after”
from “table”
join “table-2” using (“common column”);

20
Q

What are some examples of aggregate functions?

A

max(), avg(), count(), min(), sum(), every()

21
Q

What is the purpose of a group by clause?

A

grouping together values of a column based on a common value in the group by column