TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2175 6.66K
✅ Advanced SQL Practice Questions with Answers 🧠📝

1️⃣ Get the second highest salary from the employees table.
SELECT MAX(salary)  
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);


2️⃣ List employees who earn more than the average salary.
SELECT name, salary  
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);


3️⃣ Show department-wise highest paid employee.
SELECT department, name, salary  
FROM (
SELECT *,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk
FROM employees
) AS ranked
WHERE rnk = 1;


4️⃣ Display total sales made by each employee in 2023.
SELECT emp_id, SUM(amount) AS total_sales  
FROM sales
WHERE YEAR(sale_date) = 2023
GROUP BY emp_id;


5️⃣ Retrieve products with price above average in their category.
SELECT p.name, p.category, p.price  
FROM products p
WHERE price > (
SELECT AVG(price)
FROM products
WHERE category = p.category
);


6️⃣ Identify duplicate emails in the users table.
SELECT email, COUNT(*)  
FROM users
GROUP BY email
HAVING COUNT(*) > 1;


7️⃣ Rank customers based on total purchase amount.
SELECT customer_id,
SUM(amount) AS total_spent,
RANK() OVER (ORDER BY SUM(amount) DESC) AS rank
FROM orders
GROUP BY customer_id;


💬 Double Tap ❤️ For More!
  • ❤ 24
  • 👍 1
More from @sqlanalyst
  1. Oct 13, 2026SQL Interview Series — Part 8 📌 Question 8: Find Customers Who Have Never Placed an Order…
  2. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  3. Oct 12, 2026SQL Interview Series — Part 6 📌 Question 6: Find the Top 2 Highest-Paid Employees in Each…
  4. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  5. Oct 9, 2026SQL Interview Series — Part 5 📌 Question 5: Find Employees Who Earn More Than Their Manag…
  6. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
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 →