How to Find Consecutive Dates in SQL

How to Find Consecutive Dates in SQL

A product manager once asked me for a list of users who had logged in five days in a row, so the team could send them a loyalty reward. An analyst had already tried. The query joined the login table to itself five times, took several minutes on a table with a few million rows, … Read more

How to Find NULL Values in a Table in SQL Server

How to Find NULL Values in a Table in SQL Server

A marketing analyst once asked me why the “customers without an email” report returned zero rows. The company had 12,000 customers and a visible gap in its email data. The query read WHERE Email = NULL. It ran without errors, returned nothing, and the campaign went out to the wrong list. That mistake is one … Read more

How to Generate Sequence Number in SQL Server (4 Ways)

How to Generate Sequence Number in SQL Server

A small e-commerce team once asked me why two customers had received the same order number on Black Friday. The app read MAX(OrderNumber), added 1, and inserted the row. Under light traffic it worked for months. Under load, two sessions read the same maximum at the same moment and both wrote the same “next” number. … Read more

How to Swap Two Column Values in SQL

How to Swap Two Column Values in SQL

A customer success team once imported 40,000 contacts from a CRM export. A week later, support noticed that thousands of records greeted customers as “Dear Johnson,” with first and last names reversed. The developer who found it said, “I’ll just copy one column into the other.” He stopped when he realized the first UPDATE would … Read more

SQL Query to Find Employees With Same Salary (4 Ways)

SQL Query to Find Employees With Same Salary

A compensation team is preparing a pay-equity review, and someone asks a simple question: “Which employees earn exactly the same salary?” The analyst exports the table to Excel, builds a pivot, and misses half the matches because two names were stored with different spacing. The same question takes seconds in the database, if you write … Read more

SQL Query to Count Employees in Each Department

SQL Query to Count Employees in Each Department

An HR analyst once sent me a headcount report that looked fine until the Legal director spotted the problem. Legal had three open roles and no employees, and the department was missing from the report entirely. A second department showed one extra person who didn’t exist. The query was short, and both mistakes came from … Read more

How to Get Top 5 Highest Salary in SQL

How to Get Top 5 Highest Salary in SQL

A few years ago, an HR team at a mid-size U.S. retailer asked me why their “top 5 earners” report showed seven people on some days and four on others. The developer had used a simple TOP 5 query. Two employees shared the same salary at the cutoff, and the report silently dropped one of … 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 highest paid Employee in SQL

How to find highest paid Employee in SQL

A finance analyst runs a quick query to pull the top earner for a budget review. The query returns one name, but two people share that salary, and a third employee with a NULL salary quietly slipped into the sort. The report goes to leadership, and the next day somebody asks why the second person … Read more

How to Find Maximum Salary in SQL

How to Find Maximum Salary in SQL

A finance analyst once sent me a screenshot of a payroll report with a cheerful note: “Highest salary is 95,000.” The CFO had just approved an offer at 140,000, so the number looked wrong. The report’s salary column had been imported as text. SQL Server compared the values as strings, and “95000” sorted above “140000” … Read more