SQL Programming Resources
76.6K subscribers
599 photos
1 video
12 files
569 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
๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€

Students and freshers can explore internship opportunities through the AICTE Internship Portal across multiple domains:

๐Ÿ’ป AI, Data Science & Web Development
๐Ÿ“ˆ Digital Marketing & Business Development
๐Ÿ’ฐ Finance & Accounting
โœ๏ธ Content Writing & Graphic Design
โš™๏ธ Mechanical, Civil & Electrical Engineering

๐Ÿ”— ๐—–๐—ต๐—ฒ๐—ฐ๐—ธ ๐—ข๐—ฝ๐—ฝ๐—ผ๐—ฟ๐˜๐˜‚๐—ป๐—ถ๐˜๐—ถ๐—ฒ๐˜€ & ๐—”๐—ฝ๐—ฝ๐—น๐˜† ๐Ÿ‘‡

https://pdlink.in/4y3vcBl

๐Ÿ“ข Share this opportunity with your friends and classmates!
SQL Interview Series โ€” Part 5

๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Manager

Suppose you have an Employee table:

Employee

โ€ข employee_id

โ€ข employee_name

โ€ข manager_id

โ€ข salary

Example:

employee_id | employee_name | manager_id | salary
------------|---------------|------------|-------
1 | Amit | NULL | 100000
2 | Rahul | 1 | 120000
3 | Priya | 1 | 90000
4 | Neha | 2 | 130000
5 | Raj | 2 | 110000


โ“ Find employees whose salary is greater than their manager's salary.

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

๐Ÿ’ก Approach

The Employee table contains both employees and managers.

So, we need to join the table with itself.

This is called a Self Join.

๐Ÿ“Œ SQL Solution

SELECT
e.employee_name AS employee,
e.salary AS employee_salary,
m.employee_name AS manager,
m.salary AS manager_salary
FROM Employee e
JOIN Employee m
ON e.manager_id = m.employee_id
WHERE e.salary > m.salary;


โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

๐Ÿ”Ž How does it work?

We use the Employee table twice:

"e" โ†’ represents the employee

"m" โ†’ represents the manager

The join condition:

e.manager_id = m.employee_id


connects each employee with their manager.

Then:

WHERE e.salary > m.salary


keeps only employees whose salary is higher than their manager's.

Expected result:

employee | employee_salary | manager | manager_salary
---------|-----------------|---------|---------------
Rahul | 120000 | Amit | 100000
Neha | 130000 | Rahul | 120000


โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

๐ŸŽฏ Interview Tip

Whenever the same table contains a relationship such as:

โ€ข Employee โ†’ Manager

โ€ข Employee โ†’ Mentor

โ€ข Employee โ†’ Supervisor

โ€ข Employee โ†’ Parent

think about using a Self Join.

A very common interview pattern is:

FROM Employee e
JOIN Employee m
ON e.manager_id = m.employee_id


The key is to understand what each table alias represents before writing the query.

Double Tap โค๏ธ For More
โค7๐Ÿ‘1
๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ?

Want to build apps, uncover insights, or create AI solutions? Explore certification courses that match your career goals! ๐ŸŽฏ

๐Ÿ’ป ๐—™๐˜‚๐—น๐—น ๐—ฆ๐˜๐—ฎ๐—ฐ๐—ธ ๐——๐—ฒ๐˜ƒ๐—ฒ๐—น๐—ผ๐—ฝ๐—บ๐—ฒ๐—ป๐˜
๐Ÿ‘‰ https://pdlink.in/3SuUeuD

๐Ÿ“Š ๐——๐—ฎ๐˜๐—ฎ ๐—”๐—ป๐—ฎ๐—น๐˜†๐˜๐—ถ๐—ฐ๐˜€
๐Ÿ‘‰ https://pdlink.in/45vk5ph

๐Ÿ’ซ๐—”๐—œ ๐—˜๐—ป๐—ด๐—ถ๐—ป๐—ฒ๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด
๐Ÿ‘‰ https://pdlink.in/4fWJVID

๐Ÿ”ฅ Choose your path, sharpen your skills, and take the next step towards your tech career!

๐Ÿ“Œ Save this post & share it with a friend planning to upskill.
SQL Interview Series โ€” Part 6

๐Ÿ“Œ Question 6: Find the Top 2 Highest-Paid Employees in Each Department

Suppose you have:

Employee

employee_id employee_name department salary

1 Amit IT 60000

2 Rahul IT 90000

3 Priya IT 80000

4 Neha HR 70000

5 Raj HR 50000

6 Karan HR 90000

โ“ Find the top 2 highest-paid employees from each department.

๐Ÿ’ก Approach

This is a classic window

-function interview question.

We need to:

1๏ธโƒฃ Divide employees by department.

2๏ธโƒฃ Rank employees by salary within each department.

3๏ธโƒฃ Keep only employees with rank โ‰ค 2.

SQL Solution

