*🔥 Top SQL Interview Questions with Answers*
*🎯 1️⃣ Find 2nd Highest Salary*
📊 Table: employees
id | name | salary
1 | Rahul | 50000
2 | Priya | 70000
3 | Amit | 60000
4 | Neha | 70000
❓ Problem Statement: Find the second highest distinct salary from the employees table.
✅ Solution
SELECT MAX(salary) FROM employees WHERE salary < ( SELECT MAX(salary) FROM employees );
*🎯 2️⃣ Find Nth Highest Salary*
📊 Table: employees
id | name | salary
1 | A | 100
2 | B | 200
3 | C | 300
4 | D | 200
❓ Problem Statement: Write a query to find the 3rd highest salary.
✅ Solution
SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER(ORDER BY salary DESC) r FROM employees ) t WHERE r = 3;
*🎯 3️⃣ Find Duplicate Records*
📊 Table: employees
id | name
1 | Rahul
2 | Amit
3 | Rahul
4 | Neha
❓ Problem Statement: Find all duplicate names in the employees table.
✅ Solution
SELECT name, COUNT(_) FROM employees GROUP BY name HAVING COUNT(_) > 1;
*🎯 4️⃣ Customers with No Orders*
📊 Table: customers
customer_id | name
1 | Rahul
2 | Priya
3 | Amit
📊 Table: orders
order_id | customer_id
101 | 1
102 | 2
❓ Problem Statement: Find customers who have not placed any orders.
✅ Solution
SELECT c.name FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.customer_id IS NULL;
*🎯 5️⃣ Top 3 Salaries per Department*
📊 Table: employees
name | department | salary
A | IT | 100
B | IT | 200
C | IT | 150
D | HR | 120
E | HR | 180
❓ Problem Statement: Find the top 3 highest salaries in each department.
✅ Solution
SELECT * FROM ( SELECT name, department, salary, ROW_NUMBER() OVER( PARTITION BY department ORDER BY salary DESC ) r FROM employees ) t WHERE r <= 3;
*🎯 6️⃣ Running Total of Sales*
📊 Table: sales
date | sales
2024-01-01 | 100
2024-01-02 | 200
2024-01-03 | 300
❓ Problem Statement: Calculate the running total of sales by date.
✅ Solution
SELECT date, sales, SUM(sales) OVER(ORDER BY date) AS running_total FROM sales;
*🎯 7️⃣ Employees Above Average Salary*
📊 Table: employees
name | salary
A | 100
B | 200
C | 300
❓ Problem Statement: Find employees earning more than the average salary.
✅ Solution
SELECT name, salary FROM employees WHERE salary > ( SELECT AVG(salary) FROM employees );
*🎯 8️⃣ Department with Highest Total Salary*
📊 Table: employees
name | department | salary
A | IT | 100
B | IT | 200
C | HR | 500
❓ Problem Statement: Find the department with the highest total salary.
✅ Solution
SELECT department, SUM(salary) AS total_salary FROM employees GROUP BY department ORDER BY total_salary DESC LIMIT 1;
*🎯 9️⃣ Customers Who Placed Orders*
📊 Tables: Same as Q4
❓ Problem Statement: Find customers who have placed at least one order.
✅ Solution
SELECT name FROM customers c WHERE EXISTS ( SELECT 1 FROM orders o WHERE c.customer_id = o.customer_id );
*🎯 🔟 Remove Duplicate Records*
📊 Table: employees
id | name
1 | Rahul
2 | Rahul
3 | Amit
❓ Problem Statement: Delete duplicate records but keep one unique record.
✅ Solution
DELETE FROM employees WHERE id NOT IN ( SELECT MIN(id) FROM employees GROUP BY name );
*🚀 Pro Tip:*
👉 In interviews:
First explain logic
Then write query
Then optimize
*Double Tap ♥️ For More*
*🎯 1️⃣ Find 2nd Highest Salary*
📊 Table: employees
id | name | salary
1 | Rahul | 50000
2 | Priya | 70000
3 | Amit | 60000
4 | Neha | 70000
❓ Problem Statement: Find the second highest distinct salary from the employees table.
✅ Solution
SELECT MAX(salary) FROM employees WHERE salary < ( SELECT MAX(salary) FROM employees );
*🎯 2️⃣ Find Nth Highest Salary*
📊 Table: employees
id | name | salary
1 | A | 100
2 | B | 200
3 | C | 300
4 | D | 200
❓ Problem Statement: Write a query to find the 3rd highest salary.
✅ Solution
SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER(ORDER BY salary DESC) r FROM employees ) t WHERE r = 3;
*🎯 3️⃣ Find Duplicate Records*
📊 Table: employees
id | name
1 | Rahul
2 | Amit
3 | Rahul
4 | Neha
❓ Problem Statement: Find all duplicate names in the employees table.
✅ Solution
SELECT name, COUNT(_) FROM employees GROUP BY name HAVING COUNT(_) > 1;
*🎯 4️⃣ Customers with No Orders*
📊 Table: customers
customer_id | name
1 | Rahul
2 | Priya
3 | Amit
📊 Table: orders
order_id | customer_id
101 | 1
102 | 2
❓ Problem Statement: Find customers who have not placed any orders.
✅ Solution
SELECT c.name FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.customer_id IS NULL;
*🎯 5️⃣ Top 3 Salaries per Department*
📊 Table: employees
name | department | salary
A | IT | 100
B | IT | 200
C | IT | 150
D | HR | 120
E | HR | 180
❓ Problem Statement: Find the top 3 highest salaries in each department.
✅ Solution
SELECT * FROM ( SELECT name, department, salary, ROW_NUMBER() OVER( PARTITION BY department ORDER BY salary DESC ) r FROM employees ) t WHERE r <= 3;
*🎯 6️⃣ Running Total of Sales*
📊 Table: sales
date | sales
2024-01-01 | 100
2024-01-02 | 200
2024-01-03 | 300
❓ Problem Statement: Calculate the running total of sales by date.
✅ Solution
SELECT date, sales, SUM(sales) OVER(ORDER BY date) AS running_total FROM sales;
*🎯 7️⃣ Employees Above Average Salary*
📊 Table: employees
name | salary
A | 100
B | 200
C | 300
❓ Problem Statement: Find employees earning more than the average salary.
✅ Solution
SELECT name, salary FROM employees WHERE salary > ( SELECT AVG(salary) FROM employees );
*🎯 8️⃣ Department with Highest Total Salary*
📊 Table: employees
name | department | salary
A | IT | 100
B | IT | 200
C | HR | 500
❓ Problem Statement: Find the department with the highest total salary.
✅ Solution
SELECT department, SUM(salary) AS total_salary FROM employees GROUP BY department ORDER BY total_salary DESC LIMIT 1;
*🎯 9️⃣ Customers Who Placed Orders*
📊 Tables: Same as Q4
❓ Problem Statement: Find customers who have placed at least one order.
✅ Solution
SELECT name FROM customers c WHERE EXISTS ( SELECT 1 FROM orders o WHERE c.customer_id = o.customer_id );
*🎯 🔟 Remove Duplicate Records*
📊 Table: employees
id | name
1 | Rahul
2 | Rahul
3 | Amit
❓ Problem Statement: Delete duplicate records but keep one unique record.
✅ Solution
DELETE FROM employees WHERE id NOT IN ( SELECT MIN(id) FROM employees GROUP BY name );
*🚀 Pro Tip:*
👉 In interviews:
First explain logic
Then write query
Then optimize
*Double Tap ♥️ For More*
*SQL Cheat Sheet for Data Analysts 🗄️📊*
*1. SELECT*
What it is: Used to choose columns from a table
What it does: Returns specific columns of data
Query: Fetch name and salary
SELECT name, salary
FROM employees;
*2. FROM*
What it is: Specifies the table
What it does: Tells SQL where to get data from
Query: Fetch all data from employees
SELECT *
FROM employees;
*3. WHERE*
What it is: Filters rows based on condition
What it does: Returns only matching rows
Query: Employees with salary > 30000
SELECT *
FROM employees
WHERE salary > 30000;
*4. ORDER BY*
What it is: Sorts the data
What it does: Arranges rows in order
Query: Sort by salary (highest first)
SELECT *
FROM employees
ORDER BY salary DESC;
*5. COUNT()*
What it is: Counts rows
What it does: Returns total records
Query: Count employees
SELECT COUNT(*)
FROM employees;
*6. AVG()*
What it is: Calculates average
What it does: Returns mean value
Query: Average salary
SELECT AVG(salary)
FROM employees;
*7. GROUP BY*
What it is: Groups rows by column
What it does: Applies aggregation per group
Query: Avg salary per department
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
*8. HAVING*
What it is: Filters grouped data
What it does: Returns filtered groups
Query: Departments with avg salary > 40000
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 40000;
*9. INNER JOIN*
What it is: Combines matching rows from tables
What it does: Returns common data
Query: Employees with department names
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;
*10. LEFT JOIN*
What it is: Combines all left + matching right
What it does: Returns all left table data
Query: All employees with departments
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
*11. CASE WHEN*
What it is: Conditional logic
What it does: Creates values based on condition
Query: Categorize salary
SELECT name,
CASE
WHEN salary > 40000 THEN 'High'
ELSE 'Low'
END AS category
FROM employees;
*12. SUBQUERY*
What it is: Query inside another query
What it does: Uses result of inner query
Query: Salary above average
SELECT name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);
*13. RANK()*
What it is: Window function
What it does: Assigns rank without grouping
Query: Rank employees by salary
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
*14. DISTINCT*
What it is: Removes duplicates
What it does: Returns unique values
Query: Unique departments
SELECT DISTINCT department
FROM employees;
*15. LIKE*
What it is: Pattern matching
What it does: Filters text patterns
Query: Names starting with A
SELECT *
FROM employees
WHERE name LIKE 'A%';
*Double Tap ♥️ For More*
*1. SELECT*
What it is: Used to choose columns from a table
What it does: Returns specific columns of data
Query: Fetch name and salary
SELECT name, salary
FROM employees;
*2. FROM*
What it is: Specifies the table
What it does: Tells SQL where to get data from
Query: Fetch all data from employees
SELECT *
FROM employees;
*3. WHERE*
What it is: Filters rows based on condition
What it does: Returns only matching rows
Query: Employees with salary > 30000
SELECT *
FROM employees
WHERE salary > 30000;
*4. ORDER BY*
What it is: Sorts the data
What it does: Arranges rows in order
Query: Sort by salary (highest first)
SELECT *
FROM employees
ORDER BY salary DESC;
*5. COUNT()*
What it is: Counts rows
What it does: Returns total records
Query: Count employees
SELECT COUNT(*)
FROM employees;
*6. AVG()*
What it is: Calculates average
What it does: Returns mean value
Query: Average salary
SELECT AVG(salary)
FROM employees;
*7. GROUP BY*
What it is: Groups rows by column
What it does: Applies aggregation per group
Query: Avg salary per department
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
*8. HAVING*
What it is: Filters grouped data
What it does: Returns filtered groups
Query: Departments with avg salary > 40000
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 40000;
*9. INNER JOIN*
What it is: Combines matching rows from tables
What it does: Returns common data
Query: Employees with department names
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;
*10. LEFT JOIN*
What it is: Combines all left + matching right
What it does: Returns all left table data
Query: All employees with departments
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
*11. CASE WHEN*
What it is: Conditional logic
What it does: Creates values based on condition
Query: Categorize salary
SELECT name,
CASE
WHEN salary > 40000 THEN 'High'
ELSE 'Low'
END AS category
FROM employees;
*12. SUBQUERY*
What it is: Query inside another query
What it does: Uses result of inner query
Query: Salary above average
SELECT name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);
*13. RANK()*
What it is: Window function
What it does: Assigns rank without grouping
Query: Rank employees by salary
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
*14. DISTINCT*
What it is: Removes duplicates
What it does: Returns unique values
Query: Unique departments
SELECT DISTINCT department
FROM employees;
*15. LIKE*
What it is: Pattern matching
What it does: Filters text patterns
Query: Names starting with A
SELECT *
FROM employees
WHERE name LIKE 'A%';
*Double Tap ♥️ For More*
💻 *Top Coding Languages for Beginners & Their Uses* 🌟🚀
🔹 *Python* — Easy syntax, great for AI, web, and data
🔹 *JavaScript* — Web interactivity and frontend magic
🔹 *Java* — Enterprise apps and Android development
🔹 *HTML/CSS* — Website structure & styling basics
🔹 *Scratch* — Visual coding for kids & newbies
🔹 *SQL* — Managing and querying databases
🔹 *C#* — Game dev with Unity and Windows apps
🔹 *Ruby* — Simple web app building with Rails
🔹 *Swift* — Making apps for Apple devices
🔹 *PHP* — Server-side scripting for websites
💬 *Tap ❤️ if you found this useful!*
🔹 *Python* — Easy syntax, great for AI, web, and data
🔹 *JavaScript* — Web interactivity and frontend magic
🔹 *Java* — Enterprise apps and Android development
🔹 *HTML/CSS* — Website structure & styling basics
🔹 *Scratch* — Visual coding for kids & newbies
🔹 *SQL* — Managing and querying databases
🔹 *C#* — Game dev with Unity and Windows apps
🔹 *Ruby* — Simple web app building with Rails
🔹 *Swift* — Making apps for Apple devices
🔹 *PHP* — Server-side scripting for websites
💬 *Tap ❤️ if you found this useful!*

