T-SQL reference › AND and OR

AND vs OR in SQL Server: put the brackets in

AND is both. OR is either. AND binds tighter, so a missing bracket means “all shipped rows, plus anything over £50” when you meant “shipped and over £50.”

What you meant

SELECT order_id, status, total_amount
FROM Orders
WHERE status = 'Shipped'
  AND (total_amount > 50 OR customer_id = 12);
No brackets

WHERE status = 'Shipped' AND total_amount > 50 OR customer_id = 12 includes every order for customer 12, including cancelled ones. When you type OR, add brackets.

Try it on Harbour & Co

Tom Gallagher (Warehouse Lead) needs this now: “Which orders are still Processing? I need order_id, order_date and total_amount.” Write the query, run it, send the results. Free.

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