Skip to main content

🗄️ 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

TermWhat It MeansAnalogy
DatabaseCollection of related tablesAn Excel workbook
TableStructured data with rows and columnsAn Excel sheet
Row (Record)A single entry in a tableOne row in Excel
Column (Field)An attribute/propertyA column in Excel
Primary KeyUnique identifier for each rowAadhaar number — unique per person
Foreign KeyLinks one table to anotherA reference number connecting two sheets
SchemaBlueprint/structure of a databaseThe 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_idnamecitysegmentsignup_date
1Rajesh KumarDelhiCorporate2023-01-15
2Priya SinghMumbaiConsumer2023-02-20
3Amit PatelAhmedabadCorporate2023-03-10
4Sneha GuptaDelhiHome Office2023-04-05
5Vikram JoshiBangaloreConsumer2023-05-12

Table: orders

order_idcustomer_idorder_dateamountproduct_category
10112023-06-015000Technology
10222023-06-153000Furniture
10312023-07-018000Technology
10432023-07-202000Supplies
10552023-08-1012000Technology
10622023-08-151500Supplies
10742023-09-014500Furniture

Table: products

product_idproduct_namecategoryprice
P1LaptopTechnology45000
P2DeskFurniture12000
P3Pen SetSupplies500
P4MonitorTechnology20000
P5ChairFurniture8000

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 bhi SELECT * 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

OperatorMeaningExample
=Equal toWHERE city = 'Delhi'
!= or <>Not equal toWHERE city != 'Delhi'
>Greater thanWHERE amount > 5000
<Less thanWHERE amount \< 3000
>=Greater than or equalWHERE amount >= 5000
<=Less than or equalWHERE amount \<= 3000
BETWEENIn a range (inclusive)WHERE amount BETWEEN 2000 AND 8000
INMatches any value in a listWHERE city IN ('Delhi', 'Mumbai')
LIKEPattern matchingWHERE name LIKE 'A%' (starts with A)
IS NULLIs empty/missingWHERE phone IS NULL
IS NOT NULLIs not emptyWHERE phone IS NOT NULL

LIKE patterns:

PatternMeaningExample
'A%'Starts with AAmit, Anita
'%a'Ends with aPriya, Sneha
'%kumar%'Contains "kumar"Rajesh Kumar
'_mit'4 chara