# SQL intro

### What is SQL?

* **SQL** means **Structured Query Language**.
    
* It is used to **work with data in databases**.
    
* With SQL, you can **get**, **add**, **change**, and **remove** data in a database.
    
* SQL became a standard language in **1986 (ANSI)** and **1987 (ISO)**.
    

---

### What Can SQL Do?

* SQL can **ask questions** (queries) to get data from a database.
    
* It can **retrieve** (fetch) data.
    
* It can **insert** (add) new data.
    
* It can **update** existing data.
    
* It can **delete** unwanted data.
    
* It can **create** a new database.
    
* It can **create tables** in a database.
    
* It can create **stored procedures** (pre-written blocks of code).
    
* It can create **views** (virtual tables made from other tables).
    
* It can **set permissions** (control who can do what with the data).
    

---

### SQL is a Standard, But...

* Even though SQL is a standard language, **different database companies** (like MySQL, Oracle, etc.) have **slightly different versions**.
    
* They all support the **main commands** like SELECT, INSERT, DELETE, etc.
    
* But they may also have **extra features** specific to their own software.
    

---

### Using SQL on a Website

If you want your website to show data from a database, you’ll need:

1. A **database program** (like MySQL, SQL Server, MS Access).
    
2. A **server-side language** (like PHP or ASP) to run SQL commands.
    
3. **SQL** to get the data from the database.
    
4. **HTML and CSS** to display and style the data on the web page.
    

---

### What is RDBMS?

* **RDBMS** means **Relational Database Management System**.
    
* It is the system behind all modern databases like MySQL, Oracle, and MS SQL Server.
    
* Data in RDBMS is stored in **tables**.
    

---

### What is a Table?

* A **table** is like an Excel sheet.
    
* It has **columns (fields)** and **rows (records)**.
    
* Each **column** holds one type of information (like name, address, etc.).
    
* Each **row** is one complete entry (like one customer).
    

Example table: `Customers`  
It may have columns like:

* CustomerID
    
* CustomerName
    
* ContactName
    
* Address
    
* City
    
* PostalCode
    
* Country
    

---

### In Short:

* A **field** is a column in a table.
    
* A **record** is a row in a table.
    
* A **column** is vertical — it holds the same kind of data for all records.
    
* A **row** is horizontal — it shows all the data for one person or item.
    

### 💬 SQL Statements — What Are They?

* Most things you want to do with a database are done using **SQL statements**.
    
* These are **commands written in English-like keywords**, so they’re not hard to read.
    

---

### ✅ Example

This SQL command shows **all the data** from a table named **"Customers"**:

```sql
SELECT * FROM Customers;
```

* `SELECT` means **get data**.
    
* `*` means **all columns**.
    
* `FROM Customers` means **from the Customers table**.
    

---

### 📘 What You Will Learn

In this tutorial, you'll learn about many different **types of SQL statements** and how to use them to work with data.

---

### 🗃️ What Are Database Tables?

* A **database** usually has one or more **tables**.
    
* Each **table** has a **name** (like `Customers` or `Orders`).
    
* Tables store **data in rows and columns**.
    

For example, this is part of a `Customers` table:

| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
| --- | --- | --- | --- | --- | --- | --- |
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |

* This table has **5 rows** (one for each customer).
    
* It has **7 columns**: `CustomerID`, `CustomerName`, `ContactName`, `Address`, `City`, `PostalCode`, and `Country`.
    

---

### 📌 Things to Remember

* **SQL commands are not case-sensitive**:  
    Writing `select` or `SELECT` means the same thing.  
    (But in tutorials, we use **capital letters** for keywords to make them easier to see.)
    
* **Semicolon** `;` at the end of SQL commands:
    
    * Some databases need it, some don’t.
        
    * It is used to **separate multiple SQL commands**.
        
    * In this tutorial, we will use semicolons.
        

---

### 📋 Most Common SQL Commands

| Command | What It Does |
| --- | --- |
| `SELECT` | Gets data from a table |
| `UPDATE` | Changes existing data |
| `DELETE` | Removes data from a table |
| `INSERT INTO` | Adds new data to a table |
| `CREATE DATABASE` | Makes a new database |
| `ALTER DATABASE` | Changes a database |
| `CREATE TABLE` | Makes a new table |
| `ALTER TABLE` | Changes an existing table |
| `DROP TABLE` | Deletes a table |
| `CREATE INDEX` | Creates an index (used for faster search) |
| `DROP INDEX` | Deletes an index |

---

### ❓Practice Question:

**Which SQL command shows all records from the table "Customers"?**

