TGViewer
Channel Public Channel
SQL Programming Resources

SQL Programming Resources

@sqlanalyst

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
Subscribers
76.7K
Photos
597
Videos
1
Links
567

Showing posts older than #2531 ยท Back to latest

Older Posts 20 shown
Post #2529 2.31K
  • โค 2
Post #2527 1.83K
๐Ÿ”ฅ SQL Interview Case Studies (Advanced Business Scenarios) ๐Ÿ’ฏ

๐Ÿง  Case Study 1: Find Repeat Customers
๐Ÿ“Š Orders Table
order_id customer_id
1 101
2 102
3 101

โ“ Business Question
Find customers who placed more than 1 order.

โœ… Solution
SELECT customer_id,
COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 1;

๐Ÿง  Case Study 2: Highest Paid Employee Per Department
โ“ Business Question
Find highest paid employee in every department.

โœ… Solution
WITH RankedEmployees AS (
SELECT *,
ROW_NUMBER() OVER(
PARTITION BY department
ORDER BY salary DESC
) rn
FROM employees
)
SELECT *
FROM RankedEmployees
WHERE rn = 1;

๐Ÿง  Case Study 3: Find Inactive Customers
โ“ Business Question
Customers who haven't ordered in the last 6 months.

โœ… Solution
SELECT c.customer_id,
c.name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.name
HAVING MAX(o.order_date) <
DATE_SUB(CURDATE(), INTERVAL 6 MONTH);

๐Ÿง  Case Study 4: Product Generating Highest Revenue
๐Ÿ“Š Sales Table
product_id amount

โœ… Solution
SELECT product_id,
SUM(amount) AS revenue
FROM sales
GROUP BY product_id
ORDER BY revenue DESC
LIMIT 1;

๐Ÿง  Case Study 5: Employee Salary Above Department Average

โœ… Solution
WITH DeptAvg AS (
SELECT department,
AVG(salary) avg_salary
FROM employees
GROUP BY department
)

SELECT e.*
FROM employees e
JOIN DeptAvg d
ON e.department = d.department
WHERE e.salary > d.avg_salary;

๐Ÿง  Case Study 6: Running Total Sales
โ“ Business Question
Calculate cumulative sales.

โœ… Solution
SELECT order_date,
amount,
SUM(amount) OVER(
ORDER BY order_date
) AS running_total
FROM sales;

๐Ÿง  Case Study 7: Top 3 Products Per Category

โœ… Solution
WITH RankedProducts AS (
SELECT *,
ROW_NUMBER() OVER(
PARTITION BY category
ORDER BY sales DESC
) rn
FROM products
)
SELECT *
FROM RankedProducts
WHERE rn <= 3;

๐ŸŽฏ Practice Tasks
1๏ธโƒฃ Find customer with highest number of orders
2๏ธโƒฃ Find lowest salary employee per department
3๏ธโƒฃ Find products never sold
4๏ธโƒฃ Find departments with average salary > company average
5๏ธโƒฃ Find month with lowest sales

โšก Mini Challenge ๐Ÿ”ฅ
Banking Scenario
Tables:
Accounts
account_id customer_name

Transactions
transaction_id account_id amount transaction_date

Question
๐Ÿ‘‰ Find the top 3 customers with highest total transaction amount in the last 1 year.

๐Ÿ”ฅ Interview Tip
When solving case studies:
1๏ธโƒฃ Understand business question
2๏ธโƒฃ Identify tables
3๏ธโƒฃ Identify JOINs
4๏ธโƒฃ Identify Aggregations
5๏ธโƒฃ Decide whether Window Function is needed

Double Tap โค๏ธ For More
  • โค 5
  • ๐Ÿ‘ 1
Post #2525 1.91K
How to Build an Impressive Data Analysis Portfolio

As a data analyst, your portfolio is your personal brand. It showcases not only your technical skills but also your ability to solve real-world problems.

Having a strong, well-rounded portfolio can set you apart from other candidates and help you land your next job or freelance project.

