SQL Career Principles Flashcards

(31 cards)

1
Q

What are the six main data types in SQL?

A
  1. INT - integer (whole number)
  2. DECIMAL (x,x) - Decimal numbers (first x states digits, second x states digits after decimal)
  3. VARCHAR(x)- string of text(x is used to specify max length)
  4. DATE - in YYYY-MM-DD format
  5. TIMESTAMP - in YYYY-MM-DD HH:MM:SS
  6. BLOB - Binary Large Object. Used for files, images, videos, and other large projects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the SQL syntax to create a database?

A

CREATE DATABASE name

name = database name

or click the “Create New Schema” button

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

What is the SQL syntax to create a table?

A

CREATE TABLE name (
column name1 INT PRIMARY KEY,
column name2 VARCHAR(25),
column name3 DEC(8,2)
)

name = table name; column name = name of column, for each column you need to identify the data type and also indicate the primary key

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

What does first_name VARCHAR(25) mean?

A

this is a column name that is a text data type limited to 25 characters

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

what does salary DEC(8,2) mean?

A

this is a column name and this data type is used for decimal numbers. The “8” represents the number of total digits and the “2” represents the number of decimals. In this case a $100K salary would show as 100000.00

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

How do you make a change to a table or column after creating?

A

To make a change to a table:

 ALTER TABLE database.tablename ADD column4 INT(2)

 this will add another column that is a whole 
 number with max 2 characters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to change the name of a table ?

A

RENAME TABLE tablename TO tablename2

write the current table name first and the new table name second

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

How to change the data type for a column?

A

ALTER TABLE tablename MODIFY COLUMN column2 VARCHAR(50)

this will change column2 to a text field with a 50 character max

NEED TO CONFIRM THIS

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

How to add data to the tables?

A

most of the time you will add data via CSV or API but you can add manually by:

INSERT INTO tablename VALUES (Column1data, Column2data, Column3data)

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

How to change data in a table?

A

UPDATE tablename
SET column1 = ‘bbbb’
WHERE column1 = ‘aaaa’

*this changes any ‘aaa’ in column1 to ‘bbbb’

*** if you want to change all the data within a column to the same text/number/date you just delete the WHERE statement

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

how to run a query for the top 10 records?

A

simply use the LIMIT 10 statement

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

Let’s say you have a column lif_ expectancy and you want to create another column that adds 10 years to each record for life_expectancy, how would you do this?

A

Select
life_expectancy,
life_expectancy +10
FROM tablename

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

What is the BETWEEN statement and what is the syntax?

A

the BETWEEN statement is used with WHERE or HAVING. this will filter the records based on a range of values

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN lower_bound
AND upper_bound;

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

What is REGEXP?

A

this is a statement used with WHERE or HAVING that is used for pattern matching. It is similar to LIKE but more powerful

examples:

match strings starting with a specific letter: WHERE name REGEXP ‘^A’

match strings ending with a specific letter:
WHERE name REGEXP ‘N$’

match strings containing digits: where product_number REGEXP ‘[0-9]’

match multiple patterns: WHERE fruit_name REGEXP ‘apple|orange’

 | = or

You can write WHERE name REGEXP ‘^a[cdf]’ – this will return any text that starts with an ‘a’ and either has a c,d,f as the second character.

You can write WHERE name REGEXP ‘^a[b-f]’ – this will return any text that starts with an ‘a’ and either has a b,c,d,e,f as a second character

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

What are window functions in SQL?

A

a special type of function that perform calculations across a set of rows that are related to the current row. However, unlike aggregate functions (like SUM, AVG, etc.), window functions do not group the rows into a single result—they retain the individual rows while providing a calculated value based on the defined “window” of data

this statement goes in the SELECT section and the syntax is:

function_name (column_name) OVER (
[PARTITION BY column_name(s)]
[ORDER BY column_name(s)]
)

There are several types of functions you can use.
1. Aggregate
2. Value
3. Ranking

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

Related to the Window function, explain the different functions you can use

A

Aggregate:
-Avg()
-Max()
-Min()
-Sum()
-Count

Value:
-Row_number
-RANK()
-DENSE_RANK()
-PERCENT_RANK()
-NTILE()

Ranking:
-LAG()
-LEAD()
-FIRST_VALUE()
-LAST_VALUE()
-NTH_VALUE()

17
Q

Related to the Window function, what is the LAG & LEAD function?

A

Lag Function “look behind function”:

  • What it does: It retrieves the value from the previous row in a result set.
  • Simple Example: Imagine a list of dates and sales. If you want to know what the sales were on the previous day for each row, you use the LAG function.

Lead Function look ahead function:

  • What it does: It retrieves the value from the next row in a result set.
  • Simple Example: If you want to know what the sales will be on the next day for each row, you use the LEAD function.
