SQL Short Notes
SQL Handbook — Complete Guide for Beginners (Part 1) 📘
A complete Part-1 SQL handbook — from Introduction all the way to Subqueries & Set Operations. Every command, example, and table explained in Plain English so beginners can follow along easily. On Data Insights.
•August 15, 2026•⏱️ 24 min read•🗄️ SQL
🔑 Key Takeaways
SQL (Structured Query Language) is the standard language used to create, modify, query, and manage relational databases.
There are 5 types of SQL commands: DDL, DML, DQL, DCL, TCL — each for a different job.
Core operations: INSERT (add data), UPDATE (change data), DELETE (remove data), SELECT (retrieve data).
For filtering use
WHERE,LIKE,IN,BETWEEN; for grouping useGROUP BY+HAVING.To combine multiple tables use Joins (INNER, LEFT, RIGHT, FULL OUTER), and for nested queries use Subqueries.
📑 What You'll Learn in This Blog:
1. Introduction to SQL 🟢
📘 What is SQL? SQL (Structured Query Language) is a standard programming language used to create, modify, query, and manage relational databases.
🎯 In Simple Terms: Imagine a company stores all of its data (employees, sales, products) inside a big digital cabinet called a database. SQL is the language you use to talk to that cabinet — you ask it, "give me all the employees whose salary is more than 50,000," and SQL pulls exactly that data out for you.
Why SQL? — Why It Matters
SQL is used to store data, retrieve data, update data, and perform various operations on relational databases. Without SQL, you simply cannot get data out of a database — which is why SQL is considered the single most important skill for a Data Analyst.
Database, DBMS & RDBMS — The Difference
Term | Meaning |
|---|---|
Database | A collection of related data (e.g., a list of all employees in one file). |
DBMS | Software that manages the database (Database Management System). |
RDBMS | Relational Database Management System — data is stored in tables (rows & columns). |
SQL Architecture — How It Works
⚡ Flow: User sends a request → SQL Interface processes it → SQL Server interacts with the database → The result is returned to the user.
👤 Client / User
Sends the request
🖥️ SQL Interface
Processes the query
🗄️ SQL Server
Interacts with database
📤 Result / Output
Returned to the user
Key Benefits of SQL
✅ Easy to learn — reads almost like plain English
✅ Data integrity — data stays accurate and consistent
✅ Data security — access can be controlled
✅ Reduced redundancy — less duplicated data
✅ Standard language — works across every RDBMS
✅ Easy data retrieval — millions of rows in seconds
Types of SQL Commands — 5 Categories
Type | Full Form | Commands |
|---|---|---|
DDL | Data Definition Language |
|
DML | Data Manipulation Language |
|
DQL | Data Query Language |
|
DCL | Data Control Language |
|
TCL | Transaction Control Language |
|
2. Database, Tables & Data Types 🟢
Creating a database and tables is the first step in SQL. First you create a database, then tables inside it, and for each column you choose the right data type.
Create Database
SQL
CREATE DATABASE CompanyDB;Create Table
Let's create an Employee table with 6 columns — each with its own data type:
SQL
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Dept VARCHAR(30),
Salary DECIMAL(10,2),
JoinDate DATE,
IsActive BOOLEAN
);Column Name | Data Type | Description |
|---|---|---|
EmpID |
| Employee ID (Primary Key) |
Name |
| Employee Name |
Dept |
| Department Name |
Salary |
| Employee Salary |
JoinDate |
| Date of Joining |
IsActive |
| Employee Active Status |
Common Data Types
Data Type | Use |
|---|---|
| Integer numbers (1, 100, 9999) |
| String — up to n characters |
| Decimal — m total digits, d digits after the decimal point |
| Date values (2024-01-15) |
| True / False values |
📘 Primary Key: A column (or combination of columns) that uniquely identifies each row in a table. For example, EmpID — every employee has a different ID, and it can never be duplicated.
Example Table: Employee
EmpID | Name | Dept | Salary | JoinDate | IsActive |
|---|---|---|---|---|---|
1 | Rahul | IT | 55000.00 | 2024-01-15 | TRUE |
2 | Priya | HR | 45000.00 | 2024-02-10 | TRUE |
3 | Vikram | Finance | 50000.00 | 2024-03-12 | TRUE |
3. INSERT, UPDATE & DELETE 🟡
Once a table is created, you'll learn how to add data (INSERT), change it (UPDATE), and remove it (DELETE). These three DML commands are used by every analyst on a daily basis.
INSERT — Add Data
Syntax
INSERT INTO TableName (Column1, Column2, ...)
VALUES (Value1, Value2, ...);Example — add a new employee, Sneha
INSERT INTO Employee (EmpID, Name, Dept, Salary, JoinDate, IsActive)
VALUES (4, 'Sneha', 'IT', 60000, '2024-04-18', TRUE);UPDATE — Change Data
Syntax
UPDATE TableName
SET Column1 = Value1, Column2 = Value2, ...
WHERE Condition;Example — set Priya's (EmpID 2) salary to 60000
UPDATE Employee
SET Salary = 60000
WHERE EmpID = 2;⚠️ Important: Always use WHERE with UPDATE! Without a WHERE clause, every single row in the table gets updated — this is the most common mistake beginners make.
DELETE — Remove Data
Example — delete the row with EmpID 3
DELETE FROM Employee
WHERE EmpID = 3;TRUNCATE — Remove All Data at Once
Syntax
TRUNCATE TABLE TableName;📘 TRUNCATE: It removes all rows from the table but keeps the table structure intact. The difference between DELETE and TRUNCATE comes up in almost every interview.
DELETE | TRUNCATE | |
|---|---|---|
Rows | Specific (with WHERE) | All rows |
Structure | Kept | Kept |
Rollback | Possible | Generally not |
Speed | Slower | Faster |
4. SELECT & Filtering 🟡
📘 SELECT: The command used to retrieve (pull out) data from a database. It's the most used SQL command — and it belongs to the DQL category.
Basic SELECT
SQL
-- Return all columns
SELECT * FROM Employee;
-- Only employees with salary greater than 50000
SELECT * FROM Employee WHERE Salary > 50000;
-- Unique departments
SELECT DISTINCT Dept FROM Employee;
-- Order by salary ascending
SELECT * FROM Employee ORDER BY Salary ASC;
SELECT * FROM Employee ORDER BY Salary DESC;
-- Only 3 rows
SELECT * FROM Employee LIMIT 3; -- MySQL
SELECT TOP 3 * FROM Employee; -- SQL ServerFiltering Operators — BETWEEN, IN, LIKE
SQL
-- Salary between 40000 and 60000
SELECT * FROM Employee WHERE Salary BETWEEN 40000 AND 60000;
-- Employees in IT or HR department
SELECT * FROM Employee WHERE Dept IN ('IT', 'HR');
-- Pattern matching with LIKE
SELECT * FROM Employee WHERE Name LIKE 'S%'; -- starts with S
SELECT * FROM Employee WHERE Name LIKE '%l'; -- ends with l
SELECT * FROM Employee WHERE Name LIKE '_i%'; -- second letter is i🎯 In Simple Terms: % means "anything" (any number of characters) and _ means "exactly one character." For example, 'S%' = all names starting with S (Sanjay, Sneha), and '_i%' = any name whose second letter is 'i' (like Priya, Vikram).
Sample Employee Table
EmpID | Name | Dept | Salary | JoinDate |
|---|---|---|---|---|
1 | Rahul | IT | 55000 | 2024-01-15 |
2 | Priya | HR | 45000 | 2024-02-10 |
3 | Vikram | Finance | 50000 | 2024-03-12 |
4 | Sneha | IT | 60000 | 2024-04-18 |
5 | Sanjay | HR | 35000 | 2024-05-20 |
5. SQL Functions 🟡
📘 Definition: SQL Functions are predefined formulas that perform a calculation and return a single value. There are two types: Aggregate (work on multiple rows) and Scalar (work on a single value).
A. Aggregate Functions
Function | What It Does |
|---|---|
| Counts the number of rows |
| Returns the total |
| Returns the average |
| Returns the minimum value |
| Returns the maximum value |
Aggregate Examples
-- Total number of employees
SELECT COUNT(*) AS TotalEmployees FROM Employee; -- 5
-- Total salary
SELECT SUM(Salary) AS TotalSalary FROM Employee; -- 245000
-- Average salary
SELECT AVG(Salary) AS AverageSalary FROM Employee; -- 49000
-- Min & Max salary
SELECT MIN(Salary) AS MinSalary FROM Employee; -- 35000
SELECT MAX(Salary) AS MaxSalary FROM Employee; -- 60000B. Scalar Functions
Function | What It Does |
|---|---|
| Converts to uppercase |
| Converts to lowercase |
| Returns the length of a string |
| Rounds a number |
| Joins two or more strings together |
Scalar Examples
SELECT UPPER(Name) AS NameUpper FROM Employee; -- RAHUL
SELECT LOWER(Name) AS NameLower FROM Employee; -- rahul
SELECT Name, LENGTH(Name) AS Len FROM Employee; -- Rahul → 5
SELECT CONCAT(Name, '-', Dept) AS Info FROM Employee; -- Rahul-IT
SELECT ROUND(AVG(Salary), 2) AS AvgSal FROM Employee; -- 49000.00💡 Note: Aggregate functions work on a set of rows and return one value, while Scalar functions work on individual values.
6. GROUP BY & HAVING 🟡
📘 GROUP BY: Groups together rows that have the same values (e.g., department-wise). 📘 HAVING: Filters those groups after they've been created.
GROUP BY Example
How many employees in each department?
SELECT Dept, COUNT(*) AS TotalEmployees
FROM Employee
GROUP BY Dept;Dept | TotalEmployees |
|---|---|
Finance | 1 |
HR | 2 |
IT | 2 |
HAVING Example
Show only the departments that have more than 1 employee:
SQL
SELECT Dept, COUNT(*) AS TotalEmployees
FROM Employee
GROUP BY Dept
HAVING COUNT(*) > 1;Dept | TotalEmployees |
|---|---|
IT | 2 |
HR | 2 |
WHERE vs HAVING — Important Difference
WHERE | HAVING |
|---|---|
Filters rows before grouping | Filters groups after grouping |
Cannot be used with aggregate functions | Can be used with aggregate functions |
Works on individual rows | Works on grouped results |
Used with SELECT, UPDATE, DELETE | Used only with GROUP BY |
Average salary by department
SELECT Dept, AVG(Salary) AS AvgSalary
FROM Employee
GROUP BY Dept; -- IT:57500, HR:40000, Finance:500007. SQL Joins 🔴
📘 Joins: Combine rows from two or more tables based on a related column. In the real world, data is always spread across multiple tables — Joins bring it together.
🎯 In Simple Terms: One table holds employees (Name, DeptID) and another holds departments (DeptID, DeptName). Both share a common column — DeptID. By joining them, you can answer questions like "which department does Rahul belong to?" — because Rahul's DeptID matches the IT department.
EmpID | Name | DeptID |
|---|---|---|
1 | Rahul | 10 |
2 | Priya | 20 |
3 | Vikram | 10 |
4 | Sneha | 30 |
5 | Sanjay | NULL |
DeptID | DeptName |
|---|---|
10 | IT |
20 | HR |
30 | Finance |
40 | Marketing |
1) INNER JOIN — Only Matching Rows
SQL
SELECT Emp.EmpID, Emp.Name, Dept.DeptName
FROM Employee Emp
INNER JOIN Department Dept
ON Emp.DeptID = Dept.DeptID;EmpID | Name | DeptName |
|---|---|---|
1 | Rahul | IT |
2 | Priya | HR |
3 | Vikram | IT |
4 | Sneha | Finance |
ℹ️ Sanjay (DeptID NULL) and the Marketing dept (40) are left out — they had no match.
2) LEFT JOIN — All Rows from the Left Table
SQL
SELECT Emp.EmpID, Emp.Name, Dept.DeptName
FROM Employee Emp
LEFT JOIN Department Dept
ON Emp.DeptID = Dept.DeptID;EmpID | Name | DeptName |
|---|---|---|
1 | Rahul | IT |
2 | Priya | HR |
3 | Vikram | IT |
4 | Sneha | Finance |
5 | Sanjay | NULL |
ℹ️ All employees appear — Sanjay had no match, so his DeptName is NULL.
3) RIGHT JOIN — All Rows from the Right Table
SQL
SELECT Emp.EmpID, Emp.Name, Dept.DeptName
FROM Employee Emp
RIGHT JOIN Department Dept
ON Emp.DeptID = Dept.DeptID;ℹ️ All departments appear — Marketing (40) has no employee, so its EmpID/Name come back as NULL.
4) FULL OUTER JOIN — All Rows from Both Tables
SQL
SELECT Emp.EmpID, Emp.Name, Dept.DeptName
FROM Employee Emp
FULL OUTER JOIN Department Dept
ON Emp.DeptID = Dept.DeptID;ℹ️ Every row from both tables appears — unmatched ones show NULL. Both Sanjay (NULL) and Marketing (NULL) are included.
💡 Note: INNER JOIN returns only matched rows. LEFT / RIGHT / FULL JOIN return NULL for unmatched rows.
8. Subqueries & Set Operations 🔴
A. Subqueries
📘 Subquery: A query nested inside another query. There are 3 types: Single Row, Multiple Row, and Correlated.
1) Single Row Subquery (returns one value)
Employee with the highest salary
SELECT Name, Salary
FROM Employee
WHERE Salary = (SELECT MAX(Salary) FROM Employee);2) Multiple Row Subquery (returns multiple values)
Employees who belong to the IT department
SELECT Name, Dept
FROM Employee
WHERE Dept IN (SELECT Dept FROM Department WHERE DeptName = 'IT');3) Correlated Subquery (depends on the outer query)
Employees earning more than their department's average
SELECT E.Name, E.Salary
FROM Employee E
WHERE E.Salary > (SELECT AVG(Salary)
FROM Employee
WHERE Dept = E.Dept);B. Set Operations
📘 Set Operations: Combine the results of two or more SELECT statements. Both queries must have the same number of columns and matching data types.
Operator | What It Does |
|---|---|
| All distinct rows from both queries |
| All rows including duplicates |
| Only the common rows |
| Rows from the first query that are not in the second |
Set Operation Examples
-- UNION: all unique names from IT + HR
SELECT Name FROM Employee WHERE Dept = 'IT'
UNION
SELECT Name FROM Employee WHERE Dept = 'HR';
-- INTERSECT: names common to both
SELECT Name FROM Employee WHERE Dept = 'IT'
INTERSECT
SELECT Name FROM Employee WHERE Dept = 'HR';💡 Notes: Both queries must have the same number of columns and the same data types, in the same order. UNION removes duplicates, while UNION ALL keeps them (and is faster).
🎉 That's it for Part-1! Happy Learning!
💬 Interview Questions
Q1. How many types of SQL commands are there?
A: 5 types — DDL (CREATE, ALTER, DROP, TRUNCATE), DML (INSERT, UPDATE, DELETE), DQL (SELECT), DCL (GRANT, REVOKE), TCL (COMMIT, ROLLBACK, SAVEPOINT).
Q2. What is the difference between DELETE and TRUNCATE?
A: DELETE removes specific rows (with a WHERE clause) and can be rolled back. TRUNCATE removes all rows at once, is faster, and generally cannot be rolled back. In both cases, the table structure is kept.
Q3. What is the difference between WHERE and HAVING?
A: WHERE filters rows before grouping and cannot be used with aggregate functions. HAVING filters groups after grouping and is used with aggregate functions.
Q4. What is a Primary Key?
A: A column or combination of columns that uniquely identifies each row in a table — it must be unique and NOT NULL.
Q5. What is the difference between INNER JOIN and LEFT JOIN?
A: INNER JOIN returns only the matching rows from both tables. LEFT JOIN returns all rows from the left table, with NULL on the right side where there is no match.
Q6. What is the difference between UNION and UNION ALL?
A: UNION removes duplicates, while UNION ALL keeps them. UNION ALL is faster.
Q7. How are % and _ used with LIKE?
A: % matches any number of characters (including zero), and _ matches exactly one character. For example, 'S%' = names starting with S, and '_i%' = names whose second letter is 'i'.
📋 Quick Cheat Sheet
┌─ SQL COMMANDS ─────────────────────────────────┐
DDL → CREATE, ALTER, DROP, TRUNCATE (structure)
DML → INSERT, UPDATE, DELETE (data change)
DQL → SELECT (retrieve)
DCL → GRANT, REVOKE (permissions)
TCL → COMMIT, ROLLBACK, SAVEPOINT (transactions)
┌─ FILTERING ───────────────────────────────────┐
WHERE → filter rows (before grouping)
LIKE → pattern ( % any, _ one )
IN → match multiple values
BETWEEN → range
ORDER BY→ sort (ASC / DESC)
LIMIT → restrict rows
┌─ AGGREGATION ──────────────────────────────────┐
GROUP BY → group rows
HAVING → filter groups (after GROUP BY)
COUNT, SUM, AVG, MIN, MAX
┌─ JOINS ────────────────────────────────────────┐
INNER → matched only
LEFT → all left + matched right
RIGHT → all right + matched left
FULL → all from both
┌─ ADVANCED ─────────────────────────────────────┐
SUBQUERY → query inside a query
UNION / UNION ALL / INTERSECT / EXCEPT
ORDER OF EXECUTION:
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
🔥 SQL Part-2 Is Coming Soon!
Window Functions, CTEs, Indexes, and Advanced SQL — coming shortly. Bookmark this so you don't miss it.
💬 Comments (0)
Loading comments...