Here's how to build a portfolio that will impress potential employers or clients.

1. Start with a Strong Introduction:
Before jumping into your projects, introduce yourself with a brief summary. Include your background, areas of expertise (e.g., Python, R, SQL), and any special achievements or certifications. This is your chance to give context to your portfolio and show your personality.

Tip: Make your introduction engaging and concise. Add a professional photo and link to your LinkedIn or personal website.


2. Showcase Real-World Projects:
The most powerful way to showcase your skills is through real-world projects. If you donโ€™t have work experience yet, create your own projects using publicly available datasets (e.g., Kaggle, UCI Machine Learning Repository). These projects should highlight the full data analysis processโ€”from data collection and cleaning to analysis and visualization.

Examples of project ideas:
- Analyzing customer data to identify purchasing trends.
- Predicting stock market trends based on historical data.
- Analyzing social media sentiment around a brand or event.


3. Focus on Impactful Data Visualizations:
Data visualization is a key part of data analysis, and itโ€™s crucial that your portfolio highlights your ability to tell stories with data. Use tools like Tableau, Power BI, or Python (matplotlib, Seaborn) to create compelling visualizations that make complex data easy to understand.

Tips for great visuals:
- Use color wisely to highlight key insights.
- Avoid clutter; focus on clarity.
- Create interactive dashboards that allow users to explore the data.


4. Explain Your Methodology:
Employers and clients will want to know how you approached each project. For each project in your portfolio, explain the methodology you used, including:
- The problem or question you aimed to solve.
- The data sources you used.
- The tools and techniques you applied (e.g., statistical tests, machine learning models).
- The insights or results you discovered.

Make sure to document this in a clear, step-by-step manner, ideally with code snippets or screenshots.


5. Include Code and Jupyter Notebooks:
If possible, include links to your code or Jupyter Notebooks so potential employers or clients can see your technical expertise firsthand. Platforms like GitHub or GitLab are perfect for hosting your code. Make sure your code is well-commented and easy to follow.

Tip: Organize your projects in a structured way on GitHub, using descriptive README files for each project.


6. Feature a Blog or Case Studies:
If you enjoy writing, consider adding a blog or case study section to your portfolio. Writing about the data analysis process and the insights youโ€™ve uncovered helps demonstrate your ability to communicate complex ideas in a digestible way. It also allows you to reflect on your projects and show your thought leadership in the field.

Blog post ideas:
- A breakdown of a data analysis project youโ€™ve completed.
- Tips for aspiring data analysts.
- Reviews of tools and technologies you use regularly.

7. Continuously Update Your Portfolio:
Your portfolio is a living document. As you gain more experience and complete new projects, regularly update it to keep it fresh and relevant. Always add new skills, projects, and certifications to reflect your growth as a data analyst.

I have curated best 80+ top-notch Data Analytics Resources ๐Ÿ‘‡๐Ÿ‘‡
https://whatsapp.com/channel/0029VaGgzAk72WTmQFERKh02

Like this post for more content like this ๐Ÿ‘โ™ฅ๏ธ

Share with credits: https://t.me/sqlspecialist

Hope it helps :)
  • โค 7
Post #2523 1.88K
๐Ÿ”ฅ SQL Interview Case Studies & Real-World Business Problems

๐Ÿง  Case Study 1: Top 3 Customers by Revenue
๐Ÿ“Š Orders Table
order_id customer_id amount
1 101 500
2 102 1000
3 101 700

โ“ Business Question
Find the top 3 customers by total revenue.

โœ… Solution
SELECT customer_id,
SUM(amount) AS total_revenue
FROM orders
GROUP BY customer_id
ORDER BY total_revenue DESC
LIMIT 3;

๐Ÿง  Case Study 2: Department with Highest Average Salary

โ“ Business Question
Which department has the highest average salary?

โœ… Solution
SELECT department,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC
LIMIT 1;

