๐ฎ๐ณ ๐๐ข๐ฉ๐๐ฅ๐ก๐ ๐๐ก๐ง ๐ข๐ ๐๐ก๐๐๐ โ ๐๐๐๐ง๐ ๐๐ก๐ง๐๐ฅ๐ก๐ฆ๐๐๐ฃ๐ฆ ๐ฎ๐ฌ๐ฎ๐ฒ ๐
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!
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:
โ 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
โโโโโโโโโโโโโโโโโโ
๐ How does it work?
We use the Employee table twice:
"e" โ represents the employee
"m" โ represents the manager
The join condition:
connects each employee with their manager.
Then:
keeps only employees whose salary is higher than their manager's.
Expected result:
โโโโโโโโโโโโโโโโโโ
๐ฏ 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:
The key is to understand what each table alias represents before writing the query.
Double Tap โค๏ธ For More
๐ 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.
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
๐ 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
๐ 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!
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
Orders
โ 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
๐ 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:
keeps only those customers.
๐ Expected Result:
โ ๏ธ Interview Trap
You can also solve this using "NOT EXISTS":
"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
๐ 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:
โ 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
๐ 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:
โ ๏ธ 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:
๐ฏ Double Tap โค๏ธ For More
๐ 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!
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! ๐ฅ
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:
โ 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
Expected result:
๐ 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:
Now both IT and HR would be returned.
๐ฏ Double Tap โค๏ธ For More
๐ 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!
โ 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!