Introduction | SQL Tutorial - Learn with VOKS
Next

Introduction


SQL stands for Structured Query Language. It is the standard language used to communicate with databases.

With SQL, you can:

  • Retrieve data from a database
  • Add new data
  • Update existing data
  • Delete data
  • Create and modify database structures

Think of SQL like a language for talking to data stored in tables.

1️⃣ What Is a Database?

A database is a collection of organized data.

Example: A company stores information about:

  • Employees
  • Customers
  • Orders
  • Products

2️⃣ SQL Basics: Tables

Data in a database is stored in tables.

A table is like a spreadsheet:

| EmployeeID | Name       | Department | Salary |
|------------|------------|------------|--------|
| 1          | John       | Sales      | 3000   |
| 2          | Mary       | HR         | 3500   |
| 3          | David      | IT         | 4000   |
| 4          | Anna       | Finance    | 3200   |
  • Rows = records
  • Columns = fields

3️⃣ Common SQL Commands

SQL commands are divided into categories:

| Category                           | Purpose                   | Example Command                       |
|-----------------------------------|---------------------------|---------------------------------------|
| DDL (Data Definition Language)     | Define database structure | CREATE TABLE, ALTER TABLE, DROP TABLE |
| DML (Data Manipulation Language)   | Add, update, delete data  | INSERT, UPDATE, DELETE                |
| DQL (Data Query Language)          | Retrieve data             | SELECT                                |
| DCL (Data Control Language)        | Control access            | GRANT, REVOKE                         |
| TCL (Transaction Control Language) | Manage transactions       | COMMIT, ROLLBACK                      |

4️⃣ Retrieving Data: SELECT Statement

The most basic SQL command is SELECT:

SELECT column1, column2
FROM table_name;

Example:

SELECT Name, Salary
FROM Employees;

Output:

| Name  | Salary |
|-------|--------|
| John  | 3000   |
| Mary  | 3500   |
| David | 4000   |
| Anna  | 3200   |

5️⃣ Filtering Data: WHERE Clause

To retrieve specific data, use WHERE:

SELECT Name, Salary
FROM Employees
WHERE Department = 'IT';

Output:

| Name  | Salary |
|-------|--------|
| David | 4000   |

6️⃣ Sorting Data: ORDER BY

Sort results ascending (ASC) or descending (DESC):

SELECT Name, Salary
FROM Employees
ORDER BY Salary DESC;

Output:

| Name  | Salary |
|-------|--------|
| David | 4000   |
| Mary  | 3500   |
| Anna  | 3200   |
| John  | 3000   |

7️⃣ Aggregating Data

SQL can calculate totals, averages, etc., using aggregate functions:

  • COUNT() → count rows
  • SUM() → total
  • AVG() → average
  • MAX() → maximum
  • MIN() → minimum

Example:

SELECT AVG(Salary) AS AverageSalary
FROM Employees;

Output:

| AverageSalary |
|---------------|
| 3425          |

8️⃣ Grouping Data: GROUP BY

Use GROUP BY to summarize data by category.

Example: Average salary per department:

SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department;

Output:

| Department | AvgSalary |
|------------|-----------|
| Sales      | 3000      |
| HR         | 3500      |
| IT         | 4000      |
| Finance    | 3200      |

9️⃣ Joining Tables

Data is often in multiple tables. Use JOIN to combine them.

Example: Two tables

Employees Table

| EmployeeID | Name  | DepartmentID |
|------------|-------|--------------|
| 1          | John  | 1            |
| 2          | Mary  | 2            |
| 3          | David | 3            |
| 4          | Anna  | 4            |

Departments Table

| DepartmentID | DepartmentName |
|--------------|----------------|
| 1            | Sales          |
| 2            | HR             |
| 3            | IT             |
| 4            | Finance        |

SQL JOIN:

SELECT e.Name, d.DepartmentName
FROM Employees e
JOIN Departments d
ON e.DepartmentID = d.DepartmentID;

Output:

| Name  | DepartmentName |
|-------|----------------|
| John  | Sales          |
| Mary  | HR             |
| David | IT             |
| Anna  | Finance        |

🔟 Inserting Data

Add new records using INSERT:

INSERT INTO Employees (EmployeeID, Name, Department, Salary)
VALUES (5, 'Liam', 'Marketing', 3600);

1️⃣1️⃣ Updating Data

Change existing data using UPDATE:

UPDATE Employees
SET Salary = 3800
WHERE Name = 'Mary';

1️⃣2️⃣ Deleting Data

Remove records using DELETE:

DELETE FROM Employees
WHERE Name = 'Anna';


SQL
Introduction What is a Database, Advantages of Database, and Database Tables Creating Databases, Tables, Constraints, and Keys Defining Data Types, Unique ID, Inserting Values, and Handling NULL SELECT, DISTINCT, TOP, LIMIT, UPDATE, DELETE, ORDER BY, WHERE, HAVING, AND, OR, NOT Aggregate function; Min, Max, Count, Avg, Sum Wildcards, IN, AS, LIKE, BETWEEN, and Aliases SQL Joins: Inner, Outer, Left, Right, Full, Cross Set Theory for SQL: Joins, UNION, INTERSECT, EXCEPT, GROUP BY SQL Arithmetic Operators SQL Bitwise Operators SQL Comparison Operator SQL View/Null Functions SQL Comments SQL Case
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL