concept WHERE in category sql

This is an excerpt from Manning's book SQL Server MVP Deep Dives Vol. 2.
When the time comes for the DBA to execute the script, everything seems fine. But on Monday morning the users are inundating the help desk with calls that the application isn’t returning the data expected and the DBA team is now involved. As it turns out, the script not only added objects, but also updated the biggest and most used table’s data with an unqualified WHERE clause that modified every row. Because you’re the DBA, you’re automatically in the hot seat: it’s up to you to get the data back. Although you have backups, the database is 1 TB and it’ll take some time to get the file from the centralized backup solution and restore it.
1. Begin each major clause—for example, SELECT, FROM, JOIN, or WHERE—on a new line.
Confirm with the business owners that (a) inventory reports come in no less than once a week, and (b) this query is only concerned with current machine inventories. Add a WHERE clause to the CTE that limits the result set to the last month or weeks’ worth of data, at most (see listing 3).
Listing 3. CTE code with updated SELECT list and WHERE clause
USE VendCo; GO WITH CurrentInventory AS ( SELECT MachineID, SlotID, UnitsInStock, DateReported, ProductID , ROW_NUMBER() OVER( PARTITION BY MachineID, SlotID ORDER BY DateReported DESC) AS ReportGroup FROM VendUnit.MachineInventory WHERE DateReported > DateAdd(dd, -7, GetDate()) ) ...This WHERE clause is an improvement to the CTE—it limits the amount of data the query pulls back—but it’s not the ideal solution. See the following section to learn how to further improve this clause.

This is an excerpt from Manning's book SQL Server MVP Deep Dives.
As mentioned in the previous section, indexes are, in general, only to be used to improve performance, not for data protection. For SQL Server 2008, Microsoft has given us a WHERE clause on the CREATE INDEX syntax that also has data protection capabilities not found in UNIQUE constraints. If you want to protect for unique values except for when the value is some given value (most commonly, the use is unique values when not NULL), you can use a unique index with a WHERE clause. For example, if you have a table that holds persons, and you want to associate a unique customer number with the user if she becomes a customer, but not until, you could define an index such as the following:
CREATE INDEX AKSperson_customerNumber on person(customerNumber)
WHERE customerNumber is not nullIf you’re using a previous version of SQL Server, there are other methods to use, such as an indexed view where you define the view to exclude data like you did with the WHERE clause on the CREATE INDEX statement. Then you add a clustered unique index on the view. Either method is acceptable, and implements the same sort of constructs with the same overhead.
Another query type that will leave T-SQL developers scratching their heads when NULLs are involved is using a subquery in the WHERE clause with NOT IN. If there is a NULL value returned in the subquery results, no rows will be returned from the outer query.