Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to SQL for Data Analysis ยท Lesson 3 of 9

Module Two

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
3

Module 2: Setting Up and Understanding SQL

Module Overview

In Module 1, you learned what SQL and databases mean. You also discovered how schools, banks, hospitals and supermarkets use databases to organise information.

In this module, you will learn how to start working with an SQL database. You will discover how to create a database, create tables, choose data types and insert information into a table.

We will use MySQL for our examples because it is a popular database system used by many websites and businesses.

Learning Objectives

By the end of this module, you should be able to:

  • Understand what an SQL environment is.
  • Identify tools used to work with SQL.
  • Connect to a MySQL database.
  • Create and select a database.
  • Understand common SQL data types.
  • Create database tables.
  • Insert records into a database table.
  • View databases and tables using SQL commands.

1. What Is an SQL Environment?

An SQL environment is the software or workspace where you write and execute SQL commands to communicate with a database.

Think of it like a classroom where you carry out practical exercises. Instead of writing notes in a notebook, you write SQL commands and the database responds to your instructions.

Examples of tools used for working with SQL include:

  • MySQL Workbench: A graphical tool for working with MySQL databases.
  • phpMyAdmin: A web-based tool for managing MySQL and MariaDB databases.
  • DB Browser for SQLite: A tool for exploring SQLite databases.
  • SQL Server Management Studio: A tool for managing Microsoft SQL Server.

2. Understanding MySQL

MySQL is a relational database management system. It helps users store, organise and retrieve information.

Imagine a supermarket in Ibadan that sells rice, beans, cooking oil and other products.

The supermarket can use MySQL to store information about:

  • Products and their prices.
  • Customers and their details.
  • Daily sales.
  • Available stock.
  • Suppliers.

Instead of keeping all this information in paper files, the supermarket can organise it in a database.

3. Connecting to a Database

Before working with a database, you need access to a database system. This usually involves opening a database tool and connecting to a MySQL server.

A MySQL connection may require the following details:

  • Hostname: The address of the database server.
  • Username: The account used to access the database.
  • Password: The secret code used to verify access.
  • Port: The communication port used by the server. MySQL commonly uses port 3306.

For example, a local computer may use localhost as its hostname. Your actual connection details depend on how MySQL is installed and configured.

Important: Never share your database password with strangers. It helps protect your information.

4. Creating Your First Database

A database is like a large digital storage room. It can contain several tables that store related information.

Imagine you want to create a database for a school. You can name it SchoolDB.

Use this SQL command:

CREATE DATABASE SchoolDB;

Let's understand the command:

  • CREATE: Tells the database to create something new.
  • DATABASE: Identifies what you want to create.
  • SchoolDB: The name of the new database.
  • ; Marks the end of the SQL statement.

When the command runs successfully, MySQL creates a database called SchoolDB.

5. Viewing Available Databases

Sometimes, you may want to see all the databases available on your MySQL server.

Use this command:

SHOW DATABASES;

MySQL will display a list of databases you are allowed to see.

6. Selecting a Database

Creating a database does not automatically mean you are working inside it. You must first select the database.

Use the USE command:

USE SchoolDB;

This tells MySQL that you want to work with the SchoolDB database.

Remember: The USE command selects an existing database. It does not create a new one.

7. What Are Data Types?

When creating a database table, you must tell the database what kind of information each column will contain.

This is where data types become important.

A data type tells the database what kind of value can be stored in a particular column.

Common SQL Data Types

Data Type Meaning Example
INT Stores whole numbers. 13, 25, 100
VARCHAR Stores text with a maximum length. Samuel, Lagos
TEXT Stores longer pieces of text. A student's address
DECIMAL Stores numbers with decimal places. 2500.50
DATE Stores dates. 2026-12-01
DATETIME Stores a date and time. 2026-12-01 10:30:00
BOOLEAN Represents true or false values. TRUE or FALSE

For example, a student's age can be stored using INT because it is a whole number. The student's name can be stored using VARCHAR because it contains text.

8. Creating Your First Table

A database can contain many tables. Each table stores a particular type of information.

Inside our SchoolDB database, let's create a table called Students.

CREATE TABLE Students (
    student_id INT,
    student_name VARCHAR(100),
    age INT,
    class VARCHAR(20)
);

Let's understand what we have created:

  • Students: The name of the table.
  • student_id: Stores the student's identification number.
  • student_name: Stores the student's name.
  • age: Stores the student's age.
  • class: Stores the student's class.

Notice that every column has a name and a data type. This helps MySQL understand the information it should store.

9. Understanding Primary Keys

Imagine your school has two students named Samuel. How would you identify the correct Samuel when searching for their records?

A primary key solves this problem.

A primary key is a column, or combination of columns, that uniquely identifies each record in a database table.

For our Students table, student_id can serve as the primary key. Every student should have a different ID.

CREATE TABLE Students (
    student_id INT PRIMARY KEY,
    student_name VARCHAR(100),
    age INT,
    class VARCHAR(20)
);

The PRIMARY KEY rule prevents two students from having the same student_id and does not allow the ID to be NULL.

