🗄️ Round 3 — SQL From Scratch
Complete Guide for Fresher Data Analyst at DecisionTree Analytics
What to expect: 30–45 minutes of SQL questions. You may be asked to write queries on a whiteboard, on paper, or in a live coding environment. SQL is the #1 most tested skill for data analyst roles at DecisionTree.
🧠 Hinglish mein samjho: SQL ek language hai jisse tum database se baat karte ho. Jaise Google pe search karte ho, waise hi SQL se data mein search karte ho. DecisionTree mein roz SQL likhni padti hai — yeh tumhara bread and butter hai.
CHAPTER 1: WHAT IS SQL & WHY DOES IT MATTER?
1.1 The Big Picture
SQL (Structured Query Language, pronounced "sequel") is the language used to talk to databases.
A database is an organized collection of data stored in tables (think of an Excel spreadsheet).
Why SQL for Data Analysts?
- Every company's data is stored in databases (PostgreSQL, MySQL, MS SQL, BigQuery)
- You'll use SQL daily to pull data, clean it, aggregate it, and create reports
- DecisionTree specifically lists SQL as a must-have in all their Data Analyst job postings
- Their stack mentions: PostgreSQL, MySQL, MS SQL
1.2 Key Terminology
| Term | What It Means | Analogy |
|---|---|---|
| Database | Collection of related tables | An Excel workbook |
| Table | Structured data with rows and columns | An Excel sheet |
| Row (Record) | A single entry in a table | One row in Excel |
| Column (Field) | An attribute/property | A column in Excel |
| Primary Key | Unique identifier for each row | Aadhaar number — unique per person |
| Foreign Key | Links one table to another | A reference number connecting two sheets |
| Schema | Blueprint/structure of a database | The design of all your sheets |
1.3 Sample Database for This Guide
We'll use these tables throughout. Imagine this is a DecisionTree client's data:
Table: customers
| customer_id | name | city | segment | signup_date |
|---|---|---|---|---|
| 1 | Rajesh Kumar | Delhi | Corporate | 2023-01-15 |
| 2 | Priya Singh | Mumbai | Consumer | 2023-02-20 |
| 3 | Amit Patel | Ahmedabad | Corporate | 2023-03-10 |
| 4 | Sneha Gupta | Delhi | Home Office | 2023-04-05 |
| 5 | Vikram Joshi | Bangalore | Consumer | 2023-05-12 |
Table: orders
| order_id | customer_id | order_date | amount | product_category |
|---|---|---|---|---|
| 101 | 1 | 2023-06-01 | 5000 | Technology |
| 102 | 2 | 2023-06-15 | 3000 | Furniture |
| 103 | 1 | 2023-07-01 | 8000 | Technology |
| 104 | 3 | 2023-07-20 | 2000 | Supplies |
| 105 | 5 | 2023-08-10 | 12000 | Technology |
| 106 | 2 | 2023-08-15 | 1500 | Supplies |
| 107 | 4 | 2023-09-01 | 4500 | Furniture |
Table: products
| product_id | product_name | category | price |
|---|---|---|---|
| P1 | Laptop | Technology | 45000 |
| P2 | Desk | Furniture | 12000 |
| P3 | Pen Set | Supplies | 500 |
| P4 | Monitor | Technology | 20000 |
| P5 | Chair | Furniture | 8000 |
CHAPTER 2: THE BASICS — SELECT, FROM, WHERE
🧠 Ek line mein samjho: SELECT = kya chahiye, FROM = kahan se, WHERE = konsa waala. Bas yahi SQL ka foundation hai!
2.1 SELECT & FROM — Retrieving Data
SELECT tells the database what columns you want. FROM tells it which table.
-- Saara data do customers table se
SELECT * FROM customers;
-- Sirf naam aur city chahiye (interview mein specific columns likho — impression achha padta hai)
SELECT name, city FROM customers;
Rule:
SELECT *means "give me everything." Interview mein hamesha specific columns likho —SELECT *likhna lazy lagta hai. Interviewer sochega ye banda production mein bhiSELECT *likhega kya? 😅
2.2 WHERE — Filtering Rows
WHERE filters rows based on a condition.
-- Customers from Delhi
SELECT * FROM customers
WHERE city = 'Delhi';
-- Orders above ₹5,000
SELECT * FROM orders
WHERE amount > 5000;
-- Orders in a date range
SELECT * FROM orders
WHERE order_date BETWEEN '2023-07-01' AND '2023-08-31';
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | WHERE city = 'Delhi' |
!= or <> | Not equal to | WHERE city != 'Delhi' |
> | Greater than | WHERE amount > 5000 |
< | Less than | WHERE amount \< 3000 |
>= | Greater than or equal | WHERE amount >= 5000 |
<= | Less than or equal | WHERE amount \<= 3000 |
BETWEEN | In a range (inclusive) | WHERE amount BETWEEN 2000 AND 8000 |
IN | Matches any value in a list | WHERE city IN ('Delhi', 'Mumbai') |
LIKE | Pattern matching | WHERE name LIKE 'A%' (starts with A) |
IS NULL | Is empty/missing | WHERE phone IS NULL |
IS NOT NULL | Is not empty | WHERE phone IS NOT NULL |
LIKE patterns:
| Pattern | Meaning | Example |
|---|---|---|
'A%' | Starts with A | Amit, Anita |
'%a' | Ends with a | Priya, Sneha |
'%kumar%' | Contains "kumar" | Rajesh Kumar |
'_mit' | 4 chara |