SQL Programming Resources
75K subscribers
496 photos
13 files
440 links
Find top SQL resources from global universities, cool projects, and learning materials for data analytics.

Admin: @coderfun

Useful links: heylink.me/DataAnalytics

Promotions: @love_data
Download Telegram
βœ… SQL CASE Statement 🎯

The CASE statement lets you apply conditional logic inside SQL queries β€” like if/else in programming.

1️⃣ Basic CASE Syntax
SELECT name, salary,
CASE
WHEN salary > 80000 THEN 'High'
WHEN salary BETWEEN 50000 AND 80000 THEN 'Medium'
ELSE 'Low'
END AS salary_level
FROM employees;

βœ… Categorizes salaries as High, Medium, or Low.

2️⃣ CASE in ORDER BY
Sort based on custom logic.

SELECT name, department  
FROM employees
ORDER BY
CASE department
WHEN 'HR' THEN 1
WHEN 'Engineering' THEN 2
ELSE 3
END;

βœ… HR shows up first, then Engineering, then others.

3️⃣ CASE in WHERE Clause
Control filtering logic conditionally.

SELECT *  
FROM orders
WHERE status =
CASE
WHEN customer_type = 'VIP' THEN 'priority'
ELSE 'standard'
END;


4️⃣ Nested CASE (Advanced)
SELECT name, marks,  
CASE
WHEN marks >= 90 THEN 'A'
WHEN marks >= 75 THEN
CASE WHEN marks >= 85 THEN 'B+' ELSE 'B' END
ELSE 'C'
END AS grade
FROM students;


🎯 Use CASE When You Want To:
β€’ Create labels or buckets
β€’ Replace multiple IF conditions
β€’ Make results more readable

πŸ“ Practice Tasks:
1. Add a column that shows β€˜Pass’ or β€˜Fail’ based on marks
2. Create a salary band (Low/Medium/High) using CASE
3. Use CASE to sort products as 'Electronics' first, then 'Clothing'

πŸ’¬ Tap ❀️ for more!
❀4
βœ… SQL Programming: Handling NULL Values πŸ› οΈ

Missing data is common in databases. COALESCE() helps you fill in defaults and avoid null-related issues.

1️⃣ What is COALESCE?
Returns the first non-null value in a list.
SELECT name, COALESCE(phone, 'Not Provided') AS contact  
FROM customers;

βœ… If phone is NULL, it shows β€˜Not Provided’.

2️⃣ COALESCE with Calculations
Prevent nulls from breaking math.
SELECT name, salary, COALESCE(bonus, 0) AS bonus,  
salary + COALESCE(bonus, 0) AS total_income
FROM employees;

βœ… If bonus is NULL, treat it as 0 to compute total.

3️⃣ Nested COALESCE
Use multiple fallback options.
SELECT name, COALESCE(email, alt_email, 'No Email') AS contact_email  
FROM users;

βœ… Checks email, then alt_email, then default text.

4️⃣ COALESCE in WHERE clause
Filter even when data has nulls.
SELECT *  
FROM products
WHERE COALESCE(category, 'Uncategorized') = 'Electronics';


🎯 Use COALESCE When You Want To:
β€’ Replace NULLs with defaults
β€’ Keep math & filters working
β€’ Avoid errors in reports or dashboards

πŸ“ Practice Tasks:
1. Replace nulls in city with β€˜Unknown’
2. Show total amount = price + tax (tax may be null)
3. Replace nulls in description with β€˜No Info Available’

βœ… Solution for Practice Tasks πŸ‘‡

1️⃣ Replace NULLs in city with 'Unknown'
SELECT name, COALESCE(city, 'Unknown') AS city  
FROM customers;


2️⃣ Show total amount = price + tax (tax may be NULL)
SELECT product_name, price, COALESCE(tax, 0) AS tax,  
price + COALESCE(tax, 0) AS total_amount
FROM products;


3️⃣ Replace NULLs in description with 'No Info Available'
SELECT product_name, COALESCE(description, 'No Info Available') AS description  
FROM products;


πŸ’¬ Tap ❀️ for more!
❀4
βœ… SQL Window Functions 🧠πŸͺŸ

Window functions perform calculations across rows that are related to the current row β€” without collapsing the result like GROUP BY.

1️⃣ ROW_NUMBER() – Assigns a unique row number per partition
SELECT name, department,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;

➀ Gives ranking within each department

2️⃣ RANK() & DENSE_RANK() – Ranking with gaps (RANK) or without gaps (DENSE_RANK)
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;


3️⃣ LAG() & LEAD() – Access previous or next row value
SELECT name, salary,
LAG(salary) OVER (ORDER BY salary) AS prev_salary,
LEAD(salary) OVER (ORDER BY salary) AS next_salary
FROM employees;

➀ Compare salary trends row-wise

4️⃣ SUM(), AVG(), COUNT() OVER() – Running totals, moving averages, etc.
SELECT department, salary,
SUM(salary) OVER (PARTITION BY department) AS dept_total
FROM employees;


5️⃣ NTILE(n) – Divides rows into n equal buckets
SELECT name, salary,
NTILE(4) OVER (ORDER BY salary DESC) AS quartile
FROM employees;


πŸ’‘ Why Use Window Functions:
β€’ Perform row-wise calculations
β€’ Avoid GROUP BY limitations
β€’ Enable advanced analytics (ranking, trends, etc.)

πŸ§ͺ Practice Task:
Write a query to find the top 2 earners in each department using ROW_NUMBER().

πŸ’¬ Tap ❀️ for more!
❀6
βœ… SQL Real-World Use Cases πŸ’ΌπŸ§ 

SQL is the backbone of data analysis and automation in many domains. Here’s how it powers real work:

1️⃣ Sales & CRM
Use Case: Sales Tracking & Pipeline Management
β€’ Track sales per region, product, rep
β€’ Identify top-performing leads
β€’ Calculate conversion rates
SQL Task:
SELECT region, SUM(sales_amount)  
FROM deals
GROUP BY region;


