How to Find Percentage of Total in SQL

How to Find Percentage of Total in SQL

A finance director once forwarded me a screenshot of her new sales dashboard. Every region showed a share of revenue of exactly 0 percent, except the largest one, which showed 100. The query looked fine. It divided each region’s sales by total sales and multiplied by 100. The columns held whole numbers, and SQL Server … 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 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 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 Find Even and Odd Numbers in SQL Server

How to Find Even and Odd Numbers in SQL Server

A warehouse team once asked me to split pick tickets between two packing lines. Odd quantities went to Line A, which handled loose items, and even quantities went to Line B, which handled boxed pairs. The developer wrote WHERE Quantity % 2 = 1 and shipped it. Within a week, returns adjustments were landing on … 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 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 Calculate Total Sales in SQL

How to Calculate Total Sales in SQL

A finance manager once sent me two dashboard numbers for the same month. They were $38,000 apart, and both came from “total sales” queries. One query summed order headers, and the other joined to order lines and counted each header total once per line. Nobody had been careless. The query had simply never been checked … 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

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