SELECT
employee_id,
employee_name,
department,
salary
FROM (
SELECT
employee_id,
employee_name,
department,
salary,
ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS rn
FROM Employee
) e
WHERE rn <= 2;


๐Ÿ”Ž How does it work?

PARTITION BY department

Creates a separate ranking for each department.

ORDER BY salary DESC

Places the highest salary first.

ROW_NUMBER()

Assigns 1, 2, 3... within each department.

Finally:

WHERE rn <= 2

keeps the top two employees from each department.

Expected Result

employee_name department salary

Rahul IT 90000

Priya IT 80000

Karan HR 90000

Neha HR 70000

โš ๏ธ Interview Trap

What if two employees have the same salary?

For example:

Rahul 90000

Priya 90000

Amit 80000

ROW_NUMBER() will still assign unique numbers:

Rahul 1

Priya 2

Amit 3

Double Tap โค๏ธ For More
โค3
๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ!

Explore FREE online learning resources to develop in-demand skills and improve your career opportunities!

๐ŸŽ“ Why Explore These Courses?
โœ… Learn valuable professional skills
โœ… Improve your resume
โœ… Build practical knowledge
โœ… Learn at your own pace

๐—˜๐˜…๐—ฝ๐—น๐—ผ๐—ฟ๐—ฒ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐—›๐—ฒ๐—ฟ๐—ฒ๐Ÿ‘‡

https://pdlink.in/3VBdt6V

๐Ÿ’ก Learn New Skills โ†’ Build Confidence โ†’ Unlock Career Opportunities!
SQL Interview Series โ€” Part 8

๐Ÿ“Œ Question 8: Find Customers Who Have Never Placed an Order

Suppose you have two tables:

Customers

customer_id | customer_name
------------|--------------
1 | Amit
2 | Rahul
3 | Priya
4 | Neha
5 | Raj


Orders

order_id | customer_id
---------|------------
101 | 1
102 | 3
103 | 1
104 | 5


โ“ Find all customers who have never placed an order.

๐Ÿ’ก Approach

We need to find customers from the "Customers" table who do not have a matching record in the "Orders" table.

A "LEFT JOIN" is a common and interview-friendly approach.

๐Ÿ“Œ SQL Solution

SELECT
c.customer_id,
c.customer_name
FROM Customers c
LEFT JOIN Orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;


๐Ÿ”Ž How does it work?

"LEFT JOIN" keeps every customer, even if they don't have an order.

For customers without an order, the columns from "Orders" become "NULL".

Then:

WHERE o.customer_id IS NULL


keeps only those customers.

๐Ÿ“Œ Expected Result:

customer_id | customer_name
------------|--------------
2 | Rahul
4 | Neha


โš ๏ธ Interview Trap

You can also solve this using "NOT EXISTS":

SELECT
c.customer_id,
c.customer_name
FROM Customers c
WHERE NOT EXISTS (
SELECT 1
FROM Orders o
WHERE o.customer_id = c.customer_id
);


"NOT EXISTS" is often preferred when you specifically want to test whether a matching row exists.

Be careful with "NOT IN" when NULL values can exist in the subquery, because NULL can lead to unexpected results.

๐ŸŽฏ Double Tap โค๏ธ For More
โค5
SQL Interview Series โ€” Part 11

๐Ÿ“Œ Question 11: Calculate a Running Total of Sales

Suppose you have a Sales table:

sale_id | sale_date  | amount
--------|------------|-------
1 | 2026-01-01 | 1000
2 | 2026-01-02 | 1500
3 | 2026-01-03 | 800
4 | 2026-01-04 | 1200


โ“ Calculate the cumulative (running) sales amount for each day.

๐Ÿ’ก Approach

A running total means:

Current day's sales + all previous sales

For example:

1000

1000 + 1500 = 2500

2500 + 800 = 3300

3300 + 1200 = 4500

The easiest approach is to use the "SUM()" window function.

๐Ÿ“Œ SQL Solution