2️⃣ Finance
Use Case: Monthly Revenue and Expense Reporting
β€’ Aggregate revenue by month
β€’ Analyze profit margins
β€’ Flag unusual transactions
SQL Task:
SELECT MONTH(date), SUM(revenue - expense) AS profit  
FROM finance_data
GROUP BY MONTH(date);


3️⃣ HR Analytics
Use Case: Employee Attrition Analysis
β€’ Track tenure, exits, departments
β€’ Calculate average retention
β€’ Segment by age, role, or location
SQL Task:
SELECT department, COUNT(*)  
FROM employees
WHERE exit_date IS NOT NULL
GROUP BY department;


4️⃣ E-commerce
Use Case: Customer Order Behavior
β€’ Find most ordered products
β€’ Time between repeat orders
β€’ Cart abandonment patterns
SQL Task:
SELECT customer_id, COUNT(order_id)  
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > 5;


5️⃣ Healthcare
Use Case: Patient Visit Frequency
β€’ Find frequent visitors
β€’ Analyze doctor performance
β€’ Calculate average stay duration
SQL Task:
SELECT patient_id, COUNT(*) AS visits  
FROM appointments
GROUP BY patient_id;


6️⃣ Marketing
Use Case: Campaign Performance by Channel
β€’ Track leads, clicks, conversions
β€’ Compare cost-per-lead by platform
SQL Task:
SELECT channel, SUM(conversions)/SUM(clicks) AS conv_rate  
FROM campaign_data
GROUP BY channel;


πŸ§ͺ Practice Task:
Pick a dataset (orders, users, sales)
β†’ Write 3 queries: summary, trend, filter
β†’ Visualize the output in Excel or Power BI

πŸ’¬ Tap ❀️ for more!
❀4
βœ… Useful Platform to Practice SQL Programming 🧠πŸ–₯️

Learning SQL is just the first step β€” practice is what builds real skill. Here are the best platforms for hands-on SQL:

1️⃣ LeetCode – For Interview-Oriented SQL Practice
β€’ Focus: Real interview-style problems
β€’ Levels: Easy to Hard
β€’ Schema + Sample Data Provided
β€’ Great for: Data Analyst, Data Engineer, FAANG roles
βœ” Tip: Start with Easy β†’ filter by β€œDatabase” tag
βœ” Popular Section: Database β†’ Top 50 SQL Questions
Example Problem: β€œFind duplicate emails in a user table” β†’ Practice filtering, GROUP BY, HAVING

2️⃣ HackerRank – Structured & Beginner-Friendly
β€’ Focus: Step-by-step SQL track
β€’ Has certification tests (SQL Basic, Intermediate)
β€’ Problem sets by topic: SELECT, JOINs, Aggregations, etc.
βœ” Tip: Follow the full SQL track
βœ” Bonus: Company-specific challenges
Try: β€œRevising Aggregations – The Count Function” β†’ Build confidence with small wins

3️⃣ Mode Analytics – Real-World SQL in Business Context
β€’ Focus: Business intelligence + SQL
β€’ Uses real-world datasets (e.g., e-commerce, finance)
β€’ Has an in-browser SQL editor with live data
βœ” Best for: Practicing dashboard-level queries
βœ” Tip: Try the SQL case studies & tutorials

4️⃣ StrataScratch – Interview Questions from Real Companies
β€’ 500+ problems from companies like Uber, Netflix, Google
β€’ Split by company, difficulty, and topic
βœ” Best for: Intermediate to advanced level
βœ” Tip: Try β€œHard” questions after doing 30–50 easy/medium

5️⃣ DataLemur – Short, Practical SQL Problems
β€’ Crisp and to the point
β€’ Good UI, fast learning
β€’ Real interview-style logic
βœ” Use when: You want fast, smart SQL drills

πŸ“Œ How to Practice Effectively:
β€’ Spend 20–30 mins/day
β€’ Focus on JOINs, GROUP BY, HAVING, Subqueries
β€’ Analyze problem β†’ write β†’ debug β†’ re-write
β€’ After solving, explain your logic out loud

πŸ§ͺ Practice Task:
Try solving 5 SQL questions from LeetCode or HackerRank this week. Start with SELECT, WHERE, and GROUP BY.

πŸ’¬ Tap ❀️ for more!
❀7
βœ… Data Analytics Roadmap for Freshers in 2025 πŸš€πŸ“Š

1️⃣ Understand What a Data Analyst Does
πŸ” Analyze data, find insights, create dashboards, support business decisions.

2️⃣ Start with Excel
πŸ“ˆ Learn:
– Basic formulas
– Charts & Pivot Tables
– Data cleaning
πŸ’‘ Excel is still the #1 tool in many companies.

3️⃣ Learn SQL
🧩 SQL helps you pull and analyze data from databases.
Start with:
– SELECT, WHERE, JOIN, GROUP BY
πŸ› οΈ Practice on platforms like W3Schools or Mode Analytics.

4️⃣ Pick a Programming Language
🐍 Start with Python (easier) or R
– Learn pandas, matplotlib, numpy
– Do small projects (e.g. analyze sales data)

5️⃣ Data Visualization Tools
πŸ“Š Learn:
– Power BI or Tableau
– Build simple dashboards
πŸ’‘ Start with free versions or YouTube tutorials.

6️⃣ Practice with Real Data
πŸ” Use sites like Kaggle or Data.gov
– Clean, analyze, visualize
– Try small case studies (sales report, customer trends)

7️⃣ Create a Portfolio
πŸ’» Share projects on:
– GitHub
– Notion or a simple website
πŸ“Œ Add visuals + brief explanations of your insights.

8️⃣ Improve Soft Skills
πŸ—£οΈ Focus on:
– Presenting data in simple words
– Asking good questions
– Thinking critically about patterns

9️⃣ Certifications to Stand Out
πŸŽ“ Try:
– Google Data Analytics (Coursera)
– IBM Data Analyst
– LinkedIn Learning basics

πŸ”Ÿ Apply for Internships & Entry Jobs
🎯 Titles to look for:
– Data Analyst (Intern)
– Junior Analyst
– Business Analyst