๐Ÿง  Case Study 3: Customers Who Never Ordered
๐Ÿ“Š Tables
Customers customer_id name
Orders order_id customer_id

โ“ Business Question
Find customers who never placed an order.

โœ… Solution
SELECT c.customer_id,
c.name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;

๐Ÿง  Case Study 4: Second Highest Salary

โ“ Business Question
Find employees with the second highest salary.

โœ… Solution
SELECT *
FROM employees
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary)
FROM employees
)
);

๐Ÿง  Case Study 5: Monthly Sales Trend

โ“ Business Question
Calculate monthly sales.

โœ… Solution
SELECT YEAR(order_date) AS year,
MONTH(order_date) AS month,
SUM(amount) AS sales
FROM orders
GROUP BY YEAR(order_date),
MONTH(order_date)
ORDER BY year, month;

๐ŸŽฏ Practice Tasks
1๏ธโƒฃ Find top-selling product
2๏ธโƒฃ Find employee with highest salary in each department
3๏ธโƒฃ Find customers with more than 5 orders
4๏ธโƒฃ Find month with highest sales
5๏ธโƒฃ Find departments having more than 10 employees

โšก Mini Challenge ๐Ÿ”ฅ
E-commerce Scenario

Tables:
Customers customer_id name
Orders order_id customer_id amount order_date

Business Question
Find the top 5 customers by total spending in the last 12 months.

๐Ÿ”ฅ Interview Tip
Most SQL interviews are NOT about syntax.

They're about:
โœ… Understanding business problem
โœ… Choosing the right approach
โœ… Writing efficient SQL

Double Tap โค๏ธ For More
  • โค 7
Post #2521 2.44K
โœ… SQL Aggregations with Interview Q&A ๐Ÿ“Š๐Ÿงฎ

Aggregation functions help summarize large datasets. Combine them with GROUP BY to analyze grouped data.

1๏ธโƒฃ COUNT()
Returns the number of records.
SELECT COUNT(*) FROM employees;


2๏ธโƒฃ SUM()
Adds up values in a column.
SELECT dept_id, SUM(salary)  
FROM employees
GROUP BY dept_id;


3๏ธโƒฃ AVG()
Returns the average of values.
SELECT AVG(salary) FROM employees;


4๏ธโƒฃ MAX() / MIN()
Returns the highest/lowest value.
SELECT MAX(salary), MIN(salary) FROM employees;


5๏ธโƒฃ GROUP BY
Groups rows that have the same values in specified columns.
SELECT dept_id, COUNT(*)  
FROM employees
GROUP BY dept_id;


6๏ธโƒฃ HAVING
Filters groups after aggregation (unlike WHERE which filters rows).
SELECT dept_id, AVG(salary)  
FROM employees
GROUP BY dept_id
HAVING AVG(salary) > 50000;


โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”

Real-World Interview Questions + Answers

Q1: Whatโ€™s the difference between WHERE and HAVING?
A: WHERE filters rows before grouping. HAVING filters after aggregation.

Q2: Can you use aggregate functions without GROUP BY?
A: Yes. Without GROUP BY, the function applies to the entire table.

Q3: How do you find departments with more than 5 employees?
SELECT dept_id, COUNT(*)  
FROM employees
GROUP BY dept_id
HAVING COUNT(*) > 5;


Q4: Can you group by multiple columns?
A: Yes.
GROUP BY dept_id, job_title


Q5: How do you calculate total and average salary per department?
SELECT dept_id, SUM(salary), AVG(salary)  
FROM employees
GROUP BY dept_id;


๐Ÿ’ฌ Tap โค๏ธ for more!
  • โค 4
Post #2519 2.69K
๐Ÿ”ฅ Now, Letโ€™s move to the next topic:

๐Ÿ”ฅ Dynamic SQL

Frequently Asked for Database Developer Roles ๐Ÿ’ฏ

๐Ÿง  1. What is Dynamic SQL?
Dynamic SQL is SQL code that is built and executed at runtime.