10. Understanding AUTO_INCREMENT

What if you do not want to type a new student ID every time you add a student?

MySQL provides a feature called AUTO_INCREMENT. It automatically generates the next number for a column when a new record is inserted.

CREATE TABLE Students (
    student_id INT AUTO_INCREMENT PRIMARY KEY,
    student_name VARCHAR(100),
    age INT,
    class VARCHAR(20)
);

If the first student receives ID 1, the next student may automatically receive ID 2, followed by ID 3.

11. Inserting Records into a Table

Creating a table gives you an empty structure. You still need to add information to it.

The INSERT INTO command is used to add new records.

Example:

INSERT INTO Students
(student_name, age, class)
VALUES
('Samuel', 13, 'JSS 2');

This command adds a student named Samuel, who is 13 years old and belongs to JSS 2.

Because student_id uses AUTO_INCREMENT, MySQL generates the ID automatically.

Adding More Than One Student

INSERT INTO Students
(student_name, age, class)
VALUES
('Samuel', 13, 'JSS 2'),
('Mary', 12, 'JSS 1'),
('Peter', 14, 'JSS 3'),
('Esther', 13, 'JSS 2');

This single SQL command inserts four student records into the Students table.

12. Viewing the Tables in Your Database

After creating your Students table, you may want to see all the tables inside your selected database.

Use this command:

SHOW TABLES;

MySQL will display the names of the tables in your current database.

13. Viewing Information Stored in a Table

You can use the SELECT command to view the records you inserted.

SELECT * FROM Students;

The database will display all the columns and records in the Students table.

14. Your First Complete SQL Practice

Now let's put everything together. Imagine you are creating a database for a secondary school in Ibadan.

Follow these commands in order:

-- Step 1: Create a database
CREATE DATABASE SchoolDB;

-- Step 2: Select the database
USE SchoolDB;

-- Step 3: Create a Students table
CREATE TABLE Students (
    student_id INT AUTO_INCREMENT PRIMARY KEY,
    student_name VARCHAR(100),
    age INT,
    class VARCHAR(20)
);

-- Step 4: Insert student records
INSERT INTO Students
(student_name, age, class)
VALUES
('Samuel', 13, 'JSS 2'),
('Mary', 12, 'JSS 1'),
('Peter', 14, 'JSS 3'),
('Esther', 13, 'JSS 2');

-- Step 5: View the student records
SELECT * FROM Students;

Congratulations! You have created a database, created a table, inserted student records and retrieved the information.

15. Important SQL Safety Tips

  • Always check that you are working in the correct database.
  • Use meaningful names for databases and tables.
  • Choose suitable data types for each column.
  • Use primary keys to identify records uniquely.
  • Never share database passwords with unauthorised people.
  • Back up important databases regularly.
  • Practise with sample data before working on real business records.

Module Summary

In this module, you have learned:

  • What an SQL environment is.
  • How MySQL helps manage databases.
  • How to create a database using CREATE DATABASE.
  • How to select a database using USE.
  • How to view databases using SHOW DATABASES.
  • What SQL data types are.
  • How to create tables using CREATE TABLE.
  • What primary keys and AUTO_INCREMENT mean.
  • How to insert records using INSERT INTO.
  • How to view tables and their records.

Knowledge Check

Answer the following questions to test your understanding.

  1. What is an SQL environment?
  2. What is MySQL?
  3. Mention three tools used to work with SQL databases.
  4. Which SQL command creates a new database?
  5. Which command selects an existing database?
  6. What is a data type?
  7. Which data type can be used to store a student's name?
  8. What is the purpose of a primary key?
  9. What does AUTO_INCREMENT do?
  10. Which SQL command adds new records to a table?
  11. Which command displays the tables in a database?
  12. What does SELECT * FROM Students; do?

Practical Assignment

Your assignment is to create a database for a small supermarket. Name the database SupermarketDB.

Create a table called Products with these columns:

  • product_id
  • product_name
  • price
  • quantity

Use suitable data types for each column.

Then complete the following tasks:

  1. Create the SupermarketDB database.
  2. Select the database.
  3. Create the Products table.
  4. Insert at least five products.
  5. Display all the products in the table.

Challenge: Can you add a primary key and AUTO_INCREMENT to the product_id column?

Key Terms to Remember

  • SQL Environment: A workspace for writing and running SQL commands.
  • MySQL: A relational database management system.
  • CREATE DATABASE: Creates a new database.
  • USE: Selects a database.
  • Data Type: Defines the kind of information a column can store.
  • CREATE TABLE: Creates a new table.
  • PRIMARY KEY: Uniquely identifies each record.
  • AUTO_INCREMENT: Automatically generates sequential numbers.
  • INSERT INTO: Adds new records to a table.
  • SELECT: Retrieves information from a database.

Congratulations!

You have completed Module 2. You now know how to create a database, build tables and insert information using SQL.

In Module 3, you will learn how to retrieve specific information from databases using the SELECT statement. This is one of the most important skills used by data analysts.

CRMNuggets โ€“ Learn. Practice. Get Certified.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’