πŸ’¬ React ❀️ for more!
❀9
βœ… How to Build a Job-Ready Data Analytics Portfolio πŸ’ΌπŸ“Š

1️⃣ Pick Solid Datasets

β€’ Public: Kaggle, UCI ML Repo, data.gov
β€’ Business-like: e-commerce, churn, marketing spend, HR attrition
β€’ Size: 5k–200k rows, relatively clean

2️⃣ Create 3 Signature Projects

β€’ SQL: Customer Cohort & Retention (joins, window functions)
β€’ BI: Executive Sales Dashboard (Power BI/Tableau, drill-through, DAX/calculated fields)
β€’ Python: Marketing ROI & Attribution (pandas, seaborn, A/B test basics)

3️⃣ Tell a Story, Not Just Charts

β€’ Problem β†’ Approach β†’ Insight β†’ Action
β€’ Add one business recommendation per insight

4️⃣ Document Like a Pro

β€’ README: problem, data source, methods, results, next steps
β€’ Screenshots or GIFs of dashboards
β€’ Repo structure: /data, /notebooks, /sql, /reports

5️⃣ Show Measurable Impact

β€’ β€œReduced reporting time by 70% with automated Power BI pipeline”
β€’ β€œIdentified 12% churn segment with a retention playbook”

6️⃣ Make It Easy to Review

β€’ Share live dashboards (Publish to Web), short Loom/YouTube walkthrough
β€’ Include SQL snippets
β€’ Pin top 3 projects on GitHub and LinkedIn Featured

7️⃣ Iterate With Feedback

β€’ Post drafts on LinkedIn, ask β€œWhat would you improve?”
β€’ Apply suggestions, track updates in a CHANGELOG
🎯 Goal: 3 projects, 3 stories, 3 measurable outcomes.

πŸ’¬ Double Tap ❀️ For More!
❀3
Core SQL Interview Questions. With answers

1 What is SQL
β€’ SQL stands for Structured Query Language
β€’ You use it to read and manage data in relational databases
β€’ Used in MySQL, PostgreSQL, SQL Server, Oracle

2 What is an RDBMS
β€’ Relational Database Management System
β€’ Stores data in tables with rows and columns
β€’ Uses keys to link tables
β€’ Example. Customer table linked to Orders table using customer_id

3 What is a table
β€’ Structured storage for data
β€’ Rows are records
β€’ Columns are attributes
β€’ Example. One row equals one customer

4 What is a primary key
β€’ Uniquely identifies each row
β€’ Cannot be NULL
β€’ No duplicate values
β€’ Example. user_id in users table

5 What is a foreign key
β€’ Links one table to another
β€’ Refers to a primary key in another table
β€’ Allows duplicate values
β€’ Example. user_id in orders table

6 Difference between primary key and foreign key
β€’ Primary key ensures uniqueness
β€’ Foreign key ensures relationship
β€’ One table can have one primary key
β€’ One table can have multiple foreign keys

7 What is NULL
β€’ Represents missing or unknown value
β€’ Not equal to zero or empty string
β€’ Use IS NULL or IS NOT NULL to check

8 What are constraints
β€’ Rules applied on columns
β€’ Maintain data quality
β€’ Common constraints
– NOT NULL
– UNIQUE
– PRIMARY KEY
– FOREIGN KEY
– CHECK

9 What are data types
β€’ Define type of data stored
β€’ Common types
– INT for numbers
– VARCHAR for text
– DATE for dates
– FLOAT or DECIMAL for decimals

10 Interview tip you must remember
β€’ Always explain with a small example
β€’ Speak logic before syntax
β€’ Keep answers short and direct

Double Tap ❀️ For More
❀11
When preparing for an SQL project-based interview, the focus typically shifts from theoretical knowledge to practical application. Here are some SQL project-based interview questions that could help assess your problem-solving skills and experience:

1. Database Design and Schema
- Question: Describe a database schema you have designed in a past project. What were the key entities, and how did you establish relationships between them?
- Follow-Up: How did you handle normalization? Did you denormalize any tables for performance reasons?

2. Data Modeling
- Question: How would you model a database for an e-commerce application? What tables would you include, and how would they relate to each other?
- Follow-Up: How would you design the schema to handle scenarios like discount codes, product reviews, and inventory management?

3. Query Optimization
- Question: Can you discuss a time when you optimized an SQL query? What was the original query, and what changes did you make to improve its performance?
- Follow-Up: What tools or techniques did you use to identify and resolve the performance issues?

4. ETL Processes
- Question: Describe an ETL (Extract, Transform, Load) process you have implemented. How did you handle data extraction, transformation, and loading?
- Follow-Up: How did you ensure data quality and consistency during the ETL process?

5. Handling Large Datasets
- Question: In a project where you dealt with large datasets, how did you manage performance and storage issues?
- Follow-Up: What indexing strategies or partitioning techniques did you use?

6. Joins and Subqueries
- Question: Provide an example of a complex query you wrote involving multiple joins and subqueries. What was the business problem you were solving?
- Follow-Up: How did you ensure that the query performed efficiently?

7. Stored Procedures and Functions
- Question: Have you created stored procedures or functions in any of your projects? Can you describe one and explain why you chose to encapsulate the logic in a stored procedure?
- Follow-Up: How did you handle error handling and logging within the stored procedure?

8. Data Integrity and Constraints
- Question: How did you enforce data integrity in your SQL projects? Can you give examples of constraints (e.g., primary keys, foreign keys, unique constraints) you implemented?
- Follow-Up: How did you handle situations where constraints needed to be temporarily disabled or modified?

9. Version Control and Collaboration
- Question: How did you manage database version control in your projects? What tools or practices did you use to ensure collaboration with other developers?
- Follow-Up: How did you handle conflicts or issues arising from multiple developers working on the same database?

10. Data Migration
- Question: Describe a data migration project you worked on. How did you ensure that the migration was successful, and what steps did you take to handle data inconsistencies or errors?
- Follow-Up: How did you test the migration process before moving to the production environment?