๐Ÿ‘‰ Query is created dynamically as a string
๐Ÿ‘‰ Executed later by the database

Unlike normal SQL:
SELECT _ FROM employees

Dynamic SQL:
'SELECT _ FROM employees WHERE department = ''IT'''

โšก 2. Why Use Dynamic SQL?
โœ” Flexible queries
โœ” Dynamic filtering
โœ” Dynamic table names
โœ” Dynamic sorting

Used when query structure changes based on user input.

๐Ÿ”ฅ 3. Dynamic SQL Example (MySQL)
SET @sql_query = 'SELECT * FROM employees';
PREPARE stmt FROM @sql_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

๐Ÿ”ฅ 4. Dynamic Filter Example
SET @dept = 'IT';
SET @sql_query = CONCAT(
'SELECT * FROM employees
WHERE department = ''',
@dept,
''''
);
PREPARE stmt FROM @sql_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

๐Ÿ”ฅ 5. Dynamic Table Name Example
SET @table_name = 'employees';
SET @sql_query = CONCAT('SELECT * FROM ', @table_name);
PREPARE stmt FROM @sql_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

โš ๏ธ 6. SQL Injection Risk
Bad Example โŒ
SELECT _ FROM users WHERE username = 'user_input'
Can be exploited by attackers.

โœ… Safe Approach
Use parameterized queries whenever possible.
PREPARE stmt FROM 'SELECT _ FROM employees WHERE department = ?'

๐ŸŽฏ 7. Real-World Uses
โœ” Search filters
โœ” Reporting systems
โœ” Dynamic dashboards
โœ” ETL processes
โœ” Admin tools

๐ŸŽฏ 8. Practice Tasks
1. Create dynamic SELECT query
2. Create dynamic WHERE condition
3. Create dynamic ORDER BY query
4. Query dynamic table name
5. Execute prepared statement

โšก Mini Challenge ๐Ÿ”ฅ
๐Ÿ‘‰ Create a dynamic SQL query that:
Accepts a department name
Returns employees from that department
Sorts them by salary DESC

๐Ÿ”ฅ Most asked question: What is the difference between Static SQL and Dynamic SQL?


Aspect: Query
Static SQL: Fixed query
Dynamic SQL: Built at runtime

Aspect: Performance
Static SQL: Faster
Dynamic SQL: More flexible

Aspect: Complexity
Static SQL: Less complex
Dynamic SQL: More complex

Aspect: Risk
Static SQL: Lower risk
Dynamic SQL: Higher risk if not parameterized


Double Tap โค๏ธ For More
  • โค 2
Post #2511 2.27K
๐Ÿ”ฅ Now, Letโ€™s move to the next topic:

โœ… Recursive CTEs

One of the most advanced and impressive SQL topics for interviews ๐Ÿ’ฏ

๐Ÿง  1. What is a Recursive CTE?

A Recursive CTE is a CTE that refers to itself.

๐Ÿ‘‰ Used for hierarchical or recursive data

Examples:

โœ” Employee-Manager hierarchy

โœ” Organization chart

โœ” Folder structure

โœ” Category trees

โšก 2. Structure of Recursive CTE

A Recursive CTE has two parts:

1๏ธโƒฃ Anchor Query

Starting point

2๏ธโƒฃ Recursive Query

Repeats until condition is met

๐Ÿ”ฅ 3. Basic Example โ€“ Generate Numbers 1 to 5

WITH RECURSIVE Numbers AS (
SELECT 1 AS num
UNION ALL
SELECT num + 1
FROM Numbers
WHERE num < 5
)

SELECT * FROM Numbers;


โœ… Output

num

1

2

3

4

5

๐Ÿ”ฅ 4. Employee Hierarchy Example

Employees Table

emp_id | name | manager_id

1 | CEO | NULL

2 | Amit | 1

3 | Neha | 2

4 | Ravi | 2

Recursive Query