SELECT
sale_date,
amount,
SUM(amount) OVER (
ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM Sales
ORDER BY sale_date;


๐Ÿ”Ž How does it work?

1๏ธโƒฃ "ORDER BY sale_date"

Determines the order in which the running total is calculated.

2๏ธโƒฃ "SUM(amount) OVER(...)"

Calculates the sum without collapsing the rows like a normal "GROUP BY".

3๏ธโƒฃ "UNBOUNDED PRECEDING"

Means:

Start from the first row.

4๏ธโƒฃ "CURRENT ROW"

Means:

Include the current row in the total.

So the calculation becomes:

Date        Amount    Running Total
---------- -------- -------------
Jan 01 1000 1000
Jan 02 1500 2500
Jan 03 800 3300
Jan 04 1200 4500


โš ๏ธ Interview Trap

Don't use:

GROUP BY sale_date

for a running total.

"GROUP BY" collapses rows into groups.

A window function allows us to calculate an aggregate while keeping individual rows.

Also, if multiple rows can have the same "sale_date", be careful about the ordering. A unique column such as "sale_id" may need to be included:

SUM(amount) OVER (
ORDER BY sale_date, sale_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)


๐ŸŽฏ Double Tap โค๏ธ For More
โค6
๐——๐—ฎ๐˜๐—ฎ ๐˜€๐—ฐ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ช๐—ถ๐˜๐—ต ๐—”๐—œ๐Ÿ˜

Future Ready Data Science and AI Skills You Build

->Build Python, Machine Learning, and AI Skills
->Get Mentored by Industry Experts
->Practice with Mock Interviews
->Ace Your Placements & Land Dream Tech Job

๐Ÿค 500+ Partner Companies |๐ŸŽ“ 2000+ Students Placed

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

https://pdlink.in/47pezFz

๐Ÿ“ฒ Save & Share with Friends Preparing for Data Science & AI Jobs!
๐—œ๐—•๐—  ๐—™๐—ฅ๐—˜๐—˜ ๐—ง๐—˜๐—–๐—› ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! ๐ŸŽ“

Explore IBM Cognitive Class and build in-demand tech skills without spending money on course fees!

๐Ÿ“š ๐—ง๐—ผ๐—ฝ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜๐—ผ ๐—˜๐˜…๐—ฝ๐—น๐—ผ๐—ฟ๐—ฒ:-

๐Ÿ’ซ Artificial Intelligence & Machine Learning
๐Ÿ”ฅPython for Data Science
๐Ÿ“Š Data Science Fundamentals
โ˜๏ธ Cloud Computing & Big Data

๐Ÿ”— ๐—ฆ๐˜๐—ฎ๐—ฟ๐˜ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป๐—ถ๐—ป๐—ด ๐—›๐—ฒ๐—ฟ๐—ฒ ๐Ÿ‘‡

https://pdlink.in/4zGswuX

๐Ÿ“ฒ Save & Share with friends preparing for tech careers! ๐Ÿ”ฅ
SQL Interview Series โ€” Part 12

๐Ÿ“Œ Question 12: Find the Department with the Highest Average Salary

Suppose you have an Employee table:

employee_id | employee_name | department | salary
------------|---------------|------------|-------
1 | Amit | IT | 60000
2 | Rahul | IT | 80000
3 | Priya | HR | 70000
4 | Neha | HR | 90000
5 | Raj | Finance | 85000
6 | Karan | Finance | 95000


โ“ Find the department that has the highest average salary.

๐Ÿ’ก Approach

First, calculate the average salary for every department.

Then, find the department with the highest average.

Department averages:

IT โ†’ (60000 + 80000) / 2 = 70000

HR โ†’ (70000 + 90000) / 2 = 80000

Finance โ†’ (85000 + 95000) / 2 = 90000

Therefore:

Finance โ†’ 90000

๐Ÿ“Œ SQL Solution

SELECT
department,
AVG(salary) AS avg_salary
FROM Employee
GROUP BY department
ORDER BY avg_salary DESC
LIMIT 1;


Expected result:

department | avg_salary
-----------|-----------
Finance | 90000


๐Ÿ”Ž How does it work?

1๏ธโƒฃ "GROUP BY department"

Creates one group for each department.

2๏ธโƒฃ "AVG(salary)"

Calculates the average salary within each department.

3๏ธโƒฃ "ORDER BY avg_salary DESC"

Places the department with the highest average first.

4๏ธโƒฃ "LIMIT 1"

Returns only the top department.

โš ๏ธ Interview Trap

What if two departments have the same highest average salary?

For example:

IT โ†’ 80000

HR โ†’ 80000

Finance โ†’ 75000

"LIMIT 1" would return only one department.

If the interviewer asks:

๐Ÿ‘‰ "Find all departments having the highest average salary"

then use a ranking approach:

SELECT department, avg_salary
FROM (
SELECT
department,
AVG(salary) AS avg_salary,
DENSE_RANK() OVER (
ORDER BY AVG(salary) DESC
) AS rnk
FROM Employee
GROUP BY department
) d
WHERE rnk = 1;


Now both IT and HR would be returned.

๐ŸŽฏ Double Tap โค๏ธ For More
โค11๐Ÿ‘1
๐Ÿฐ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐—ง๐—ผ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—–๐—ผ๐—ฑ๐—ถ๐—ป๐—ด ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ˜

โœ… 100% FREE YouTube Learning
โœ… Beginner-Friendly Programming Tutorials
โœ… Learn Popular & In-Demand Tech Skills
โœ… Study at Your Own Pace
โœ… Build a Strong Foundation for Coding Interviews

๐Ÿ”— ๐—”๐—ฐ๐—ฐ๐—ฒ๐˜€๐˜€ ๐—”๐—Ÿ๐—Ÿ ๐Ÿฐ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐Ÿ‘‡:-

https://pdlink.in/3WcuMvi

โšก Your coding journey can start todayโ€”completely FREE!