11. Security and Permissions
- Question: In your SQL projects, how did you manage database security?
- Follow-Up: How did you handle encryption or sensitive data within the database?

12. Handling Unstructured Data
- Question: Have you worked with unstructured or semi-structured data in an SQL environment?
- Follow-Up: What challenges did you face, and how did you overcome them?

13. Real-Time Data Processing
   - Question: Can you describe a project where you handled real-time data processing using SQL? What were the key challenges, and how did you address them?
   - Follow-Up: How did you ensure the performance and reliability of the real-time data processing system?

Be prepared to discuss specific examples from your past work and explain your thought process in detail.

Here you can find SQL Interview ResourcesπŸ‘‡
https://t.iss.one/DataSimplifier

Share with credits: https://t.iss.one/sqlspecialist

Hope it helps :)
❀1
βœ… Basic SQL Queries Interview Questions With Answers πŸ–₯️

1. What does SELECT do
β€’ SELECT fetches data from a table
β€’ You choose columns you want to see
Example: SELECT name, salary FROM employees;

2. What does FROM do
β€’ FROM tells SQL where data lives
β€’ It specifies the table name
Example: SELECT * FROM customers;

3. What is WHERE clause
β€’ WHERE filters rows
β€’ It runs before aggregation
Example: SELECT * FROM orders WHERE status = 'Delivered';

4. Difference between WHERE and HAVING
β€’ WHERE filters rows before GROUP BY
β€’ HAVING filters groups after aggregation
Example: WHERE filters orders, HAVING filters total_sales

5. How do you sort data
β€’ Use ORDER BY
β€’ Default order is ASC
Example: SELECT * FROM employees ORDER BY salary DESC;

6. How do you sort by multiple columns
β€’ SQL sorts left to right
Example: SELECT * FROM students ORDER BY class ASC, marks DESC;

7. What is LIMIT
β€’ LIMIT restricts number of rows returned
β€’ Useful for top N queries
Example: SELECT * FROM products LIMIT 5;

8. What is OFFSET
β€’ OFFSET skips rows
β€’ Used with LIMIT for pagination
Example: SELECT * FROM products LIMIT 5 OFFSET 10;

9. How do you filter on multiple conditions
β€’ Use AND, OR
Example: SELECT * FROM users WHERE city = 'Delhi' AND age > 25;

10. Difference between AND and OR
β€’ AND needs all conditions true
β€’ OR needs one condition true

Quick interview advice
β€’ Always say execution order: FROM β†’ WHERE β†’ SELECT β†’ ORDER BY β†’ LIMIT
β€’ Write clean examples
β€’ Speak logic first, syntax nextΒΉ

Double Tap ❀️ For More
❀6
βœ… SQL Joins Interview Questions With Answers πŸ–₯️

1. What is a JOIN in SQL. Explain with an example. 
β€’ JOIN combines data from multiple tables
β€’ Tables connect using a common column
β€’ Usually primary key to foreign key
Example tables 
Customers 
customer_id, name 
Orders 
order_id, customer_id, amount 
Query 
SELECT c.name, o.amount 
FROM customers c 
INNER JOIN orders o 
ON c.customer_id = o.customer_id; 
Explanation 
β€’ SQL matches customer_id in both tables
β€’ Output shows only related customer order data

2. What is INNER JOIN. When do you use it. 
β€’ INNER JOIN returns only matching rows
β€’ Rows without match are removed
Example 
Find customers who placed orders 
Query 
SELECT c.customer_id, c.name 
FROM customers c 
INNER JOIN orders o 
ON c.customer_id = o.customer_id; 
Logic 
β€’ Customers without orders are excluded
β€’ Only matched records appear

3. What is LEFT JOIN. Explain with use case. 
β€’ LEFT JOIN returns all rows from left table
β€’ Matching rows from right table
β€’ Non matches show NULL
Example 
Find all customers and their orders 
Query 
SELECT c.name, o.order_id 
FROM customers c 
LEFT JOIN orders o 
ON c.customer_id = o.customer_id; 
Logic 
β€’ Customers without orders still appear
β€’ order_id becomes NULL

4. Difference between INNER JOIN and LEFT JOIN. 
β€’ INNER JOIN removes non matching rows
β€’ LEFT JOIN keeps all left table rows
β€’ LEFT JOIN shows NULL for missing matches
Interview tip 
Explain using one missing record example 

5. What is RIGHT JOIN. 
β€’ Returns all rows from right table
β€’ Matching rows from left table
β€’ Rarely used in real projects
Example 
SELECT c.name, o.order_id 
FROM customers c 
RIGHT JOIN orders o 
ON c.customer_id = o.customer_id; 

6. What is FULL OUTER JOIN. 
β€’ Returns all rows from both tables
β€’ Matches where possible
β€’ Non matches show NULL
Example 
SELECT c.name, o.order_id 
FROM customers c 
FULL OUTER JOIN orders o 
ON c.customer_id = o.customer_id; 
Use case 
β€’ Data reconciliation
β€’ Comparing two datasets

7. How do you find records present in one table but not in another. 
Find customers with no orders 
Query 
SELECT c.customer_id, c.name 
FROM customers c 
LEFT JOIN orders o 
ON c.customer_id = o.customer_id 
WHERE o.order_id IS NULL; 
Logic 
β€’ LEFT JOIN keeps all customers
β€’ WHERE filters non matched rows

8. Explain JOIN with WHERE clause. Common mistake. 
β€’ WHERE runs after JOIN
β€’ Wrong WHERE condition breaks LEFT JOIN
Wrong 
LEFT JOIN orders 
WHERE orders.amount > 1000 
Correct 
LEFT JOIN orders 
ON (link unavailable) = (link unavailable) 
AND orders.amount > 1000 

9. How do you join more than two tables. 
β€’ JOIN step by step
β€’ Each JOIN needs condition
Example 
SELECT c.name, o.order_id, p.product_name 
FROM customers c 
JOIN orders o 
ON c.customer_id = o.customer_id 
JOIN products p 
ON o.product_id = p.product_id; 