WITH RECURSIVE EmployeeHierarchy AS (
SELECT
emp_id,
name,
manager_id,
1 AS level
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT
e.emp_id,
e.name,
e.manager_id,
eh.level + 1
FROM employees e
JOIN EmployeeHierarchy eh
ON e.manager_id = eh.emp_id
)

SELECT * FROM EmployeeHierarchy;


๐ŸŽฏ 5. Real-World Uses

Organizational Charts

CEO โ†’ Manager โ†’ Employee

Product Categories

Electronics โ†’ Laptop โ†’ Gaming Laptop

Folder Structures

Root โ†’ Folder โ†’ Subfolder

โšก 6. Important Rule

Every recursive CTE needs:

โœ” Anchor Query

โœ” Recursive Query

โœ” Stopping Condition

Without stopping condition โŒ Infinite loop

๐ŸŽฏ 7. Practice Tasks

1. Generate numbers 1โ€“10

2. Generate even numbers

3. Build employee hierarchy

4. Find reporting levels

5. Create category tree

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Generate multiplication table of 5 (5 to 50) using Recursive CTE

Example:

5

10

15

20

...

50

Most asked question:

๐Ÿ‘‰ Difference between CTE and Recursive CTE?

โœ… CTE = Temporary result set

โœ… Recursive CTE = Temporary result set that references itself

Double Tap โค๏ธ For More
  • โค 7
  • ๐Ÿ‘ 1
Post #2509 2.99K
๐Ÿ”ฅ Now, Letโ€™s move to the next topic:

User Defined Functions (UDFs) in SQL

๐Ÿง  1. What is a User Defined Function (UDF)?

A User Defined Function (UDF) is a custom function created by users.

๐Ÿ‘‰ Accepts input parameters

๐Ÿ‘‰ Performs calculations or logic

๐Ÿ‘‰ Returns a single value

Think like this ๐Ÿ‘‡

โœ” Built-in Function โ†’ SUM(), AVG(), COUNT()

โœ” User Defined Function โ†’ Created by YOU

โšก 2. Why Use UDFs?

โœ” Reuse business logic

โœ” Reduce code repetition

โœ” Improve readability

โœ” Easier maintenance

๐Ÿ”ฅ 3. Basic Function Example

๐Ÿ‘‰ Function to Calculate Bonus

DELIMITER //

CREATE FUNCTION CalculateBonus(
salary DECIMAL(10,2)
)
RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
RETURN salary ** 0.10;
END //

DELIMITER ;


โ–ถ๏ธ 4. Execute Function

SELECT CalculateBonus(50000);


Output: 5000

๐Ÿ”ฅ 5. Using Function in Query

SELECT
name,
salary,
CalculateBonus(salary) AS bonus
FROM employees;


โšก 6. Function with Multiple Parameters

DELIMITER //

CREATE FUNCTION TotalIncome(
salary DECIMAL(10,2),
bonus DECIMAL(10,2)
)
RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
RETURN salary + bonus;
END //

DELIMITER ;


โ–ถ๏ธ Execute

SELECT TotalIncome(50000, 5000);


Output: 55000

๐Ÿ”ฅ 7. Difference Between Function & Procedure

Feature : Function : Procedure

Must return value : Yes : May or may not return

Used inside SELECT : Yes : Called using CALL

Focus : Calculations : Actions

๐ŸŽฏ 8. Practice Tasks

1. Create function to calculate tax

2. Create function to calculate annual salary

3. Create function to calculate total income

4. Use function inside SELECT query

5. Compare function vs procedure

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Create a function: EmployeeGrade(salary)

Rules:

โ€ข salary โ‰ฅ 70000 โ†’ 'A'

โ€ข salary โ‰ฅ 50000 โ†’ 'B'

โ€ข otherwise โ†’ 'C'

Then use it for all employees.

๐Ÿ”ฅ Pro Tip (Interview Gold)

Most asked interview question:

๐Ÿ‘‰ When should you use Function instead of Procedure?

โœ… Use Function when:

โ€ข A value must be returned

