Understanding Basic Data Types in SQL

Photo by Tim Gouw on Unsplash

Understanding Basic Data Types in SQL

Continuing on from the previous blog (here), I wanted to discuss some of the basic data types that you're likely to come across when writing queries in SQL. When you’re just starting out with SQL, understanding data types might seem a bit daunting. But don’t worry! It’s simpler than it looks. Think of data types as the way SQL helps you organize and manage different kinds of data in your database. Let’s dive into some of the most common data types you’ll encounter.

  1. INTEGER

    The INTEGER data type is used for whole numbers. It’s great for things like counts, ages, or any other values that don’t need decimal points. For example, if you’re keeping track of the number of products in inventory, you’d use an INTEGER data type. When creating a database and specifying what data type your columns will be, your query will be structured something like this:

CREATE TABLE new_data_table(
    product_id INTEGER,
    product_name VARCHAR(200)
    product_count INTEGER
);

We can break down the code above as follows:

  • CREATE TABLE new_data_table( ): creates a new table in our database called 'new_data_table'

  • product_id INTEGER: our first column name is 'product_id' and it's data type will be an integer (ie - 1, 2, 3, 4...)

  • product_name VARCHAR(200): our next column name is 'product_name' and it's data type is VARCHAR (variable character - explained below) and in parentheses we note the maximum number of characters allowed

  • product_count INTEGER: our final column name - 'product_count' with a data type of INTEGER

  1. VARCHAR

    VARCHAR is short for variable character, and it’s used for text strings. This could be anything from names to descriptions. When you define a VARCHAR column, you specify the maximum number of characters it can hold in the parentheses that immediately follow.

CREATE TABLE customers (
    customer_id INTEGER,
    customer_name VARCHAR(100),
    email VARCHAR(255)
);

So, in the above query, we are creating a table called 'customers' which will have 3 columns - customer_id, customer_name, and email. Our two variable character columns also include the maximum number of characters allowed for each - 100, and 255, respectively. VARCHAR allows for the use of letters, numbers and special characters, such as '&', '@', '!', and many others. A perfect solution when storing data such as email addresses.

  1. NUMERIC / DECIMAL

    The NUMERIC or DECIMAL data type is for numbers with decimal points where precision matters. This is crucial for financial data like prices or salaries because it maintains the exact value without rounding off.

CREATE TABLE salaries (
    employee_id INTEGER,
    annual_salary NUMERIC(10, 2)  -- up to 10 digits, 2 of which are after the decimal
);

In the query above, our annual_salary column is a NUMERIC data type. This allows for precision after the decimal point, and in this instance (10, 2), we are asking it to return values that include 2 numbers after the decimal point. The 10 is asking to return 10 total digits, including the 2 digits after the decimal point, so we can generally expect to have 8 digits prior to the decimal point.

  1. FLOAT

    FLOAT is also used for numbers with decimal points but is designed for scientific calculations where approximation is acceptable. It’s less precise than NUMERIC but can handle a larger range of values.

CREATE TABLE measurements (
    measurement_id INTEGER,
    temperature FLOAT
);
  1. DATE

    The DATE data type is for storing dates. It’s perfect for keeping track of when events happen, like when an order was placed or when a product was manufactured. DATE can take many variations, but is most often structured as YYYY-MM-DD, or 2024-08-12.

CREATE TABLE orders (
    order_id INTEGER,
    order_date DATE,
    customer_id INTEGER
);
  1. BOOLEAN

    The BOOLEAN data type stores true or false values. It’s useful for flags that indicate whether a condition is met, like whether a user is active or an order is paid.

CREATE TABLE users (
    user_id INTEGER,
    user_name VARCHAR(100),
    is_active BOOLEAN
);

Booleans not only assist in helping to draw conclusions about your data, but also help to filter your data based on whether the values are true or false.

Why Data Type Matters

Choosing the right data type is crucial for several reasons:

1. Efficiency: The correct data type ensures that your database is using the right amount of storage and processing resources.

2. Accuracy: Especially with NUMERIC and DECIMAL, using the correct data type ensures you don’t lose precision in calculations.

3. Validation: SQL will check that the data you’re entering matches the expected type, helping prevent errors.

Conclusion

Understanding and using the correct data types is a fundamental step in database management. It ensures that your data is stored efficiently and accurately, and that your queries run smoothly. As you continue to work with SQL, these data types will become second nature, helping you to better organize and manage your data.

By familiarizing yourself with these basic data types, you’ll be well on your way to writing more effective and efficient SQL queries. Remember, every piece of data has a best-fit type, and using the right one will make your database work more seamlessly. And don't be afraid to try different things and make mistakes. Go out and break things - it's the best way to learn!

Here is a link to a free course on SQL from codecademy!

Feel free to reach out if you have any questions or need further clarification on any of these data types. Good luck on your SQL journey!