SQL Interview Series โ Part 2
๐ Question 2: Find Duplicate Records
Suppose you have an Employee table:
Employee
โข employee_id
โข employee_name
โข department
โข email
โ Find all email addresses that appear more than once in the Employee table.
Example:
Expected result:
๐ก Approach
We need to:
1๏ธโฃ Group records by email.
2๏ธโฃ Count how many times each email appears.
3๏ธโฃ Keep only the emails whose count is greater than 1.
๐ SQL Solution
๐ Why use HAVING instead of WHERE?
"WHERE" filters individual rows before grouping.
"HAVING" filters groups after "GROUP BY".
Since we want to filter based on "COUNT(*)", we use "HAVING".
This means:
"Group employees by email and return only those groups containing more than one record."
๐ฏ Double Tap โค๏ธ For Part-3
๐ Question 2: Find Duplicate Records
Suppose you have an Employee table:
Employee
โข employee_id
โข employee_name
โข department
โข email
โ Find all email addresses that appear more than once in the Employee table.
Example:
employee_id | employee_name | email
------------|---------------|-------------------
1 | Amit | amit@gmail.com
2 | Rahul | rahul@gmail.com
3 | Priya | amit@gmail.com
4 | Neha | neha@gmail.com
5 | Raj | rahul@gmail.com
Expected result:
amit@gmail.com
rahul@gmail.com
๐ก Approach
We need to:
1๏ธโฃ Group records by email.
2๏ธโฃ Count how many times each email appears.
3๏ธโฃ Keep only the emails whose count is greater than 1.
๐ SQL Solution
SELECT email, COUNT(*) AS occurrence_count
FROM Employee
GROUP BY email
HAVING COUNT(*) > 1;
๐ Why use HAVING instead of WHERE?
"WHERE" filters individual rows before grouping.
"HAVING" filters groups after "GROUP BY".
Since we want to filter based on "COUNT(*)", we use "HAVING".
GROUP BY email
HAVING COUNT(*) > 1
This means:
"Group employees by email and return only those groups containing more than one record."
๐ฏ Double Tap โค๏ธ For Part-3
โค16๐1
๐ ๐ฎ๐๐๐ฒ๐ฟ ๐ฃ๐ผ๐๐ฒ๐ฟ ๐๐ ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐! ๐ฅ
Learn Power BI through these FREE learning resources
โ
โจ What You'll Learn:
๐ Interactive Dashboards
๐ Data Visualization
๐งน Data Transformation
๐ผ Real-World Reporting Skills
๐ฏ Beginner-Friendly โ No Coding Required
๐ฆ๐๐ฎ๐ฟ๐ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐
โ
โhttps://pdlink.in/4hznwlu
๐ซPerfect for Students โข Freshers โข Data Analyst Aspirants โข Working Professionals
Learn Power BI through these FREE learning resources
โ
โจ What You'll Learn:
๐ Interactive Dashboards
๐ Data Visualization
๐งน Data Transformation
๐ผ Real-World Reporting Skills
๐ฏ Beginner-Friendly โ No Coding Required
๐ฆ๐๐ฎ๐ฟ๐ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐
โ
โhttps://pdlink.in/4hznwlu
๐ซPerfect for Students โข Freshers โข Data Analyst Aspirants โข Working Professionals
โค1
SQL Interview Series โ Part 4
๐ Question 4: Find the Highest Salary in Each Department
Suppose you have an Employee table:
Employee
โข employee_id
โข employee_name
โข department
โข salary
Example:
โ Find the employee(s) with the highest salary in each department.
๐ก Approach
We need to find the maximum salary separately for every department.
A common approach is to use a window function.
"RANK()" assigns a ranking within each department.
๐ How does it work?
"PARTITION BY department"
โ Creates a separate ranking for each department.
"ORDER BY salary DESC"
โ Places the highest salary first.
"RANK()"
โ Gives the highest salary a rank of 1.
Finally:
WHERE rnk = 1
โ Keeps only the highest-paid employee(s).
Expected result:
โ ๏ธ Why use "RANK()"?
If two employees have the same highest salary, both will receive rank 1 and both will be returned.
For example:
IT
Rahul 90000
Amit 90000
Raj 80000
Both Rahul and Amit will be returned.
Double Tap โค๏ธ For More
๐ Question 4: Find the Highest Salary in Each Department
Suppose you have an Employee table:
Employee
โข employee_id
โข employee_name
โข department
โข salary
Example:
employee_id | employee_name | department | salary
------------|---------------|------------|-------
1 | Amit | IT | 60000
2 | Rahul | IT | 90000
3 | Priya | HR | 50000
4 | Neha | HR | 70000
5 | Raj | Finance | 85000
6 | Karan | Finance | 95000
โ Find the employee(s) with the highest salary in each department.
๐ก Approach
We need to find the maximum salary separately for every department.
A common approach is to use a window function.
"RANK()" assigns a ranking within each department.
SELECT
employee_id,
employee_name,
department,
salary
FROM (
SELECT
employee_id,
employee_name,
department,
salary,
RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS rnk
FROM Employee
) e
WHERE rnk = 1;
๐ How does it work?
"PARTITION BY department"
โ Creates a separate ranking for each department.
"ORDER BY salary DESC"
โ Places the highest salary first.
"RANK()"
โ Gives the highest salary a rank of 1.
Finally:
WHERE rnk = 1
โ Keeps only the highest-paid employee(s).
Expected result:
employee_name | department | salary
--------------|------------|-------
Rahul | IT | 90000
Neha | HR | 70000
Karan | Finance | 95000
โ ๏ธ Why use "RANK()"?
If two employees have the same highest salary, both will receive rank 1 and both will be returned.
For example:
IT
Rahul 90000
Amit 90000
Raj 80000
Both Rahul and Amit will be returned.
Double Tap โค๏ธ For More
โค7
๐๐ฃ๐ฎ๐ ๐๐ณ๐๐ฒ๐ฟ ๐ฃ๐น๐ฎ๐ฐ๐ฒ๐บ๐ฒ๐ป๐ ๐ง๐ฟ๐ฎ๐ถ๐ป๐ถ๐ป๐ด | ๐๐ฒ๐ฐ๐ผ๐บ๐ฒ ๐ฎ ๐๐๐น๐น๐๐๐ฎ๐ฐ๐ธ ๐๐ฒ๐๐ฒ๐น๐ผ๐ฝ๐ฒ๐ฟ ๐ช๐๐๐ต ๐๐ฒ๐ป๐๐
Start a high-paying tech careerโeven without prior coding experience
๐ ๐ฃ๐น๐ฎ๐ฐ๐ฒ๐บ๐ฒ๐ป๐ ๐๐ถ๐ด๐ต๐น๐ถ๐ด๐ต๐๐:
๐ฐ โน41 LPA highest salary
๐ โน7.4 LPA average salary
๐ 2,000+ students placed
๐ข 500+ hiring partners
โ 100% job assistance
๐ Skill Indiaโauthenticated certificate
๐ ๐๐ฝ๐ฝ๐น๐ ๐ก๐ผ๐๐:-
https://pdlink.in/3SuUeuD
๐ฏ HurryUp.....Limited Seats Available
Start a high-paying tech careerโeven without prior coding experience
๐ ๐ฃ๐น๐ฎ๐ฐ๐ฒ๐บ๐ฒ๐ป๐ ๐๐ถ๐ด๐ต๐น๐ถ๐ด๐ต๐๐:
๐ฐ โน41 LPA highest salary
๐ โน7.4 LPA average salary
๐ 2,000+ students placed
๐ข 500+ hiring partners
โ 100% job assistance
๐ Skill Indiaโauthenticated certificate
๐ ๐๐ฝ๐ฝ๐น๐ ๐ก๐ผ๐๐:-
https://pdlink.in/3SuUeuD
๐ฏ HurryUp.....Limited Seats Available
๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ๐๐ผ๐ณ๐ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ถ๐๐ต ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ฒ๐! ๐๐ฅ
Upgrade your resume with Microsoft learning opportunities. Explore courses covering some of today's most in-demand tech skills!
๐ฏ Perfect for Students โข Freshers โข Job Seekers โข Working Professionals
โ Learn at your own pace โ Build career-relevant skills โ Explore available certificates and credentials
๐ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ๐๐ผ๐ณ๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ฒ๐ฟ๐ฒ ๐
https://pdlink.in/3Uf5trR
๐ฅ Share with friends looking to upskill in 2026!
Upgrade your resume with Microsoft learning opportunities. Explore courses covering some of today's most in-demand tech skills!
๐ฏ Perfect for Students โข Freshers โข Job Seekers โข Working Professionals
โ Learn at your own pace โ Build career-relevant skills โ Explore available certificates and credentials
๐ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ๐๐ผ๐ณ๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ฒ๐ฟ๐ฒ ๐
https://pdlink.in/3Uf5trR
๐ฅ Share with friends looking to upskill in 2026!
โค1
๐ฎ๐ณ ๐๐ข๐ฉ๐๐ฅ๐ก๐ ๐๐ก๐ง ๐ข๐ ๐๐ก๐๐๐ โ ๐๐๐๐ง๐ ๐๐ก๐ง๐๐ฅ๐ก๐ฆ๐๐๐ฃ๐ฆ ๐ฎ๐ฌ๐ฎ๐ฒ ๐
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
โค2
๐ง๐ข๐ฃ ๐๐ฅ๐๐ ๐๐ข๐จ๐ฅ๐ฆ๐๐ฆ ๐ง๐ข ๐๐ข๐ข๐ฆ๐ง ๐ฌ๐ข๐จ๐ฅ ๐ฆ๐๐๐๐๐ฆ ๐๐ก ๐ฎ๐ฌ๐ฎ๐ฒ!
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
โค2