**SQL** is a tool that helps us work with databases (organized
collections of data). It's not a complete programming language like
Python, but instead a "sublanguage" specifically designed for
database tasks. Here are some points from the book in easier words:
1. **What is SQL?**
- It uses commands like SELECT, INSERT, and DELETE to work
with data.
- These commands are divided into two categories:
- **DDL (Data Definition Language):** For creating and
organizing data structure.
- **DML (Data Manipulation Language):** For adding,
changing, and removing data.
2. **Where does SQL come from?**
- IBM created SQL, and it's recognized as a standard by a big
organization called ANSI.
- Many software programs, like MS Access, Oracle, and MySQL,
use SQL.
3. **Features of SQL:**
- It works like English, so it's easier to understand (e.g., "SELECT
name FROM students").
- You only need to say *what* you want, not *how* the computer
should find it.
- It's not sensitive to uppercase or lowercase letters ("select" or
"SELECT" both work).
- SQL handles groups of data (like tables) instead of one piece at a
time.
- People from different roles can use SQL, like database managers
or app developers.
- It lets you perform tasks like:
- Searching data (querying)
- Adding or deleting information
- Designing or changing databases
- Controlling who can access the data
- Keeping the data consistent and error-free
Here’s an example using the **ORDER BY** clause in SQL:
### Table: Employees
Imagine you have this data in a table called **EMP**:
| **ENAME** | **DEPTNO** | **SAL** |
|------------|------------|----------|
| Alice | 10 | 5000 |
| Bob | 20 | 4000 |
| Charlie | 10 | 4500 |
| David | 30 | 3500 |
| Eve | 20 | 3000 |
### Query
You want to:
1. Sort the employees by **DEPTNO** (department number) in
ascending order.
2. For employees in the same department, sort them by **SAL**
(salary) in descending order.
The query would look like this:
```sql
SELECT ENAME, DEPTNO, SAL
FROM EMP
ORDER BY DEPTNO ASC, SAL DESC;
```
### Result
The output would be:
| **ENAME** | **DEPTNO** | **SAL** |
|------------|------------|----------|
| Alice | 10 | 5000 |
| Charlie | 10 | 4500 |
| Bob | 20 | 4000 |
| Eve | 20 | 3000 |
| David | 30 | 3500 |
### Explanation
1. **First Sort by DEPTNO:**
- Employees from department 10 come first, then 20, and finally
30.
2. **Then Sort by SAL within DEPTNO:**
- In department 10, Alice (5000) comes before Charlie (4500).
- In department 20, Bob (4000) comes before Eve (3000).
I hope this example makes the concept clear! 😊