TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2683 8.13K
Real-world SQL Questions with Answers ๐Ÿ”ฅ

Let's dive into some real-world SQL questions with a mini dataset.

๐Ÿ“Š Dataset: employees
id  name    department  salary  manager_id
1 Aditi HR 30000 5
2 Rahul IT 50000 6
3 Neha IT 60000 6
4 Aman Sales 40000 7
5 Kiran HR 70000 NULL
6 Mohit IT 80000 NULL
7 Suresh Sales 65000 NULL
8 Pooja HR 30000 5


1. Find average salary per department
SELECT department, AVG(salary) AS avg_salary 
FROM employees
GROUP BY department;


2. Find employees earning above department average
SELECT name, department, salary 
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department = e.department
);


3. Find highest salary in each department
SELECT department, MAX(salary) AS max_salary 
FROM employees
GROUP BY department;


4. Find employees who earn more than their manager
SELECT e.name 
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE e.salary > m.salary;


5. Count employees in each department
SELECT department, COUNT(*) AS total_employees 
FROM employees
GROUP BY department;


6. Find departments with more than 2 employees
SELECT department, COUNT(*) AS total 
FROM employees
GROUP BY department
HAVING COUNT(*) > 2;


7. Find second highest salary
SELECT MAX(salary) 
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);


8. Find employees without managers
SELECT name 
FROM employees
WHERE manager_id IS NULL;


9. Rank employees by salary
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank 
FROM employees;


10. Find duplicate salaries
SELECT salary, COUNT(*) 
FROM employees
GROUP BY salary
HAVING COUNT(*) > 1;


11. Top 2 highest salaries
SELECT DISTINCT salary 
FROM employees
ORDER BY salary DESC
LIMIT 2;


Double Tap โค๏ธ For More
  • โค 51
  • ๐Ÿ‘ 3
  • ๐Ÿ‘ 1
More from @sqlspecialist
  1. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  2. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  4. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  5. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’