10. SQL execution order for JOIN queries. 
β€’ FROM
β€’ JOIN
β€’ WHERE
β€’ GROUP BY
β€’ HAVING
β€’ SELECT
β€’ ORDER BY

Interview advice 
β€’ Always explain logic first
β€’ Draw table flow in words
β€’ Then write query

Double Tap β™₯️ For More
❀9
βœ… SQL GROUP BY and AGGREGATION Interview Questions πŸŽ“

1. What is GROUP BY in SQL.
β€’ GROUP BY groups rows with same values
β€’ Used with aggregate functions
β€’ One row per group in output

Example

Find total salary per department

FROM employees
GROUP BY department;

Logic
β€’ Rows grouped by department
β€’ SUM runs on each group

2. Why do we use aggregate functions.
β€’ To summarize data
β€’ To calculate totals, averages, counts

Common functions
β€’ COUNT
β€’ SUM
β€’ AVG
β€’ MIN
β€’ MAX

3. What happens if you use GROUP BY without aggregation.
β€’ Output shows unique combinations of grouped columns

Example
FROM employees
GROUP BY department;

Logic
β€’ Acts like DISTINCT

4. Difference between WHERE and HAVING.
β€’ WHERE filters rows
β€’ HAVING filters groups
β€’ WHERE runs before GROUP BY
β€’ HAVING runs after GROUP BY

Example
Find departments with total salary above 5,00,000

FROM employees
GROUP BY department
HAVING SUM(salary) > 500000;


5. Can you use WHERE with GROUP BY.
β€’ Yes
β€’ WHERE filters raw data before grouping

Example

Ignore inactive employees

FROM employees
WHERE status = 'Active'
GROUP BY department;


6. Common GROUP BY interview error.

Why does this query fail
FROM employees
GROUP BY department;

Answer
β€’ Non aggregated column must be in GROUP BY
β€’ name is missing

Correct query
FROM employees
GROUP BY department;


7. What's the difference between COUNT(*) COUNT(column)?
β€’ COUNT(*) counts all rows
β€’ COUNT(column) skips NULL values

Example
SELECT COUNT(delivery_date) FROM orders;


8. Find total orders per customer.
FROM orders
GROUP BY customer_id;

Logic
β€’ One row per customer
β€’ COUNT runs per customer group

9. Find customers with more than 5 orders.
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > 5;

Logic
β€’ GROUP first
β€’ Filter groups using HAVING

10. Execution order for GROUP BY queries.
β€’ FROM
β€’ WHERE
β€’ GROUP BY
β€’ HAVING
β€’ SELECT
β€’ ORDER BY

Interview advice
β€’ Say execution order clearly
β€’ Explain using one simple example
β€’ Avoid mixing WHERE and HAVING logic

Double Tap β™₯️ For More
❀7
βœ… SQL Window Functions Interview Questions with Answers ✍️

1. What are window functions in SQL?
β€’ Window functions perform calculations across related rows
β€’ They do not reduce rows
β€’ Each row keeps its detail
Key syntax: OVER (PARTITION BY, ORDER BY)

2. Difference between GROUP BY and window functions
β€’ GROUP BY collapses rows
β€’ Window functions keep all rows
β€’ Window functions add calculated columns

3. What is ROW_NUMBER?
β€’ Assigns unique sequential number
β€’ No ties allowed

Example: Rank employees by salary

SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn
FROM employees;

4. Difference between ROW_NUMBER, RANK, and DENSE_RANK
β€’ ROW_NUMBER gives unique numbers
β€’ RANK skips numbers on ties
β€’ DENSE_RANK does not skip

Example salaries: 100, 100, 90

ROW_NUMBER β†’ 1, 2, 3
RANK β†’ 1, 1, 3
DENSE_RANK β†’ 1, 1, 2

5. What is PARTITION BY?
β€’ PARTITION BY splits data into groups
β€’ Window function runs inside each group

Example: Rank employees per department

SELECT department, name, salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dept_rank
FROM employees;

6. Find top 2 salaries per department

SELECT * FROM (
SELECT department, name, salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk <= 2;

7. What is LAG?
β€’ Accesses previous row value
β€’ Used for comparisons

Example: Day over day sales

SELECT date, sales, LAG(sales) OVER (ORDER BY date) AS prev_day_sales
FROM daily_sales;

8. What is LEAD?
β€’ Accesses next row value

Example: Compare today with next day

SELECT date, sales, LEAD(sales) OVER (ORDER BY date) AS next_day_sales
FROM daily_sales;

9. Calculate day over day growth
SELECT date, sales - LAG(sales) OVER (ORDER BY date) AS growth
FROM daily_sales;

10. Common window function interview mistakes
β€’ Forgetting ORDER BY inside OVER
β€’ Using WHERE instead of subquery to filter ranks
β€’ Mixing GROUP BY with window logic incorrectly

Execution order: FROM β†’ WHERE β†’ GROUP BY β†’ HAVING β†’ SELECT β†’ WINDOW β†’ ORDER BY

Double Tap β™₯️ For More
❀4πŸ‘2
πŸ“ˆ Want to Excel at Data Analytics? Master These Essential Skills! β˜‘οΈ

Core Concepts:
β€’ Statistics & Probability – Understand distributions, hypothesis testing
β€’ Excel – Pivot tables, formulas, dashboards

Programming:
β€’ Python – NumPy, Pandas, Matplotlib, Seaborn
β€’ R – Data analysis & visualization
β€’ SQL – Joins, filtering, aggregation

Data Cleaning & Wrangling:
β€’ Handle missing values, duplicates
β€’ Normalize and transform data

Visualization:
β€’ Power BI, Tableau – Dashboards
β€’ Plotly, Seaborn – Python visualizations
β€’ Data Storytelling – Present insights clearly

Advanced Analytics:
β€’ Regression, Classification, Clustering
β€’ Time Series Forecasting
β€’ A/B Testing & Hypothesis Testing

ETL & Automation:
β€’ Web Scraping – BeautifulSoup, Scrapy
β€’ APIs – Fetch and process real-world data
β€’ Build ETL Pipelines

Tools & Deployment:
β€’ Jupyter Notebook / Colab
β€’ Git & GitHub
β€’ Cloud Platforms – AWS, GCP, Azure
β€’ Google BigQuery, Snowflake

Hope it helps :)
❀3
βœ… SQL CTEs and Subqueries Interview Questions with Answers πŸ–₯️

