Lesson 5 Flashcards

Data Types; CAST (10 cards)

1
Q

There’s a huge variety of data in the real world that results in different formats in a database. Some of the main ones are:

(Name 5)

A

1. String / Varchar

  • text data like a name, address, or customer feedback text box

2. Integers

  • whole numbers without decimal places like age, number of people, or subscription count (you can’t have 1.35 people or 67.98 subscriptions!)

3. Decimals

  • numbers with decimal places like price, revenue, or rainfall amount

4. Dates

  • Date values without a timestamp like 2024-01-03

5. Datetimes

  • Date values with a timestamp like 2024-01-03 03:09:26
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does varchar stand for?

A

Variable character

  • Just means it’s a string or a text field
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is data cleaning?

________________________________

Why do we need to do it? (What’s a common issue in a database?)

A

Data cleaning can involve:

  • Cleaning up a field so that values look better and more appropriate in the output
  • Converting data types in order to use them in relevant functions
    • More common

____________________________________________

One common issue in a database is that:

  • Data can be imported or loaded into a database in incorrect formats
  • (Or maybe the data was just incorrect in the first place at the source)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a function?

A

You put something IN and get something OUT.

  • Put in values or columns (called inputs or arguments)
  • Get out some sort of output (based on what the function does)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you check the data type of every single row in the order_date column?

What’s one example of why you would do this?

(Answer & write code)

A

typeof() function

  • Helpful to confirm that data imported correctly
SELECT
    typeof(order_date)
FROM
    orders
  • Note: may have slightly different syntax depending on your SQL dialect
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Be careful – sometimes when you’re importing data, date fields can sometimes import as ___

A

Be careful – sometimes when you’re importing data, date fields can sometimes import as strings or varchars

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

How can you convert columns between different data types?

When is this especially helpful?

A

We can use the CAST() function to convert values & fields (columns) between data types.

________________________________________________

CAST can become really handy when:

  • Cleaning data
  • Converting data types, so the data can be input into different functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you:

  • Pull the coupon column from the orders table
  • Check the data types of the coupon column
  • Convert the data types of the coupon column to a decimal data type

(Write code - no short hand)

A
SELECT
    coupon,
    typeof(coupon),
    cast(coupon as decimal)
FROM
    orders
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A lot of newer cloud platforms (like Snowflake) support a shorthand notation of cast by using ____.

For example, from the orders table:

  • Pull the order_date column and convert it to a varchar data type
  • Pull the coupon column and convert it to an integer data type
  • Pull the order_date column and convert it to a datetime data type
  • Pull the price column and convert it to a decimal data type

This shorthand makes you not look like a newb :)

A

A lot of newer cloud platforms (like Snowflake) support a shorthand notation of cast with 2 colons (::) after the value or column name.

SELECT 
   order_date::varchar, 
   coupon::int, 
   order_date::datetime, 
   price::decimal 
FROM
   orders
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Changing the data type of a column in a query does / does not change the data type in the original source table.

A

Changing the data type of a column in a query does NOT change the data type in the original source table.

  • It only creates that new version temporarily in your query.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly