sql select Flashcards

1
Q

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

A

Structured Query Language is a way of interacting with relational databases to retrieve, create and manipulate data. SQL is a declarative programming language, programmers describe the result like HTML and CSS.

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 “string”, “string” from “table to retrieve from”;
to select all use select * from “products”

column double quotes

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 clause.
where double quotes comparison operator single quote

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

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

makes it easier to read and access data

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

with 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

order by “price” desc (descending)

limit 1;

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 statement
insert into “ “ ( “ “ )
values ( ‘ ‘ ) ;

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

specify more than one tuple of values separated by commas:

values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’),
(‘Tater Mitts’, ‘Scrub some taters!’, 6, ‘cooking’)

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

update clause:

update “products”
set “price” = 200,
“name” = ‘Super ShakeWeight’,
“description” = ‘Makes you ULTRA strong!’
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

otherwise all of the rows will update

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 clause

delete from “products”
where “category” = ‘cleaning’
and “price” < 20
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

by not specifying a where

17
Q

What is a foreign key?

A

a column that links one table to another, common property or column between two tables.

18
Q

How do you join two SQL tables?

A

Select statement
From “table 1”
join “table 2” using (“foreign key”);

19
Q

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

A

can alias column names using the

20
Q

What are some examples of aggregate functions?

A

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

select “category”,
avg(“price”) as “averagePrice”
from “products”
group by “category”;

21
Q

What is the purpose of a group by clause?

A

groups all of the data together