โ€ข Logic is reusable inside SELECT statements

โ€ข Calculations are needed frequently

Double Tap โค๏ธ For More
  • โค 8
Post #2508 2.7K
๐Ÿ’ซ ๐—”๐—ง๐—ง๐—˜๐—ก๐—ง๐—œ๐—ข๐—ก ๐—ฆ๐—ง๐—จ๐——๐—˜๐—ก๐—ง๐—ฆ & ๐—™๐—ฅ๐—˜๐—ฆ๐—›๐—˜๐—ฅ๐—ฆ ๐Ÿ”ฅ

This could be the biggest opportunity you join in 2026!

๐Ÿ† Win from โ‚น50 Lakh+ Prize Pool
๐ŸŽ“ Open to All Students
๐Ÿค– Explore AI & Innovation
๐Ÿ“œ Earn Recognition
๐Ÿ’ฏ Registration is FREE

Imagine adding a national innovation challenge to your resume before graduation.

โšก Registration Closes Soon

๐—ฅ๐—ฒ๐—ด๐—ถ๐˜€๐˜๐—ฒ๐—ฟ ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜ ๐Ÿ‘‡:-

https://pdlink.in/4fFWOqX

Share with your friends, classmates, teammates & colleagues who shouldn't miss this opportunity.
  • โค 1
Post #2507 2.89K
  • โค 3
Post #2505 2.69K
  • โค 1
Post #2504 2.86K
  • โค 2
Post #2503 2.69K
  • โค 2
Post #2502 2.29K
  • โค 4
Post #2501 2.8K
๐Ÿš€ Now, Letโ€™s move to the next topic:

โœ… SQL Views vs Materialized Views

๐Ÿง  1. What is a View?

A View is a virtual table created from a query.

๐Ÿ‘‰ Stores only the SQL query

๐Ÿ‘‰ Does NOT store actual data

๐Ÿ‘‰ Always shows latest data from underlying tables

CREATE VIEW employee_view AS

SELECT * FROM employees;

๐Ÿง  2. What is a Materialized View?

A Materialized View stores the query result physically.

๐Ÿ‘‰ Stores actual data

๐Ÿ‘‰ Faster for reporting queries

๐Ÿ‘‰ Needs refresh to get latest data

CREATE MATERIALIZED VIEW employee_summary AS

SELECT department,

AVG(salary) AS avg_salary

FROM employees

GROUP BY department;

โšก 3. View vs Materialized View

Feature : View : Materialized View

Stores Data : โŒ No : โœ… Yes

Storage Space : Very Low : Higher

Query Speed : Slower : Faster

Real-Time Data : โœ… Yes : โŒ Needs Refresh

Best For : OLTP Systems : Reporting & Analytics

๐Ÿ”ฅ 4. Example

View

SELECT * FROM employee_view;

Every execution runs the underlying query again.

Materialized View

SELECT * FROM employee_summary;

Reads precomputed data directly.

๐Ÿ”„ 5. Refresh Materialized View

REFRESH MATERIALIZED VIEW employee_summary;

Updates stored results with latest data.

๐ŸŽฏ 6. Real-World Usage

Views Used In:

โœ” Banking Applications

โœ” HR Systems

โœ” Transaction Systems

Materialized Views Used In:

โœ” BI Dashboards

โœ” Data Warehouses

โœ” Reporting Systems

๐ŸŽฏ 7. Practice Tasks

1. Create a view for high-salary employees

2. Query data from a view

3. Create department salary summary view

4. Create materialized view for sales summary

5. Refresh materialized view

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Create a materialized view showing:

โ€ข department

โ€ข total employees

โ€ข average salary

Then query it to find the department with the highest average salary.

Most asked interview question:

๐Ÿ‘‰ When would you choose a Materialized View over a View?

โœ… Answer: "When query execution is expensive and data changes less frequently, Materialized Views improve performance significantly."

Double Tap โค๏ธ For More
  • โค 8
Older posts โ†’
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’