SQL - Advanced Flashcards

(35 cards)

1
Q

What must you start-up in order to work with postgres?

A

PgAmin 4 – it’s in your programs - you can start it from the search bar

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

Why are datatypes important?

A

data integrity - they ensure the right data is stored in each column

efficiency - process data quickly and store it with less space

when creating a new table you have to specify the datatype for each column and any relevant properties for that datatype.

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

What are the 8 datatypes Luke likes to use?

A

INT - integer, whole number
BOOLEAN - true/false or null
NUMERIC (precision, scale)- can include decimals etc.
DATE
TIMESTAMP - when data was saved, changed etc.
TIMESTAMP with TIME ZONE
VARCHAR - strings w/character limit
TEXT - strings - no character limit

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

how many datatypes are there?

A

Probably hundreds. You can view all of them in the Postgresql documentation

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

What are the properties of the NUMERIC datatype?

A

NUMERIC data type has two properties: precision & scale

  • Precision is how many total digits the number has
  • Scale is how many digits to the right of the decimal

Example: 623.859 - this number has a precision of 6 and a scale of 3

This would be written:
NUMERIC(6,3)

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

TEXT vs. VARCHAR

A

TEXT - variable length strings of unlimited length - often used for text that doesn’t fit within the VARCHAR constraints

VARCHAR - has a ‘character limit’ property. Can store strings of any length up to that character limit.

Looks like the character limit is defined by the user. The character limit is represented by N in the following example:
VARCHAR(N)

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

What can you tell me about the BOOLEAN datatype?

A

accepts the values of true, false or null – he was using 0 and 1 and said this is effectively true or false

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

DATE vs. TIMESTAMP vs. TIMESTAMP w/ TIMEZONE

A

DATE - date w/out time
format: YYYY-MM-DD

TIMESTAMP - date with time - can include seconds depending on precision
format: YYYY-MM-DD HH-MI-SS

TIMESTAMP w/TIMEZONE -
Adjusts stored time based on the timezone
FORMAT:YYYY-MM-DD HH-MI-SS+00:00
I think the 00:00 would be HH:MI

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

What statement allows you to create a table?

A

CREATE TABLE

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

What statement allows you to add data columns to your table

A

INSERT INTO

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

What statement allows you to delete a table

A

DROP TABLE

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

What are the 4 main commands you can use with a table?

A

CREATE TABLE,
INSERT INTO - add columns
ALTER TABLE add columns, rename column, change datatype of column, delete a column
DROP TABLE - delete table

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