1. Find employees who earn more than the average salary.

Table: employees (employee_id, name, salary)
SELECT name, salary 
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);

Logic: Inner query calculates average salary. Outer query filters employees above average.

2. Find employees who earn the highest salary in each department.

Table: employees (employee_id, name, department, salary)
SELECT name, department, salary 
FROM employees e
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE department = e.department
);

Logic: Subquery runs per department. Matches max salary inside same department.

3. Find departments where average salary is greater than 60,000.
SELECT department 
FROM (
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
) t
WHERE avg_salary > 60000;

Logic: Inner query calculates department average. Outer query filters required departments.

4. Same query using CTE.

WITH dept_avg AS ( 
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
)
SELECT department
FROM dept_avg
WHERE avg_salary > 60000;

Logic: CTE stores aggregated result. Final query reads from CTE.

5. Find customers who placed more orders than the average number of orders.

Tables: customers (customer_id, name), orders (order_id, customer_id)

SELECT customer_id 
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > (
SELECT AVG(order_count)
FROM (
SELECT COUNT(order_id) AS order_count
FROM orders
GROUP BY customer_id
) x
);

Logic: Inner query calculates orders per customer. Next level gets average. HAVING filters customers above average.

6. Find top-selling product by total sales amount.
Table: sales (product_id, amount)

SELECT product_id
FROM sales
GROUP BY product_id
HAVING SUM(amount) = (
SELECT MAX(total_sales)
FROM (
SELECT SUM(amount) AS total_sales
FROM sales
GROUP BY product_id
) t
);

Logic: Inner query calculates sales per product. Outer query finds max and matches it.

7. Rewrite using CTE.

WITH product_sales AS (
SELECT product_id, SUM(amount) AS total_sales
FROM sales
GROUP BY product_id
)
SELECT product_id
FROM product_sales
WHERE total_sales = (
SELECT MAX(total_sales)
FROM product_sales
);

Logic: CTE avoids repeating aggregation. Cleaner and readable.

8. Find employees whose salary is greater than their department average.

WITH dept_avg AS (
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
)
SELECT e.name, e.salary
FROM employees e
JOIN dept_avg d ON e.department = d.department
WHERE e.salary > d.avg_salary;

Logic: First compute department averages. Join back to employees. Filter higher earners.

Double Tap β™₯️ For More
❀2
βœ… SQL CASE Statements and Data Cleaning Interview Questions with Answers πŸ“Š

1. Classify customers based on total spend
Table: orders (order_id, customer_id, amount)

Requirement:
- Gold if total_spend >= 100000
- Silver if total_spend between 50000 and 99999
- Bronze otherwise

Solution:
SELECT customer_id,
CASE
WHEN SUM(amount) >= 100000 THEN 'Gold'
WHEN SUM(amount) >= 50000 THEN 'Silver'
ELSE 'Bronze'
END AS customer_category
FROM orders
GROUP BY customer_id;

2. Create an age group column for users
Table: users (user_id, age)

Requirement:
- Below 18
- 18 to 30
- Above 30

Solution:
SELECT user_id,
CASE
WHEN age < 18 THEN 'Below 18'
WHEN age BETWEEN 18 AND 30 THEN '18-30'
ELSE 'Above 30'
END AS age_group
FROM users;

3. Replace NULL salary with 0
Table: employees (employee_id, salary)

Solution:
SELECT employee_id, COALESCE(salary, 0) AS salary
FROM employees;

4. Count employees with missing email IDs
Table: employees (employee_id, email)

Solution:
SELECT COUNT(*) AS missing_email_count
FROM employees
WHERE email IS NULL;

5. Remove extra spaces from customer names
Table: customers (customer_id, name)

Solution:
SELECT customer_id, TRIM(name) AS clean_name
FROM customers;

6. Extract first 3 characters from product code
Table: products (product_code)

Solution:
SELECT product_code, SUBSTRING(product_code, 1, 3) AS product_prefix
FROM products;

7. Standardize date format
Table: orders (order_date)

Requirement: Convert to YYYY-MM-DD

Solution:
SELECT CAST(order_date AS DATE) AS clean_order_date
FROM orders;

8. Mark inactive users based on last login
Table: users (user_id, last_login_date)

Requirement: Inactive if last login before 2023-01-01

Solution:
SELECT user_id,
CASE
WHEN last_login_date < '2023-01-01' THEN 'Inactive'
ELSE 'Active'
END AS user_status
FROM users;

9. Handle empty string as NULL
Table: customers (phone_number)

Solution:
SELECT NULLIF(phone_number, '') AS phone_number
FROM customers;

10. Create a clean reporting column using multiple rules
Table: transactions (amount)

Requirement:
- High if amount >= 10000
- Medium if 5000 to 9999
- Low otherwise

Solution:
SELECT amount,
CASE
WHEN amount >= 10000 THEN 'High'
WHEN amount >= 5000 THEN 'Medium'
ELSE 'Low'
END AS transaction_type
FROM transactions;

Double Tap β™₯️ For More
❀8
Real-world SQL Scenario based Interview Questions with Answers

Question 1: Calculate conversion rate for a sales funnel

Tables: events (user_id, event_name, event_date)

Events: signup β†’ product_view β†’ purchase

Requirement: Count users at each stage, compute conversion from signup to purchase

WITH stages AS (
SELECT user_id,
MAX(CASE WHEN event_name = 'signup' THEN 1 ELSE 0 END) AS signup,
MAX(CASE WHEN event_name = 'product_view' THEN 1 ELSE 0 END) AS view,
MAX(CASE WHEN event_name = 'purchase' THEN 1 ELSE 0 END) AS purchase
FROM events
GROUP BY user_id
)

