SQL Flashcards

1
Q

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

A

it is a querying language that stores info and u can organize the info. It is declarative, tell it what you want

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 then add the column headers
use from keyword table

select “”
from “”

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 “” = ‘’

where keyword then the column header in double quotes comparison operators then specific criteria in single quotes

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

organization and time efficiency

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 numerical value

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 * (univeral key)

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 keyword then followed by a column header in double quotes then descending if descending

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

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.T ext values are wrapped in ‘ single quotes.

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

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

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

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

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

put a comma inbetween then add the second values in the same order as above

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 keyword followed by asterisk at the end of the query and then a semicolon;

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? Why is it important to include a where clause in your update statements?

A

update “products”
set “price” = 200,
“name” = ‘Super ShakeWeight’,
“description” = ‘Makes you ULTRA strong!’

where “productId” = 24;

update keyword then set keyword then where keyword; It is so u do not update everything within a column to one value

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

How do you delete rows from a database table?

A

delete from “products”
where “productId” = 24
returning *

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

Properly start with delete from (a table name) where is the column title and value at a certain point.
Second option is if you have multiple criteria

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

How do you accidentally delete all rows from a table?

A

delete from “products”; IF YOU DO NOT SPECIFY WHERE THEN IT WILL DELETE EVERYTHING ON A TABLE

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

What is one way to see if PostgreSQL is running?

A

sudo service postgresql start (to start)

sudo service postgresql status (to check if running)

sudo service postgresql stop
(stop it from running)

17
Q

What is a foreign key?

A

It is when a key of a table is referenced from another table

18
Q

How do you join two SQL tables?

A
Using the join keyword 
EX:
select *
  from "products"
  join "suppliers" using ("supplierId");
19
Q

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

A
select "p"."name" as "product",
       "p"."category",
       "s"."name" as "supplier",
       "s"."state"
  from "products" as "p"
  join "suppliers" as "s" using ("supplierId");

By aliasing the names using as keyword