## SQL for Data Analysts — Beginners Guide India 2026
SQL (Structured Query Language) is the language that talks to databases. As a data analyst, almost every dataset you'll work with lives in a database — not in an Excel file. Companies store their sales data, customer records, product inventory, and financial transactions in relational databases like MySQL, PostgreSQL, or Microsoft SQL Server. SQL is how you get that data out and analyze it.
This guide explains the SQL concepts you need as a data analyst, with real examples you can practice immediately.
---
Why Every Data Analyst Needs SQL
The data lives in databases: When a company wants to know "what were our top 10 products by revenue last quarter?" — that data isn't in an Excel file sitting on someone's desktop. It's in a database table. SQL is the only way to query it directly.
Data analysts who know SQL earn more: In India, data analyst job postings that require SQL pay 15–25% more than those that don't. SQL is explicitly listed as a requirement in 70%+ of data analyst job postings.
SQL vs Excel: Excel is excellent for analysis — but it requires the data to already be organized. SQL is how you get the data in the first place, clean it, filter it, and combine multiple data sources. Most real-world analyses start with SQL, then move to Excel or Power BI for visualization.
SQL works everywhere: MySQL, PostgreSQL, SQL Server, SQLite, Oracle, BigQuery, Snowflake, Redshift — all use SQL with minor dialect differences. Learn SQL once and it works in every database environment.
---
Setting Up to Practice SQL (Free)
Option 1 — MySQL (best for learning): - Download MySQL Community Server (free from mysql.com) - Download MySQL Workbench (free GUI to write and run queries) - Create a local database and import a sample dataset (MySQL provides sample datasets like "sakila" — a DVD rental store database)
Option 2 — SQLiteOnline.com: Practice SQL directly in your browser — no installation required. Good for quick practice.
Option 3 — Mode Analytics, HackerRank SQL, LeetCode SQL: Online platforms with SQL problems of increasing difficulty. Great for interview preparation.
---
Core SQL Concepts for Data Analysts
SELECT — Retrieving Data
The foundation of every SQL query:
SELECT * FROM customers;
This retrieves all columns from the "customers" table. In practice, always specify columns instead of using * :
SELECT customer_id, name, city, email FROM customers;
Filtering with WHERE:
SELECT name, city FROM customers WHERE city = 'Amritsar';
SELECT product_name, price FROM products WHERE price > 500;
SELECT * FROM orders WHERE order_date BETWEEN '2026-01-01' AND '2026-03-31';
Sorting results:
SELECT name, revenue FROM products ORDER BY revenue DESC; -- highest first
SELECT name, revenue FROM products ORDER BY revenue ASC; -- lowest first
Limiting results:
SELECT name, revenue FROM products ORDER BY revenue DESC LIMIT 10; -- top 10 by revenue
---
Aggregate Functions — Calculating Summary Statistics
This is where SQL becomes powerful for data analysts:
COUNT — how many: SELECT COUNT(*) FROM orders; SELECT COUNT(DISTINCT customer_id) FROM orders; -- unique customers
SUM — total: SELECT SUM(amount) FROM orders;
AVG — average: SELECT AVG(amount) FROM orders;
MAX and MIN: SELECT MAX(amount), MIN(amount) FROM orders;
---
GROUP BY — The Most Important Analytical Query
GROUP BY splits your data into groups and applies aggregate functions to each group:
Total revenue by city: SELECT city, SUM(revenue) as total_revenue FROM sales GROUP BY city ORDER BY total_revenue DESC;
Count of orders by product: SELECT product_name, COUNT(*) as order_count FROM orders GROUP BY product_name ORDER BY order_count DESC;
Average order value by customer: SELECT customer_id, AVG(order_amount) as avg_order_value FROM orders GROUP BY customer_id ORDER BY avg_order_value DESC;
HAVING — filtering after GROUP BY:
HAVING is like WHERE but for grouped results. WHERE filters rows before grouping; HAVING filters groups after:
SELECT city, COUNT(*) as customer_count FROM customers GROUP BY city HAVING customer_count > 100; -- only cities with more than 100 customers
---
JOINs — Combining Multiple Tables
Real databases have data split across many tables. JOINs combine them:
INNER JOIN — only matching rows in both tables:
SELECT o.order_id, c.name, o.amount FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id;
This combines the orders table with the customers table — so you can see customer names alongside order amounts.
LEFT JOIN — all rows from left table, matching rows from right:
SELECT c.name, o.order_id, o.amount FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;
This returns ALL customers — even those who have never placed an order (their order columns will show NULL).
When to use which JOIN: - INNER JOIN: when you only want rows that have a match in both tables - LEFT JOIN: when you want all rows from the main table regardless of whether they have matching data in the second table
---
Subqueries — Queries Inside Queries
A subquery is a query nested inside another query:
Find customers who have spent more than the average order amount: SELECT customer_id, name FROM customers WHERE customer_id IN ( SELECT customer_id FROM orders WHERE amount > (SELECT AVG(amount) FROM orders) );
The inner SELECT runs first, the outer SELECT uses its results.
---
Common Table Expressions (CTEs) — Making Complex Queries Readable
CTEs (using WITH keyword) make complex queries easier to read and debug:
WITH high_value_customers AS ( SELECT customer_id, SUM(amount) as total_spent FROM orders GROUP BY customer_id HAVING total_spent > 10000 ) SELECT c.name, h.total_spent FROM customers c INNER JOIN high_value_customers h ON c.customer_id = h.customer_id ORDER BY h.total_spent DESC;
---
SQL for Data Analysts — Real Scenarios
Scenario 1 — Marketing campaign analysis: "Which marketing channels brought in the most high-value customers (those who spent over ₹5,000)?"
SELECT m.channel, COUNT(DISTINCT o.customer_id) as customers, SUM(o.amount) as revenue FROM orders o JOIN marketing_attribution m ON o.customer_id = m.customer_id WHERE o.amount > 5000 GROUP BY m.channel ORDER BY revenue DESC;
Scenario 2 — Cohort analysis (first month retention): "Of customers who joined in January 2026, how many made a second purchase in February?"
WITH jan_customers AS ( SELECT DISTINCT customer_id FROM orders WHERE order_date BETWEEN '2026-01-01' AND '2026-01-31' ), feb_returners AS ( SELECT DISTINCT j.customer_id FROM jan_customers j JOIN orders o ON j.customer_id = o.customer_id WHERE o.order_date BETWEEN '2026-02-01' AND '2026-02-28' ) SELECT COUNT(*) as jan_customers, (SELECT COUNT(*) FROM feb_returners) as feb_returners;
---
SQL + Power BI + Python — The Complete Data Analyst Stack
SQL is the foundation; Power BI and Python are built on top of it:
- •. SQL: Extract and clean data from databases
- •. Python (Pandas): Complex data transformation, statistical analysis, ML prep
- •. Power BI: Dashboard and visualization layer (Power BI can connect directly to databases via SQL queries)
- •. Excel: Ad-hoc analysis and presentation
A data analyst proficient in all four is the most hireable profile in India's data job market in 2026.
---
Data Analytics Course in Amritsar →