What You Will Learn in This Lesson
In this lesson, we will cover:
- What SQL is and why it’s important
- How databases, tables, rows, and columns work
- How to install / access a SQL environment
- How to create your first database
- How to create tables we will use throughout this entire SQL course
- How to insert sample data
- How to run your first SELECT queries
This lesson builds the foundation for everything we will learn later.
What is SQL?
SQL (Structured Query Language) is a standard language used to store, retrieve, update, and manage data inside a database.
Whenever you:
- build analytics
- design dashboards
- write ETL pipelines
- work with data warehouses
- do backend development
…you use SQL.
SQL is used in:
- MySQL
- PostgreSQL
- SQL Server
- Oracle
- Snowflake
- BigQuery
- Databricks SQL
Even though each database has small differences, the core SQL syntax is the same.
What is a Database?
A database is a structured container where we store data.
Inside a database, we have:
| Component | Meaning |
|---|---|
| Table | A structured data grid (rows + columns) |
| Row | A single record (e.g., one customer) |
| Column | A data field (e.g., name, email) |
| Primary Key | A unique identifier |
| Foreign Key | Link between related tables |
Setting Up Your SQL Environment
You can run SQL on:
Option 1 — Local MySQL
Download from: https://dev.mysql.com/downloads/
Option 2 — Online SQL Editors
Option 3 — VS Code with SQLite Extension
For this tutorial series, we use MySQL syntax, but everything works on PostgreSQL and SQL Server too.
Create Your Database
Let’s start by creating a database called:datatorials_dbCopy
Run:
Now switch to the database:CREATE DATABASE datatorials_db; CopyUSE datatorials_db; Copy
Tables We Will Use Throughout This Course
We will create 4 core tables that represent a small sales system:
1. customers
Stores customer information.
2. products
List of products.
3. orders
High-level order information.
4. order_items
Each item inside an order.
This structure lets us practice:
- SELECT
- JOIN
- GROUP BY
- Window functions
- Subqueries
- CTEs
- Aggregations
- DML
- DDL
- Real-world reporting
Step 1 — Create the customers Table
CREATE TABLE customers ( customer_id INT PRIMARY KEY AUTO_INCREMENT, full_name VARCHAR(100), email VARCHAR(120), city VARCHAR(50), country VARCHAR(50), signup_date DATE ); Copy
Step 2 — Create the products Table
CREATE TABLE products ( product_id INT PRIMARY KEY AUTO_INCREMENT, product_name VARCHAR(100), category VARCHAR(50), price DECIMAL(10,2) ); Copy
Step 3 — Create the orders Table
CREATE TABLE orders ( order_id INT PRIMARY KEY AUTO_INCREMENT, customer_id INT, order_date DATE, total_amount DECIMAL(12,2), FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ); Copy
Step 4 — Create the order_items Table
CREATE TABLE order_items ( item_id INT PRIMARY KEY AUTO_INCREMENT, order_id INT, product_id INT, quantity INT, price_per_unit DECIMAL(10,2), FOREIGN KEY (order_id) REFERENCES orders(order_id), FOREIGN KEY (product_id) REFERENCES products(product_id) ); Copy
Insert Sample Data
Customers:
INSERT INTO customers (full_name, email, city, country, signup_date) VALUES ('Ali Raza', 'ali@example.com', 'Lahore', 'Pakistan', '2023-01-12'), ('Sara Khan', 'sara@example.com', 'Karachi', 'Pakistan', '2023-03-22'), ('John Doe', 'john@example.com', 'New York', 'USA', '2023-02-10'); Copy
Products:
INSERT INTO products (product_name, category, price) VALUES ('Laptop', 'Electronics', 1200.00), ('Keyboard', 'Electronics', 45.00), ('Mobile Phone', 'Electronics', 900.00), ('Office Chair', 'Furniture', 150.00); Copy
Orders:
INSERT INTO orders (customer_id, order_date, total_amount) VALUES (1, '2023-05-12', 1245.00), (2, '2023-05-15', 900.00), (3, '2023-05-20', 150.00); Copy
Order Items:
INSERT INTO order_items (order_id, product_id, quantity, price_per_unit) VALUES (1, 1, 1, 1200.00), (1, 2, 1, 45.00), (2, 3, 1, 900.00), (3, 4, 1, 150.00); Copy
Run Your First Query
Try:SELECT * FROM customers; CopySELECT full_name, city, country FROM customers; Copy
Try Your First JOIN
To see orders with customer names:SELECT orders.order_id, customers.full_name, orders.order_date, orders.total_amount FROM orders JOIN customers ON orders.customer_id = customers.customer_id; Copy
What’s Next?
In Lesson 2, we will learn: