8️⃣ Understanding Relationships
The relationship is:
Customers
Customer_ID
↓
Orders
One customer can have multiple orders.
For example:
John
↓
Order 5001
Order 5003
Order 5010
This is a:
One-to-Many relationship
It's one of the most important database concepts for Data Analysts.
9️⃣ What Is a Relational Database?
A relational database stores data in related tables.
For example:
Customers
↓
Orders
↓
Order Details
↓
Products
Instead of storing the customer's name repeatedly in every order, the database can store:
Customer_ID
and retrieve the customer information through relationships.
This helps reduce unnecessary duplication.
🔟 What Is SQL Syntax?
SQL queries generally consist of keywords and expressions.
For example:
SELECT *
FROM Customers;
This asks:
Return all columns from the Customers table.
Let me break it down.
SELECT: Specifies what you want to retrieve.
FROM: Specifies the table.
Customers: The table you're querying.
1️⃣1️⃣ SELECT
SELECT is one of the first SQL commands you need to learn.
Suppose you have:
Employees
Employee_ID Name Department Salary
101 John IT 75,000
102 Sarah HR 60,000
103 Mike Finance 82,000
To retrieve all columns:
SELECT *
FROM Employees;
1️⃣2️⃣ Selecting Specific Columns
You don't always need every column.
Suppose you only want:
Name and Department
Use:
SELECT Name, Department
FROM Employees;
Result:
Name Department
John IT
Sarah HR
Mike Finance
This is generally better than using SELECT * when you only need specific fields.
1️⃣3️⃣ Why Avoid SELECT * in Production Queries?
You may see beginners writing:
SELECT *
FROM Employees;
all the time.
It's useful while learning and exploring data.
But in production queries, explicitly selecting the required columns is often better because:
• It makes the query clearer
• It avoids retrieving unnecessary data
• It can reduce data transfer
• It makes downstream dependencies more predictable
For example:
SELECT Employee_ID, Name, Salary
FROM Employees;
is more intentional.
1️⃣4️⃣ WHERE
WHERE filters records.
Suppose you want employees from IT.
SELECT *
FROM Employees
WHERE Department = 'IT';
Result:
Employee_ID Name Department Salary
101 John IT 75,000
The database only returns records satisfying the condition.
1️⃣5️⃣ Filtering Numeric Values
Suppose you want employees earning more than ₹70,000.
SELECT *
FROM Employees
WHERE Salary > 70000;
Result:
Employee_ID Name Department Salary
101 John IT 75,000
103 Mike Finance 82,000
1️⃣6️⃣ Comparison Operators
You should know these operators:
Operator Meaning
= Equal to
<> Not equal to
Greater than
< Less than
= Greater than or equal
<= Less than or equal
Examples:
WHERE Salary >= 80000
WHERE Department <> 'HR'
1️⃣7️⃣ AND
AND requires all conditions to be true.
Suppose you want:
IT employees earning more than ₹70,000.
SELECT *
FROM Employees
WHERE Department = 'IT'
AND Salary > 70000;
The record must satisfy both conditions.
Think:
IT
AND
Salary > 70,000
1️⃣8️⃣ OR
OR requires at least one condition to be true.
Suppose you want:
IT or Finance employees.