A. `SELECT FROM Customers;`  
B. `SELECT ALL FROM Customers;`  
C. ✅ `SELECT * FROM Customers;`  
D. `GET ALL FROM Customers;`

✔️ **Correct Answer: C**

## 🟡 What is the `SELECT` Statement in SQL?

* The `SELECT` statement is used to **get data** from a table in a database.
    
* It tells the database: “**Show me these columns** from this table.”
    

---

### ✅ Example

Let’s say you want to get only the **Customer Name** and **City** from the `Customers` table:

```sql
SELECT CustomerName, City FROM Customers;
```

---

### 🧾 Syntax of SELECT:

```sql
SELECT column1, column2, ...
FROM table_name;
```

* `column1, column2`: The names of the columns you want to see.
    
* `table_name`: The name of the table you're selecting from.
    

---

### 📊 Sample Table: `Customers`

| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
| --- | --- | --- | --- | --- | --- | --- |
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |

---

### 🔄 Select **All Columns**

If you want to get **all the data** from the table (not just specific columns), use:

```sql
SELECT * FROM Customers;
```

* `*` means **"all columns"**.
    
* This command gives you every column and every row from the table.
    

---

## 🟢 INSERT Command in SQL

* The `INSERT INTO` statement is used to **add new data (a row)** to a table.
    

---

### ✅ Example

Add a new customer to the `Customers` table:

```sql
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Singh Store', 'Amit Singh', 'Main Street 21', 'Delhi', '110001', 'India');
```

---

### 🧾 Syntax of INSERT:

```sql
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
```

* `table_name`: The name of the table you're adding to.
    
* `(column1, column2, ...)`: The names of the columns you're inserting data into.
    
* `(value1, value2, ...)`: The actual data you want to add.
    

## 🔷 What is `SELECT DISTINCT` in SQL?

* The `SELECT DISTINCT` command is used when you want to get **only different values** from a column — not repeated ones.
    
* It helps you to **remove duplicates** and get **unique results**.
    

---

### 🎯 Why Use `DISTINCT`?

Imagine a table where many customers are from the same country, like "Mexico".  
If you just want to know **which countries** your customers are from (not how many), you **don't want to see "Mexico" 10 times** — just once.

That’s when you use `DISTINCT`.

---

## ✅ Simple Example

### Table: `Customers`

| CustomerID | CustomerName | Country |
| --- | --- | --- |
| 1 | Alfreds Futterkiste | Germany |
| 2 | Ana Trujillo | Mexico |
| 3 | Antonio Moreno | Mexico |
| 4 | Around the Horn | UK |
| 5 | Berglunds snabbköp | Sweden |

### Normal `SELECT`:

```sql
SELECT Country FROM Customers;
```

📌 This will show:

```sql
Germany  
Mexico  
Mexico  
UK  
Sweden
```

🙁 See? "Mexico" is repeated.

---

### Using `DISTINCT`:

```sql
SELECT DISTINCT Country FROM Customers;
```

✅ This will show:

```sql
Germany  
Mexico  
UK  
Sweden
```

✅ "Mexico" comes only once now. Much cleaner!

---

## 🧾 Easy Syntax

```sql
SELECT DISTINCT column1, column2, ...
FROM table_name;
```

* `DISTINCT` removes rows with **same values** in the selected columns.
    
* You can use it for **one or more columns**.
    

---

## 🧠 Real-life Example

### Let's say you have this student table:

| ID | Name | Class |
| --- | --- | --- |
| 1 | Raj | 10 |
| 2 | Simran | 9 |
| 3 | Amit | 10 |
| 4 | Riya | 8 |

You want to see which **different classes** students are in:

```sql
SELECT DISTINCT Class FROM Students;
```

Result:

```sql
10  
9  
8
```

---

## 🔢 Count How Many Unique Values?

If you want to know **how many different countries** there are in the `Customers` table, use:

```sql
SELECT COUNT(DISTINCT Country) FROM Customers;
```

🎯 This gives you a **number** — the count of unique countries.

---

### ❗ For Microsoft Access Users

Microsoft Access does **not support** `COUNT(DISTINCT column)` directly.  
So instead, use this trick:

```sql
SELECT COUNT(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM Customers);
```

📌 This creates a mini-table of unique countries, and then counts them.

---

## 💡 Summary

| Command | Meaning |
| --- | --- |
| `SELECT Country FROM Customers;` | Shows **all** countries (with duplicates) |
| `SELECT DISTINCT Country FROM Customers;` | Shows **only different** countries |
| `SELECT COUNT(DISTINCT Country)` | Tells you **how many** different countries there are |
| `SELECT DISTINCT column1, column2` |  |
