100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

SELECT and Filtering

Learn how to retrieve exactly the rows and columns you need from SQL Server tables using SELECT, WHERE, and related filtering clauses.

QueryingBeginner8 min readJul 10, 2026
Analogies

The SELECT Statement

The SELECT statement is the core tool for reading data in T-SQL. A basic query names the columns you want and the table they live in: SELECT CustomerID, FirstName, LastName FROM Sales.Customer. SQL Server processes this logically in an order different from how you type it — FROM and WHERE are evaluated before SELECT — which is why column aliases defined in SELECT cannot be reused in the same statement's WHERE clause.

🏏

Cricket analogy: Just as a scorer first decides which match and innings to pull data from before deciding which columns (runs, balls, fours) to display on the scoreboard, SQL Server resolves FROM before it resolves the SELECT list.

Every column in the SELECT list can be given an alias with AS, and expressions — not just raw columns — are fully supported: SELECT UnitPrice * Quantity AS LineTotal. Using SELECT * is convenient during ad-hoc exploration but is discouraged in production code because it breaks when columns are added or reordered, and it prevents the query optimizer from using narrow covering indexes.

🏏

Cricket analogy: Naming a computed column LineTotal with AS is like a commentator renaming a raw stat 'strike rate' instead of reading out the formula runs divided by balls times 100 every time — SELECT * is like reading the entire scorecard when you only needed the strike rate.

Filtering with WHERE

The WHERE clause filters rows before any grouping or aggregation happens, using comparison operators (=, <>, >, <, BETWEEN, IN, LIKE) combined with AND, OR, and NOT. Because SQL Server uses three-valued logic, comparisons against NULL never evaluate to TRUE — WHERE Region = NULL always returns zero rows, and you must use IS NULL or IS NOT NULL instead.

🏏

Cricket analogy: Filtering WHERE Runs > 50 is like a selector shortlisting only players who cross a half-century in the last ten innings, and just as a blank scorecard entry (retired, did not bat) can't be compared numerically, NULL can't be compared with '=' in T-SQL.

sql
SELECT
    OrderID,
    CustomerID,
    OrderDate,
    TotalAmount = Quantity * UnitPrice
FROM Sales.Orders
WHERE OrderDate >= '2026-01-01'
  AND Status IN ('Shipped', 'Delivered')
  AND Region IS NOT NULL
ORDER BY OrderDate DESC;

Pattern Matching and Ranges

LIKE enables pattern matching with wildcards: % matches any sequence of characters and _ matches exactly one, so WHERE LastName LIKE 'Mc%' finds every surname starting with 'Mc'. BETWEEN is inclusive on both ends, meaning WHERE OrderDate BETWEEN '2026-01-01' AND '2026-01-31' includes rows on both boundary dates, which trips up developers expecting exclusive ranges.

🏏

Cricket analogy: Searching a database WHERE PlayerName LIKE 'Sharma%' pulls up every Sharma on the roster the way a scorer scans for a surname prefix, and BETWEEN over-scores 45 AND 99 inclusively counts both a 45 and a 99 as qualifying innings.

TOP combined with ORDER BY is the standard way to limit result sets in SQL Server, e.g. SELECT TOP 10 * FROM Sales.Orders ORDER BY TotalAmount DESC. Without ORDER BY, TOP returns an arbitrary set of rows because there is no guaranteed row order in a relational table.

Never use WHERE Column = NULL to find null values — it silently returns zero rows because NULL is not equal to anything, including itself. Always use WHERE Column IS NULL or WHERE Column IS NOT NULL.

  • SELECT defines which columns/expressions to return; FROM and WHERE are logically evaluated first.
  • Column aliases (AS) can rename raw columns or computed expressions for readability.
  • SELECT * is fine for exploration but hurts maintainability and index usage in production queries.
  • WHERE filters rows using comparisons, AND/OR/NOT, IN, BETWEEN, and LIKE.
  • NULL requires IS NULL / IS NOT NULL — standard equality operators never match NULL.
  • BETWEEN is inclusive of both boundary values.
  • TOP requires ORDER BY to produce a deterministic, meaningful subset of rows.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#SQLServerTSQLStudyNotes#SELECTAndFiltering#SELECT#Filtering#Statement#WHERE#StudyNotes#SkillVeris#ExamPrep