How to Find the Second Lowest Salary in SQL Server

How to Find the Second Lowest Salary in SQL Server

A payroll report lists the lowest-paid employee twice because two people earn the same amount. A developer skips the first row to find the second-lowest salary, but the query returns that same minimum again. The statement runs successfully; the business answer is wrong. Understanding how to find the second lowest salary in SQL Server starts … Read more

How to Find the Lowest Salary in SQL Server

How to Find the Lowest Salary in SQL Server

A payroll dashboard shows the lowest salary as zero, but HR says no active employee earns that amount. The query runs successfully. The problem is that it mixes placeholder values, inactive employees, and legitimate salaries without defining which records belong in the report. Knowing how to find the lowest salary in SQL Server starts with … Read more

How to Find 3rd Highest Salary in SQL Server

How to Find 3rd Highest Salary in SQL Server

A payroll report can return the wrong employee even when its SQL runs without errors. Two employees share the highest salary, and a query that simply selects the third row reports the second-highest salary instead of the third distinct amount. Understanding how to find 3rd highest salary in SQL Server starts with defining what “third … Read more

How to Find Nth Highest Salary in SQL Server

How to Find Nth Highest Salary in SQL Server

A payroll analyst at a mid-size staffing company once sent me a screenshot of a report. It showed the “third highest salary” in a department, and the number was wrong. Two people shared the top salary, and the query counted them as separate ranks. The report fed a compensation review, so a small logic bug … Read more

How to Find Second Highest Salary in SQL Server

How to Find Second Highest Salary in SQL Server

A payroll analyst at a mid-sized staffing company once sent me a spreadsheet and a worried email. Their HR dashboard showed the “second highest salary” per department, but two departments returned the same number as the top salary. The query had used TOP 1 with a sort, and it counted tied employees as separate ranks. … Read more

Scalar Function vs Table Valued Function SQL Server

Scalar Function vs Table Valued Function SQL Server

A retail client once called me because their order report had gone from 8 seconds to 14 minutes. Nothing had changed except one thing. A developer had wrapped a tax calculation in a user-defined function and called it for every row in a 3-million-row table. The query looked clean, but it ran one function call … Read more

How To Check If SQL Server Is Reachable

How To Check If SQL Server Is Reachable

A retail company called me on a Monday morning because its order app had stopped working. The error message said “A network-related or instance-specific error occurred,” and the team had already restarted the app servers twice. Nobody had checked whether the app servers could even reach the database. It turned out someone had changed a … Read more

How to Save Stored Procedure in SQL Server

How To Save Stored Procedure In SQL Server

When I trained a new database developer on my team last year, one of the first questions he asked me was, “How do I actually save this stored procedure once I’ve written it?” It’s a fair question, because unlike saving a Word document or an Excel file, saving a stored procedure in SQL Server doesn’t … Read more

Stored Procedure vs View

Stored Procedure vs View

When I first started managing databases for a company in Dallas, my team lead asked me to “just create a view instead of a procedure” for a reporting request. I remember pausing, because at that point I genuinely wasn’t sure why it mattered. Years later, after building and maintaining dozens of production databases, I can … Read more