SELECT
SUM(signup) AS signups,
SUM(purchase) AS purchases,
ROUND(100.0 * SUM(purchase) / SUM(signup), 2) AS conversion_rate
FROM stages;


Question 2: Build a monthly retention cohort

Tables: users (user_id, signup_date), logins (user_id, login_date)

Requirement: Cohort by signup month, show retained users by login month

WITH cohort AS (
SELECT user_id, DATE_TRUNC('month', signup_date) AS cohort_month
FROM users
),
activity AS (
SELECT l.user_id, DATE_TRUNC('month', l.login_date) AS activity_month
FROM logins l
)

SELECT
c.cohort_month,
a.activity_month,
COUNT(DISTINCT a.user_id) AS active_users
FROM cohort c
JOIN activity a ON c.user_id = a.user_id
GROUP BY c.cohort_month, a.activity_month
ORDER BY c.cohort_month, a.activity_month;


Question 3: Calculate monthly churn rate

Tables: subscriptions (user_id, start_date, end_date)

Requirement: Churned users per month, churn rate = churned / active users

WITH active_users AS (
SELECT DATE_TRUNC('month', start_date) AS month, COUNT(DISTINCT user_id) AS active_users
FROM subscriptions
GROUP BY DATE_TRUNC('month', start_date)
),
churned_users AS (
SELECT DATE_TRUNC('month', end_date) AS month, COUNT(DISTINCT user_id) AS churned_users
FROM subscriptions
WHERE end_date IS NOT NULL
GROUP BY DATE_TRUNC('month', end_date)
)
SELECT
a.month,
a.active_users,
COALESCE(c.churned_users, 0) AS churned_users,
ROUND(100.0 * COALESCE(c.churned_users, 0) / a.active_users, 2) AS churn_rate
FROM active_users a
LEFT JOIN churned_users c ON a.month = c.month
ORDER BY a.month;


Question 4: Calculate Daily Active Users

Table: user_activity (user_id, activity_date)

Requirement: DAU per day

SELECT activity_date, COUNT(DISTINCT user_id) AS dau
FROM user_activity
GROUP BY activity_date
ORDER BY activity_date;


Question 5: Revenue by marketing channel

Tables: orders (order_id, user_id, amount, order_date), users (user_id, channel)

Requirement: Total revenue per channel, monthly breakdown

SELECT 
DATE_TRUNC('month', o.order_date) AS month,
u.channel,
SUM(o.amount) AS revenue
FROM orders o
JOIN users u ON o.user_id = u.user_id
GROUP BY DATE_TRUNC('month', o.order_date), u.channel
ORDER BY month, revenue DESC;


Question 6: Identify returning customers

Table: orders (order_id, customer_id, order_date)

Requirement: Customers with more than one order

SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > 1;


Question 7: Top product by revenue per month

Table: sales (product_id, amount, sale_date)

WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', sale_date) AS month,
product_id,
SUM(amount) AS revenue
FROM sales
GROUP BY DATE_TRUNC('month', sale_date), product_id
),
ranked AS (
SELECT *, RANK() OVER (PARTITION BY month ORDER BY revenue DESC) AS rnk
FROM monthly_sales
)

SELECT month, product_id, revenue
FROM ranked
WHERE rnk = 1;


Double Tap β™₯️ For More
❀6
βœ… Complete SQL Roadmap in 2 Months

Month 1: Strong SQL Foundations
Week 1: Database and query basics
β€’ What SQL does in analytics and business
β€’ Tables, rows, columns
β€’ Primary key and foreign key
β€’ SELECT, DISTINCT
β€’ WHERE with AND, OR, IN, BETWEEN
Outcome: You understand data structure and fetch filtered data.

Week 2: Sorting and aggregation
β€’ ORDER BY and LIMIT
β€’ COUNT, SUM, AVG, MIN, MAX
β€’ GROUP BY
β€’ HAVING vs WHERE
β€’ Use case like total sales per product
Outcome: You summarize data clearly.

Week 3: Joins fundamentals
β€’ INNER JOIN
β€’ LEFT JOIN
β€’ RIGHT JOIN
β€’ Join conditions
β€’ Handling NULL values
Outcome: You combine multiple tables correctly.

Week 4: Joins practice and cleanup
β€’ Duplicate rows after joins
β€’ SELF JOIN with examples
β€’ Data cleaning using SQL
β€’ Daily join-based questions
Outcome: You stop making join mistakes.

Month 2: Analytics-Level SQL
Week 5: Subqueries and CTEs
β€’ Subqueries in WHERE and SELECT
β€’ Correlated subqueries
β€’ Common Table Expressions
β€’ Readability and reuse
Outcome: You write structured queries.

Week 6: Window functions
β€’ ROW_NUMBER, RANK, DENSE_RANK
β€’ PARTITION BY and ORDER BY
β€’ Running totals
β€’ Top N per category problems
Outcome: You solve advanced analytics queries.

Week 7: Date and string analysis
β€’ Date functions for daily, monthly analysis
β€’ Year-over-year and month-over-month logic
β€’ String functions for text cleanup
Outcome: You handle real business datasets.

Week 8: Project and interview prep
β€’ Build a SQL project using sales or HR data
β€’ Write KPI queries
β€’ Explain query logic step by step
β€’ Daily interview questions practice
Outcome: You are SQL interview ready.

Practice platforms
β€’ LeetCode SQL
β€’ HackerRank SQL
β€’ Kaggle datasets

Double Tap β™₯️ For Detailed Explanation of Each Topic
❀11
Glad to see the amazing response on SQL roadmap. ❀️

Today, let's start with the first topic of SQL roadmap:

βœ… Introduction to SQL

SQL is the language you use to ask questions from data stored in databases. Companies store all important data in databases. Sales. Users. Payments. Inventory. When a manager asks a question, SQL pulls the answer.

