How to Convert Rows Into Columns in SQL

How to Convert Rows Into Columns in SQL

A finance team once asked me for a report that looked simple on paper: one line per product, with four columns for Q1 through Q4. Their database stored sales the way databases should, with one row per product per quarter. An analyst had been exporting the data to Excel and rebuilding the layout by hand … Read more

How to Use CASE WHEN in SQL Server

How to Use CASE WHEN in SQL Server

A support lead once asked me why her “high priority” ticket report showed refunds as low priority. The developer had written a long chain of IF logic in the application, and every time the rules changed, the app and the report disagreed. We moved the rule into one T-SQL expression, and the two numbers matched … Read more

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