T-SQL reference › COUNT

COUNT in SQL Server: rows, values, and distinct people

Three different questions. See also <a href="/guides/count-vs-count-distinct">COUNT vs COUNT DISTINCT</a>.

Last week

SELECT COUNT(*) AS orders,
       COUNT(customer_id) AS orders_with_customer,
       COUNT(DISTINCT customer_id) AS customers
FROM Orders
WHERE order_date >= DATEADD(day, -7, CAST(GETDATE() AS date))
  AND order_date <  CAST(GETDATE() AS date);

Try it on Harbour & Co

Amy Nair (Finance Lead) needs this now: “Three numbers for shipped orders, all time: how many, total revenue, average order.” Write the query, run it, send the results. On the paid plan.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets