SQL Programming Resources
76.7K 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
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:
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:
email

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
โค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:

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
๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ

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!
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
โค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!
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
โค2