TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #2741 1.98K
๐Ÿ“Š SQL Interview Queries โ€“ Intermediate Level

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ“ Query 01: Find employees earning more than the average salary

SELECT *
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

โ“ Query 02: Find department-wise employee count

SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department;

โ“ Query 03: Find departments with average salary greater than 60,000

SELECT department
FROM employees
GROUP BY department
HAVING AVG(salary) > 60000;

โ“ Query 04: Fetch employees who do not belong to any department

SELECT e.*
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;

โ“ Query 05: Find second highest salary

SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

โ“ Query 06: Get highest salary in each department

SELECT department, MAX(salary)
FROM employees
GROUP BY department;

โ“ Query 07: Fetch employees hired in the last 6 months

SELECT *
FROM employees
WHERE hire_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH);

โ“ Query 08: Find duplicate email IDs

SELECT email, COUNT(*)
FROM employees
GROUP BY email
HAVING COUNT(*) > 1;

โ“ Query 09: Rank employees by salary within each department

SELECT *,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;

โ“ Query 10: Fetch top 2 highest paid employees from each department

SELECT *
FROM (
SELECT *,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk <= 2;

๐Ÿ”ฅ Show some love with a reaction โค๏ธ
  • โค 13
More from @crackingthecodinginterview
  1. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  2. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  3. Oct 7, 2026๐Ÿš€ DSA Topics Every Programmer Should Know ๐Ÿ’ป๐Ÿ”ฅ ๐Ÿ“ฆ 1. Arrays โœ” Traversal โœ” Searching โœ” Sorโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  6. Sep 29, 2026โœ… Daily Coding Habits That Make You a Better Developer ๐Ÿง ๐Ÿ’ปโœจ 1๏ธโƒฃ Code Every Day (Even 30 Mโ€ฆ
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 โ†’