18
Q

Related to the Window function, what is the difference between ROW_NUMBER, RANK, and DENSE_RANK?

A
  1. ROW_NUMBER
    What it does: Assigns a unique, sequential
    number to each row in the result set.Key feature: No ties—each row gets a unique
    number, even if two or more rows have the same
    values in the columns being ordered.
  2. RANK
    What it does: Assigns a rank to each row, but
    rows with the same values get the same rank.However, it skips the next rank(s) after a tie.
  3. DENSE_RANK
    What it does: Similar to RANK, but it doesn’t skip
    ranks after a tie.
19
Q

How to join two different databases?

A

Very similar to joining two tables, just add the database name (note world and company are two different databases):

SELECT * FROM world.city
INNER JOIN company.teams
ON city.id = teams.staff_id

20
Q

What are some of the top STRING functions?

A
  1. LENGTH: Returns the string length
    LENGTH(string)
  2. LEFT / RIGHT: Returns the left n characters / right
    n characters of a string
    LEFT(string, n) / RIGHT(string, n)
  3. REPLACE: Replaces all occurrences of a specified
    string value with another specified string value
    REPLACE(string, start, length, replacement)
  4. UPPER / LOWER: Changes the format to
    uppercase / lowercase
    UPPER(string) / LOWER(string)
  5. TRIM: Removes the space character or other
    specific character from the start / end of a string
    TRIM(string)
  6. CONCAT: Returns a string after joining two or
    more string
    values
    CONCAT(string1, string2, …)
  7. SUBSTRING: Extracts a substring from a string
    SUBSTRING(string, start, length)
21
Q

What are some data & time functions?

A
  1. NOW: Returns the current date and time - NOW()
  2. CURTIME: Returns the current time - CURTIME()
  3. CURDATE: Returns the current date - CURDATE()
  4. DAY: Returns the day - DAY(date)
  5. MONTH: Returns the month- MONTH(date)
  6. YEAR: Returns the year - YEAR(date)
  7. DAYNAME: Returns the name of the day -
    DAYNAME(date)
  8. MONTHNAME: Returns the name of the month -
    MONTHNAME(date)
  9. WEEK: Returns the week number - WEEK(date)
  10. DATEFORMAT: Formats the date as you specify -
    DATE_FORMAT(date, format)
22
Q

How do you write an IF statement?

A

it is pretty similar to excel

IF(salary >= 50000,’large_salary’,’small salary’)

Notes:
-when using text, make sure to use quotes

23
Q

What is the CASE statement and how do you use it?

A

this is used in the SELECT section and is it similar to the IF function but more powerful

CASE
WHEN indepYear = 0 THEN ‘never’
WHEN indepYear < 1900 THEN ‘ancient
else ‘new’
END AS category

make sure you use END for the statement to work

24
Q

What is the CAST statement and how do you use it?

A

The CAST statement in SQL is like a tool that changes the “type” of data so it fits what you need. Imagine you’re working with numbers, text, or dates, and you need to switch one type into another to do something specific. That’s where CAST helps.

you would use this to change a decimal data type to change an Integer (note you need to use the SIGNED function). see below:

SELECT
lifeexpectancy,
CAST(lifeexpectancy AS SIGNED)
FROM country;

25
What are subqueries?
A subquery in SQL is like a small "question within a question." It’s a query (a request for data) that is nested inside another query. The result of the subquery helps the main query do its job. You can think of it like this: "I need to answer a small question first, and then use that answer to solve the bigger question." Where Subqueries Are Used: You can use subqueries almost anywhere in a SQL query, such as in the SELECT, FROM, or WHERE clauses. For example, find the countries that have a life expectancy over the world average.
26
Practice Problem: Create a new column called district_update to replace all dashes with a space, and make all the values upper case.
SELECT Name, District, UPPER(REPLACE(District,'-',' ')) AS District_Update FROM world.city;
27
Practice Problem: Increase the country GNP by 10% if it's greater than zero. If it's zero, leave blank. Call the new column adjusted GNP
SELECT Name, Region, GNP, CAST(IF(GNP>0,GNP*1.1,'') AS SIGNED) AS Adjusted_GNP FROM world.country;
28
Practice Problem: How do you change the name of a column header?
ALTER TABLE tablename MODIFY COLUMN columnNameOld TO columnNameNew
29
What's the difference between the CEILING, FLOOR, and ROUND function?
these are all used to round a decimal but CEILING rounds every number up, FLOOR rounds every number down, and ROUND rounds eithers up or down depending on what the decimal is (ie 1.4 becomes 1, 1.6 becomes 2)
30
Practice Problem: If you want to round a column with decimals to whole numbers, what is the query?
UPDATE tablename SET columnname = ROUND (columnname)
31