TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2945 5.21K
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ: 
You have 2 minutes to solve this SQL query.

Q: Find the employee(s) who received the highest salary increment compared to their previous salary.

Assume the table structure: 
salary_history(employee_id, salary, effective_date)

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

WITH salary_changes AS (
    SELECT
        employee_id,
        salary,
        effective_date,
        salary - LAG(salary) OVER (
            PARTITION BY employee_id
            ORDER BY effective_date
        ) AS salary_increment
    FROM salary_history
)
SELECT
    employee_id,
    salary_increment
FROM (
    SELECT
        employee_id,
        salary_increment,
        DENSE_RANK() OVER (
            ORDER BY salary_increment DESC
        ) AS rnk
    FROM salary_changes
    WHERE salary_increment IS NOT NULL
) ranked
WHERE rnk = 1;


๐Ÿ’ก Explanation: 
This query calculates each employee's salary increment and then finds the highest increment across all employees.

โ€ข LAG(salary) retrieves the employee's previous salary
โ€ข The difference between the current and previous salary gives the increment
โ€ข DENSE_RANK() ranks increments from highest to lowest
โ€ข The outer query returns all employees tied for the highest salary increment

This question tests your understanding of: 
โœ… LAG() Window Function 
โœ… Common Table Expressions (CTEs) 
โœ… DENSE_RANK() 
โœ… Time-Series Data Analysis

๐ŸŽฏ Expected Output Example

Employee ID | Salary Increment 
101 | 20,000 
205 | 20,000 

Both employees received the largest salary increase.

๐Ÿš€ Why Interviewers Ask This? 
This is a classic window function interview question. It evaluates your ability to compare a row with its previous rowโ€”a common requirement in payroll, finance, and audit systems.

๐Ÿš€ Tip for SQL Job Seekers: 
Master these analytical window functions: 
LAG() / LEAD() / FIRST_VALUE() / LAST_VALUE() / NTILE() 

These functions are frequently tested in product-based companies and data-focused interviews because they simplify complex row-by-row comparisons.

โค๏ธ React with โค๏ธ for more interview challenges!
  • โค 13
More from @sqlspecialist
  1. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  2. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
  3. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 5 Guys, let's continue our Data Analyst Interviewโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026๐Ÿ”Ÿ What is the difference between UNION and JOIN? Sample Answer: "JOIN combines columns frโ€ฆ
  6. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 4 Guys, let's continue our Data Analyst Interviewโ€ฆ
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 โ†’