TGViewer
Data Analytics Data Analytics @sqlspecialist · 111K subscribers
Post #2417 10.5K
✅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!
  • ❤ 26
  • 👏 2
More from @sqlspecialist
  1. Oct 13, 2026📊 Data Analyst Interview Series — Part 9 Guys, let's continue our Data Analyst Interview…
  2. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  3. Oct 12, 20268️⃣ You receive a dataset with inconsistent date formats. How would you clean it? Sample A…
  4. Oct 12, 2026📊 Data Analyst Interview Series — Part 7 Guys, let's continue our Data Analyst Interview…
  5. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  6. Oct 9, 2026“Here, the data is sorted by the second column in descending order and the first five rows…
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 →