# **Beginner-Friendly SQL Course for WAMP Server (MySQL + phpMyAdmin)**
*A step-by-step guide to learning SQL with practical examples on WAMP (Windows, Apache, MySQL,
PHP).*
---
## **1. Introduction to SQL**
### **What is SQL?**
- **SQL (Structured Query Language)** is a programming language used to manage and manipulate
relational databases.
- It allows you to **create, read, update, and delete (CRUD)** data in databases like MySQL.
### **Why is SQL Important?**
- Used in web applications (e.g., WordPress, e-commerce).
- Essential for data analysis and reporting.
- Works with PHP to build dynamic websites.
### **WAMP & phpMyAdmin Setup**
- Install **WAMP Server** (download from [wampserver.com](http://www.wampserver.com/)).
- Open **phpMyAdmin** (`http://localhost/phpmyadmin`).
- We’ll run all SQL queries in the **SQL tab** of phpMyAdmin.
---
## **2. Creating Tables (CREATE TABLE)**
### **Syntax:**
```sql
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
```
### **Example Tables:**
#### **1. Users Table**
```sql
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
age INT,
signup_date DATE DEFAULT CURRENT_DATE
);
```
#### **2. Products Table**
```sql
CREATE TABLE products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2) CHECK (price > 0),
stock INT DEFAULT 0
);
```
#### **3. Orders Table (with Foreign Key)**
```sql
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
product_id INT,
quantity INT,
order_date DATETIME DEFAULT NOW(),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
```
### **Common Mistakes:**
❌ Forgetting `PRIMARY KEY` or `AUTO_INCREMENT`.
❌ Misspelling column names or datatypes.
---
## **3. Retrieving Data (SELECT Queries)**
### **Basic SELECT**
```sql
SELECT * FROM users; -- Gets all columns
SELECT username, email FROM users; -- Specific columns
```
### **Filtering with WHERE**
```sql
SELECT * FROM products WHERE price > 50;
SELECT * FROM users WHERE age >= 18 AND age <= 30;
```
### **Sorting (ORDER BY) & Limiting (LIMIT)**
```sql
SELECT * FROM products ORDER BY price DESC; -- High to low
SELECT * FROM users ORDER BY signup_date LIMIT 5; -- First 5 signups
```
### **Common Mistakes:**
❌ Forgetting `WHERE` before conditions.
❌ Using `=` instead of `LIKE` for text matching.
---
## **4. Modifying Tables (ALTER TABLE)**
### **Add a Column**
```sql
ALTER TABLE users ADD COLUMN phone VARCHAR(15);
```
### **Drop a Column**
```sql
ALTER TABLE users DROP COLUMN age;
```
### **Modify Column Type**
```sql
ALTER TABLE products MODIFY COLUMN price DECIMAL(12, 2);
```
### **Common Mistakes:**
❌ Forgetting `COLUMN` keyword in `ALTER TABLE`.
❌ Changing column types that break existing data.
---
## **5. Updating & Deleting Data**
### **UPDATE (With & Without Conditions)**
```sql
UPDATE users SET email = '[email protected]' WHERE user_id = 1;
UPDATE products SET stock = stock + 10; -- Updates all rows
```
### **DELETE (With & Without Conditions)**
```sql
DELETE FROM orders WHERE order_id = 5; -- Deletes one order
DELETE FROM users; -- Deletes ALL users (be careful!)
```
### **Common Mistakes:**
❌ Forgetting `WHERE` in `UPDATE`/`DELETE` (updates ALL rows!).
❌ Not backing up data before mass deletions.
---
## **6. Filtering Records (WHERE, LIKE, IN, BETWEEN)**
```sql
-- LIKE (partial match)
SELECT * FROM users WHERE username LIKE 'j%'; -- Starts with 'j'
-- IN (multiple possible values)
SELECT * FROM products WHERE product_id IN (1, 3, 5);
-- BETWEEN (range)
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';
```
---
## **7. Aggregate Functions (COUNT, SUM, AVG, MIN, MAX)**
```sql
SELECT COUNT(*) FROM users; -- Total users
SELECT AVG(price) FROM products; -- Average price
SELECT MAX(price) FROM products; -- Most expensive product
```
---
## **8. Joins (INNER, LEFT, RIGHT JOIN)**
### **INNER JOIN (Matching Records Only)**
```sql
SELECT users.username, orders.order_date
FROM users
INNER JOIN orders ON users.user_id = orders.user_id;
```
### **LEFT JOIN (All from Left Table + Matches)**
```sql
SELECT products.name, orders.quantity
FROM products
LEFT JOIN orders ON products.product_id = orders.product_id;
```
### **Common Mistakes:**
❌ Forgetting the `ON` clause (causes a Cartesian product).
❌ Mixing up `LEFT` vs `INNER` JOIN.
---
## **9. GROUP BY & HAVING**
```sql
SELECT user_id, COUNT(*) AS total_orders
FROM orders
GROUP BY user_id
HAVING COUNT(*) > 2; -- Users with >2 orders
```
### **Common Mistakes:**
❌ Using `WHERE` instead of `HAVING` for grouped conditions.
---
## **10. Indexes & Keys**
### **Adding an Index (Speeds Up Searches)**
```sql
CREATE INDEX idx_email ON users(email);
```
### **Primary & Foreign Keys**
- Already used in `CREATE TABLE` examples.
- Ensures data integrity.
### **Common Mistakes:**
❌ Over-indexing (slows down inserts/updates).
---
## **Final Tips**
✅ Always **back up** your database before big changes.
✅ Use `LIMIT` with `UPDATE`/`DELETE` to test first.
✅ Test queries in **phpMyAdmin** before using them in PHP.
---
### **Next Steps:**
- Practice with real datasets.
- Learn **SQL injection prevention** for PHP security.
- Explore **stored procedures** and **triggers**.
Happy querying! 🚀