Chapter 4 — MySQL: Database Foundations
📚 Chapter Overview
Databases are the engine behind data storage. In this chapter, you will learn the basics of SQL (Structured Query Language), create your first database, and master the art of selecting and filtering data.
Learning Objectives:
- Understand RDBMS and Table structures.
- Master
CREATE,INSERT, andSELECT. - Use the
WHEREclause for filtering.
4.1 SQL Basics: CRUD Operations
Part 1 — Definition
SQL is the standard language for dealing with Relational Databases. The most common operations are CRUD: Create (INSERT), Read (SELECT), Update (UPDATE), and Delete (DELETE). Data is stored in Tables consisting of rows and columns.
Part 2 — Syntax
CREATE TABLE students (id INT, name VARCHAR(50));
INSERT INTO students VALUES (1, 'Ali');
SELECT * FROM students;
Part 3 — Example
Problem: Select only the students who are older than 20.
SELECT name, age
FROM students
WHERE age > 20;
Observation: The WHERE clause is the most important tool for filtering large datasets down to what you actually need.
Part 4 — Video
Video: Coming Soon
4.2 Sorting and Distinct Values
Part 1 — Definition
Often, you need to see unique values or sort your data. DISTINCT removes duplicates from your results, and ORDER BY sorts the data ascending (ASC) or descending (DESC).
Part 2 — Syntax
SELECT DISTINCT city FROM customers;
SELECT * FROM orders ORDER BY amount DESC;
Part 3 — Example
Problem: Find all unique regions where we have customers, sorted alphabetically.
SELECT DISTINCT region
FROM customers
ORDER BY region ASC;
Part 4 — Video
Video: Coming Soon
🏆 Chapter Challenge
Challenge Objective
Create and Query a "Product Inventory" database.
Requirements
- Create a table
productswith columns: ID, Name, Category, Price, Stock. - Insert 5 sample products.
- Write a query to find all "Electronics" with a price greater than 500.
Step-by-Step Process
- Run the
CREATE TABLEcommand. - Perform 5
INSERToperations. - Use
SELECT * FROM products WHERE ...to get the result.
Expected Deliverables
- The SQL script (.sql) containing the commands.