What are the 4 main commands INSIDE the ALTER TABLE command?`

A

ADD - add a column
RENAME COLUMN
ALTER COLUMN - change the datatype of a column
DROP COLUMN - delete a column

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

After you’ve created the database shell and you’re ready to work with your tables, what do you do?

A
  1. Go into pgAdmin4 and make sure your connection to the desired database is up and running - just click on it and it will start up
  2. Go into VS Code and click on databases (cylinder on left) and click on the desired database and make sure it has the name of your desired database on the very bottom - in the footer of the window.
  3. Click on the database name in the left pane - and then click on the new sql file icon - which is has a plus sign with “SQL” next to it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you initially create a table with columns?

A

CREATE TABLE table_name (
column_name datatype,
column_name datatype,
. . .
);

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

how do you insert data into a table?

A

INSERT INTO table_name (column_name1, column_name2, column_name3 . . . . )
VALUES (value1, value2, . . . )

17
Q

How do you alter a table?

A

ALTER TABLE table_name

ADD column_name datatype;

RENAME COLUMN column_name TO new_name;

ALTER COLUMN column_name TYPE datatype;

DROP COLUMN column_name;

Not sure if you can do multiple of these separated by a comma, or if you can only do one action per ALTER TABLE command.

18
Q

How do you update a table?

A

UPDATE table_name
SET column_name = ‘new_value’
WHERE condition;

in this instance you use WHERE to filter which rows to update based on a condition.

Example:
UPDATE job_applied
SET contact = ‘Prince Harry’
WHERE job_id = 3;

19
Q

How do you rename a column?

A

ALTER TABLE table_name
RENAME COLUMN column_name TO new_name;

20
Q

How do you change the datatype of a column?

A

ALTER TABLE table_name
ALTER COLUMN column_name TYPE new_data_type;

example:
-ALTER TABLE job_applied
–ALTER COLUMN contact_name TYPE TEXT;

21
Q

How do you drop (delete) a column?

A

ALTER TABLE table_name
DROP COLUMN column_name

Always check and double check before you drop anything!!!

22
Q

How do you drop a table?

A

DROP TABLE table_name

Always be extremely careful when you do this! Check and double check!

23
Q

How do you create a database (3 steps)

A
  1. download CSV and SQL files
  2. create tables in the database
  3. load data into tables
24
Q

Primary Key vs. Foreign Key

A

A primary key is a unique identifier for each row within a table. Every table in the database has a primary key.

A foreign key is a column in one table that references the primary key in another table. It creates a link between the two tables.

Example
Customer_ID could be the primary key in the Customer table. Order_ID could be the primary key in the Orders table.
A Customer_ID column in the orders table would link each order to a customer. Customer_ID would be a foreign key in the ORDERS table and Order_ID could be a foreign key in the Customers table.

in VS Code - left side bar - gold key is primary key and gray key signifies the Foreign Key.

25
What is a primary key in a database?
A primary key is a unique identifier for each record (row) in a table. --It ensures that each row is distinct and prevents duplicate entries -- Primary keys cannot contain NULL value - every row must have a valid, unique value -- allow for efficient data retrieval -- enable relationships between tables in a database Usually a primary key is composed of 1 column, but it can be composed of multiple columns which is called a composite key
26
What is a foreign key in a database
A foreign key in a table is a primary key from another table. You add a foreign key to a table so that you can relate the two tables (part of the reason it's called a relational database). It establishes a link between the two tables. ibid - a foreign key is a column (or set of columns) in one table that refers to the primary key of another table. Foreign keys are CRUCIAL for performing JOIN operations in SQL
27
How do you use the EXTRACT keyword? And what does it do?
EXTRACT gets a field (like year, month or day) from a date/time value Example: SELECT EXTRACT(MONTH FROM column_name) AS new_column_name FROM table_name
28
How do you take data from a large table and put a subsection of that data into a smaller table?
This gets all the job postings from the month of January and puts it in a new table. CREATE TABLE january_jobs AS SELECT * FROM table_name WHERE EXTRACT (MONTH FROM column_name) = 1;
29
what is CASE expression?
CASE expression is a way to apply conditional logic within your SQL queries. Similar to an IF statement in other programming languages. Example: SELECT CASE WHEN column_name = 'value1' THEN 'description for value 1' WHEN column_name = 'value2' THEN 'description for value 2' ELSE 'output if condition is not true' END AS new_column_you're_creating FROM table_name; CASE - begins the expression WHEN - specifies conditions to look at THEN - what to do when the condition is true ELSE - provides output if none of the conditions are met You could use this to find jobs that are remote, or find jobs that are local and disregard the rest.
30
using a case statement, how would you group job postings into 3 locations: local, remote and everything else?
SELECT job_title_short, job_location, CASE WHEN job_location = 'New York, NY' THEN 'local' WHEN job_location = 'Anywhere' THEN 'Remote' ELSE 'Move required' END AS location_category FROM job_postings_fact
31
What is a CTE and what does CTE stand for?
Common Table Expressions They are used for organizing and simplifying complex queries. Breaks the query into smaller more manageable parts. - Define a temporary result set that you can reference - Must use the keyword WITH - enclosed in parenthesis - Can reference the temporary result set within a SELECT, INSERT, UPDATE, DELETE statement Example - this selects all the jobs postings posted in January and puts it in it's own "table" (january_jobs) but it's really a temporary result set. So you can use it like a table, but you don't have to create another table: WITH january_jobs AS ( SELECT * FROM job_postings_fact WHERE EXTRACT (MONTH FROM job_posted_date) = 1 ) SELECT * FROM january_jobs
32
What is a subquery?
A subquery, also known as a nested query or inner query, is used to organize and simplify complex queries - Breaks the query into smaller more manageable parts. - A subquery is a query that's NESTED inside of another query. - It can be used in the SELECT, FROM and WHERE and HAVING clauses. - allows you to use the result of one query as input in another query. -- It's executed first and then those results are passed to the outer query. -- Can be used when you want to perform a calculation before the main query performs it's calculation. kind like order of operations - does the subquery first, then the outer query. Example he gave: If you wanted to find all the jobs where there was not a degree mentioned and then you wanted to know the name of the company that listed that job posting. Since the subquery makes a 'temporary result set' you may be able to use it like a (temporary) table. Create the subquery and then put it in the FROM clause like you would a table.
33
What is an example of a CTE?
This query finds the skills that are mentioned most often in job postings and orders them by most mentioned to least mentioned. Then it matches the skill_id with the skill name using a join. Because a CTE makes a new temporary collection of data, you put the CTE in the FROM clause, just like you were using it as a table. For example, below I make a 'temporary result set' named skill_counts and I use it like a table, the left table, when I make a join. WITH skill_counts AS (SELECT COUNT (skill_id) AS molly, skill_id FROM skills_job_dim GROUP BY skill_id ORDER BY molly DESC) SELECT skill_counts.molly, skill_counts.skill_id, skills_dim.skills FROM skill_counts LEFT JOIN skills_dim ON skill_counts.skill_id = skills_dim.skill_id You also have a copy of this in VS Code.
34
What is the UNION operator and what does it do?
It combines the result sets of two or more select statements into a single result set. UNION - removes all duplicate rows UNION ALL - includes all duplicate rows each SELECT statement within the UNION must have the same number of columns and those columns must have the same datatype For example - we used UNION and UNION all to combine the results of the January, February and March tables. They all had the same columns and datatypes.
35
What is the syntax for a UNION or UNION ALL statement
SELECT column_name FROM table_name1 UNION (or UNION ALL) SELECT column_name FROM table_name2 (You could also add another one!) UNION (or UNION ALL) SELECT column_name FROM table_name3 etc. etc. etc.