What a database is
A database is an organized storage system for data. Think of it as a digital cupboard where each drawer holds related data. Each drawer is called a table.

What a table is
A table looks like an Excel sheet. It has rows and columns. Each table stores one type of data.

Example table: customers
- Columns
- customer_id
- name
- email
- city
- signup_date
- Rows
- Each row represents one customer
- One row equals one real-world record

How rows and columns work together
- Columns define what kind of data you store
- Rows hold actual values
- All rows follow the same column structure

Example row
- customer_id: 101
- name: Rahul
- email: [email protected]
- city: Pune
- signup_date: 2024-03-10

Why structure matters
- Clean structure makes data reliable
- Easy to filter, count, and analyze
- Required for accurate reporting

How SQL interacts with tables
- SQL reads data from tables
- SQL filters rows
- SQL selects columns
- SQL summarizes results

Simple SQL example
You ask the database to show names and cities of customers.
SELECT name, city FROM customers;


What happens behind the scenes
- Database scans the customers table
- Picks name and city columns
- Returns matching rows

Where you will use this daily
- Pull user lists
- Check sales numbers
- Validate data issues

Double Tap β™₯️ For More
❀9
Glad to see the amazing response on SQL roadmap. ❀️

Today, let's move to the next topic of SQL roadmap:

βœ… Database Basics: Primary Key and Foreign Key

Why Keys Exist
Databases store millions of rows. Keys help identify and connect data correctly. Without keys, data breaks fast.

Primary Key
- A primary key uniquely identifies each row in a table
- No two rows share the same primary key
- It never stays empty

Example Table: customers
- Columns: customer_id, name, email, city
- Primary key: customer_id

Why Primary Key Matters
- Prevents duplicate records
- Helps find a row fast
- Keeps data consistent

Foreign Key
- A foreign key links one table to another
- It creates relationships between tables

Example Table: orders
- Columns: order_id, customer_id, order_date, amount
- Foreign key: customer_id

Relationship Explained
- customers.customer_id is primary key
- orders.customer_id is foreign key
- One customer has many orders

Why Foreign Keys Matter
- Enable joins
- Prevent orphan records
- Maintain data integrity

Simple Join Idea
SQL matches customer_id in both tables. This gives customer name with order amount.

Where Beginners Go Wrong
- Using names instead of IDs
- Allowing duplicate primary keys
- Ignoring missing foreign key values

Checkpoint
- You understand primary keys
- You understand foreign keys
- You know how tables connect

Double Tap β™₯️ For More
❀4
Today, let's move to the next topic of SQL Roadmap:

Basic SQL Queries: SELECT, WHERE, Filtering πŸ–₯️

What SELECT Does
- SELECT chooses columns
- You decide what data you want to see
- Database returns only those columns

Example Table: customers
customer_id | name | city | signup_date
101 | Rahul | Pune | 2024-01-15
102 | Neha | Mumbai | 2024-02-10
103 | Amit | Delhi | 2024-03-05
104 | Priya | Pune | 2024-04-20

Basic SELECT
SELECT name, city FROM customers;

Output
name | city
Rahul | Pune
Neha | Mumbai
Amit | Delhi
Priya | Pune

What Happens
- Database scans all rows
- Returns name and city columns
- No filtering yet

Why SELECT Matters
- Smaller output
- Faster queries
- Clear analysis

What WHERE Does
- WHERE filters rows
- It answers conditions like who, when, how much

Think Like This
- SELECT decides columns
- WHERE decides rows

Basic WHERE Example
SELECT name, city FROM customers WHERE city = 'Pune';

Output
name | city
Rahul | Pune
Priya | Pune

Common Operators
- = equal
- != not equal
- > greater than
- < less than
- > =, <=

Example
SELECT name, signup_date FROM customers WHERE signup_date >= '2024-03-01';

Output
name | signup_date
Amit | 2024-03-05
Priya | 2024-04-20

Logical Filters
AND
- All conditions must match
Example: SELECT name FROM customers WHERE city = 'Pune' AND signup_date >= '2024-01-01';

Output
name
Rahul
Priya

OR
- Any condition can match
Example: SELECT name FROM customers WHERE city = 'Pune' OR city = 'Mumbai';

Output
name
Rahul
Neha
Priya

IN
- Shortcut for multiple OR conditions
Example: SELECT name FROM customers WHERE city IN ('Pune','Mumbai','Delhi');

Output
name
Rahul
Neha
Amit
Priya

BETWEEN
- Filters within a range
- Inclusive of start and end
Example: SELECT name FROM customers WHERE signup_date BETWEEN '2024-01-01' AND '2024-03-31';

Output
name
Rahul
Neha
Amit

Filtering Numbers
Example table: orders
order_id | customer_id | amount | order_date
1 | 101 | 3000 | 2024-01-16
2 | 102 | 6000 | 2024-02-11
3 | 103 | 4000 | 2024-03-06
SELECT order_id, amount FROM orders WHERE amount > 5000;

Output
order_id | amount
2 | 6000

Filtering Text
- Text values go inside quotes
- Case sensitivity depends on database

Filtering NULL Values
- NULL means missing value
- = NULL does not work
Correct way: SELECT name FROM customers WHERE city IS NULL;

Assume city is NULL for customer_id 103
Output
name
Amit

Exclude NULL: SELECT name FROM customers WHERE city IS NOT NULL;

Output
name
Rahul
Neha
Priya

How SELECT and WHERE Work Together
- FROM picks the table
- WHERE filters rows
- SELECT picks columns
- Result is sent back

Real Business Example
- Manager asks for Pune customers with orders above 5,000
- WHERE applies city and amount filters
- SELECT shows required columns

Assume orders table has customer_id 101 with amount 6000
SELECT name FROM customers WHERE city = 'Pune' AND customer_id IN (SELECT customer_id FROM orders WHERE amount > 5000);

Output
name
Rahul

Common Beginner Mistakes
- Using WHERE before FROM
- Forgetting quotes for text
- Using = NULL
- Writing SELECT * always

Double Tap β™₯️ For More
❀9