TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview · 52.2K subscribers
Post #2926 1.85K
✅SQL Interview Questions with Answers

1️⃣ Write a query to find the second highest salary in the employee table.
SELECT MAX(salary) 
FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee);


2️⃣ Get the top 3 products by revenue from sales table.
SELECT product_id, SUM(revenue) AS total_revenue 
FROM sales
GROUP BY product_id
ORDER BY total_revenue DESC
LIMIT 3;


3️⃣ Use JOIN to combine customer and order data.
SELECT c.customer_name, o.order_id, o.order_date 
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id;

(That's an INNER JOIN—use LEFT JOIN to include all customers, even without orders.)

4️⃣ Difference between WHERE and HAVING?
⦁ WHERE filters rows before aggregation (e.g., on individual records).
⦁ HAVING filters rows after aggregation (used with GROUP BY on aggregates). 
  Example:
SELECT department, COUNT(*) 
FROM employee
GROUP BY department
HAVING COUNT(*) > 5;


5️⃣ Explain INDEX and how it improves performance. 
An INDEX is a data structure that improves the speed of data retrieval. 
It works like a lookup table and reduces the need to scan every row in a table. 
Especially useful for large datasets and on columns used in WHERE, JOIN, or ORDER BY—think 10x faster queries, but it slows inserts/updates a bit.

💬 Tap ❤️ for more!
  • ❤ 5
More from @crackingthecodinginterview
  1. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  2. Oct 7, 2026🚀 DSA Topics Every Programmer Should Know 💻🔥 📦 1. Arrays ✔ Traversal ✔ Searching ✔ Sor…
  3. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
  4. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
  5. Sep 29, 2026✅ Daily Coding Habits That Make You a Better Developer 🧠💻✨ 1️⃣ Code Every Day (Even 30 M…
  6. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
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 →