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.
๐1
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
โค8
๐๐ฎ๐๐ฎ ๐๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป ๐ช๐ถ๐๐ต ๐๐๐
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
โค13๐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!
โค2
๐ง๐๐ง๐ ๐๐ฅ๐๐ ๐ ๐๐๐ฅ๐ข-๐๐ก๐ง๐๐ฅ๐ก๐ฆ๐๐๐ฃ๐ฆ ๐ฎ๐ฌ๐ฎ๐ฒ! ๐
โ 100% FREE Learning Experience
โ Short Programs โ Approximately 4โ6 Hours
โ Practical, Real-World Tasks
โ Self-Paced & Online
โ Certificate Opportunities for Your Resume & LinkedIn
โ No Traditional Interview Process
๐ ๐๐ ๐ฝ๐น๐ผ๐ฟ๐ฒ ๐ง๐๐ง๐ ๐๐ฅ๐๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ-๐๐ป๐๐ฒ๐ฟ๐ป๐๐ต๐ถ๐ฝ๐๐:-
https://pdlink.in/4yWgaPn
โก Don't wait for your first job to start gaining practical experience!
โ 100% FREE Learning Experience
โ Short Programs โ Approximately 4โ6 Hours
โ Practical, Real-World Tasks
โ Self-Paced & Online
โ Certificate Opportunities for Your Resume & LinkedIn
โ No Traditional Interview Process
๐ ๐๐ ๐ฝ๐น๐ผ๐ฟ๐ฒ ๐ง๐๐ง๐ ๐๐ฅ๐๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ-๐๐ป๐๐ฒ๐ฟ๐ป๐๐ต๐ถ๐ฝ๐๐:-
https://pdlink.in/4yWgaPn
โก Don't wait for your first job to start gaining practical experience!
SQL Interview Series โ Part 13
๐ Question 13: Find Employees Earning More Than the Company Average
Suppose you have an Employee table:
โ Find all employees whose salary is greater than the average salary of the entire company.
๐ก Approach
First, calculate the average salary across all employees.
Then compare every employee's salary with that average.
Company average:
(50000 + 80000 + 60000 + 90000 + 70000) / 5
= 70000
Employees earning more than 70000:
Rahul โ 80000
Neha โ 90000
๐ SQL Solution
๐ How does it work?
The subquery:
calculates the company-wide average:
70000
The outer query then checks:
and returns only employees earning above that amount.
๐ Expected Result:
Double Tap โค๏ธ For More
๐ Question 13: Find Employees Earning More Than the Company Average
Suppose you have an Employee table:
employee_id | employee_name | department | salary
------------|---------------|------------|-------
1 | Amit | IT | 50000
2 | Rahul | IT | 80000
3 | Priya | HR | 60000
4 | Neha | HR | 90000
5 | Raj | Finance | 70000
โ Find all employees whose salary is greater than the average salary of the entire company.
๐ก Approach
First, calculate the average salary across all employees.
Then compare every employee's salary with that average.
Company average:
(50000 + 80000 + 60000 + 90000 + 70000) / 5
= 70000
Employees earning more than 70000:
Rahul โ 80000
Neha โ 90000
๐ SQL Solution
SELECT
employee_id,
employee_name,
department,
salary
FROM Employee
WHERE salary > (
SELECT AVG(salary)
FROM Employee
);
๐ How does it work?
The subquery:
SELECT AVG(salary)
FROM Employee;
calculates the company-wide average:
70000
The outer query then checks:
WHERE salary > 70000
and returns only employees earning above that amount.
๐ Expected Result:
employee_name | department | salary
--------------|------------|-------
Rahul | IT | 80000
Neha | HR | 90000
Double Tap โค๏ธ For More
โค7๐1
๐ ๐๐๐ฅ๐ข๐ฆ๐ข๐๐ง ๐๐ฅ๐๐ ๐๐๐ง๐ ๐ฆ๐๐๐๐ก๐๐ ๐ฅ๐๐ฆ๐ข๐จ๐ฅ๐๐๐ฆ ๐ฎ๐ฌ๐ฎ๐ฒ! ๐
Microsoft offers valuable FREE learning resources to help you develop in-demand technical skills! ๐ป
โ 100% FREE Learning Resources
โ Beginner-Friendly Tutorials
โ Self-Paced Learning
โ Practical Skills for Real-World Projects
โ Useful for Data Analytics & Data Science Career Preparation
๐ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐๐๐ ๐ฐ ๐๐ฅ๐๐ ๐ฅ๐ฒ๐๐ผ๐๐ฟ๐ฐ๐ฒ๐ ๐:-
https://pdlink.in/4k6ylNq
โก Your Data Science journey can start today โ completely FREE!
Microsoft offers valuable FREE learning resources to help you develop in-demand technical skills! ๐ป
โ 100% FREE Learning Resources
โ Beginner-Friendly Tutorials
โ Self-Paced Learning
โ Practical Skills for Real-World Projects
โ Useful for Data Analytics & Data Science Career Preparation
๐ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐๐๐ ๐ฐ ๐๐ฅ๐๐ ๐ฅ๐ฒ๐๐ผ๐๐ฟ๐ฐ๐ฒ๐ ๐:-
https://pdlink.in/4k6ylNq
โก Your Data Science journey can start today โ completely FREE!
๐๐ฎ๐๐ฎ ๐ฆ๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ & ๐ ๐ฎ๐ฐ๐ต๐ถ๐ป๐ฒ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด ๐๐ฅ๐๐ ๐ข๐ป๐น๐ถ๐ป๐ฒ ๐ ๐ฎ๐๐๐ฒ๐ฟ๐ฐ๐น๐ฎ๐๐ ๐
Build a strong foundation in Data Science .Explore Machine Learning and Deep Learning
Eligibility :- Students โข Freshers โข Working Professionals
๐ Date:- 23 October 2026
โฐ Time:- 7:00 PMโ9:00 PM IST
๐ป Mode:- Online
๐ ๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐๐ผ๐ฟ ๐๐ฅ๐๐๐ :-
https://pdlink.in/4i8MKGn
๐ข Share this opportunity with your friends and classmates.
Build a strong foundation in Data Science .Explore Machine Learning and Deep Learning
Eligibility :- Students โข Freshers โข Working Professionals
๐ Date:- 23 October 2026
โฐ Time:- 7:00 PMโ9:00 PM IST
๐ป Mode:- Online
๐ ๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐๐ผ๐ฟ ๐๐ฅ๐๐๐ :-
https://pdlink.in/4i8MKGn
๐ข Share this opportunity with your friends and classmates.
โค2
What does COUNT(*) count?
Anonymous Quiz
11%
A) Only non-NULL values
12%
B) Only unique rows
65%
C) All rows
12%
D) Only rows containing numeric values
โค1
What will this query return?
SELECT COUNT(salary)
FROM Employee; If the table has 10 rows and 3 employees have NULL salary:
SELECT COUNT(salary)
FROM Employee; If the table has 10 rows and 3 employees have NULL salary:
Anonymous Quiz
48%
10
52%
7
What does DISTINCT do?
Anonymous Quiz
2%
A) Sorts data
91%
B) Removes duplicate rows from the selected result
5%
C) Removes NULL values
2%
D) Groups data permanently
Which clause is evaluated to filter rows before GROUP BY?
Anonymous Quiz
27%
A) HAVING
57%
B) WHERE
15%
C) ORDER BY
2%
D) DISTINCT
What is the key difference between UNION and UNION ALL?
Anonymous Quiz
11%
A) UNION sorts data, UNION ALL doesn't
69%
B) UNION removes duplicates, UNION ALL keeps duplicates
16%
C) UNION works with tables, UNION ALL works with views
5%
D) There is no difference
๐๐ ๐๐ฆ๐ง๐๐ฅ ๐๐ซ๐๐๐ ๐๐ข๐ฅ ๐๐ฅ๐๐ ๐๐ก ๐ฎ๐ฌ๐ฎ๐ฒ! ๐
Discover 4 FREE Websites to learn Microsoft Excel from beginner to advanced level! ๐
๐ฅ FREE Learning Resources
๐ฅ Beginner to Advanced Tutorials
๐ฅ Practical Examples & Exercises
๐ฅ Learn Anytime, Anywhere
๐ ๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/4jDfcCP
โก One powerful skill can open doors to multiple career opportunities!
Discover 4 FREE Websites to learn Microsoft Excel from beginner to advanced level! ๐
๐ฅ FREE Learning Resources
๐ฅ Beginner to Advanced Tutorials
๐ฅ Practical Examples & Exercises
๐ฅ Learn Anytime, Anywhere
๐ ๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/4jDfcCP
โก One powerful skill can open doors to multiple career opportunities!