Your First T-SQL Query
The anatomy of a basic query is SELECT column_list FROM table_name, run against a database you've selected in SSMS (either via the dropdown or a USE statement). SELECT lets you name exactly the columns you want back, in whatever order you want them, rather than retrieving an entire row's worth of data. Running the query, typically with F5 or the Execute button in SSMS, sends the batch to the server and displays the returned rows in the Results grid below the query editor.
Cricket analogy: A SELECT statement is like requesting a specific stat from the scorebook: SELECT PlayerName, Runs FROM Innings is like asking the scorer 'just show me names and runs for this innings,' not the entire ball-by-ball commentary.
Filtering with WHERE
The WHERE clause filters which rows are returned, evaluated using operators like =, <>, >, <, and BETWEEN. Combine multiple conditions with AND (all must be true) and OR (at least one must be true), and use parentheses to make the evaluation order explicit when mixing them. Because NULL represents an unknown value, standard equality doesn't work for it — WHERE Column = NULL never matches; you must use IS NULL or IS NOT NULL instead to correctly filter for missing values.
Cricket analogy: A WHERE clause filtering WHERE Runs > 50 is like a selector only including innings where a batsman passed fifty: just as WHERE Country = 'India' AND Format = 'ODI' narrows results the way a highlights reel focuses only on India's ODI matches.
Sorting and Limiting Results
ORDER BY sorts the result set by one or more columns, ascending by default (ASC) or descending with DESC. TOP (n) restricts how many rows come back, and is almost always paired with ORDER BY so 'top' means something meaningful rather than an arbitrary subset. For paging through larger result sets, OFFSET ... FETCH NEXT lets you skip a number of rows before returning the next batch, which TOP alone cannot do.
Cricket analogy: ORDER BY Runs DESC is like ranking a tournament's batting leaderboard from the highest run-scorer down, and TOP 5 is like listing only the top five names on that leaderboard rather than every player who batted.
SELECT
p.ProductName,
p.UnitPrice,
p.UnitPrice * 0.9 AS DiscountedPrice
FROM dbo.Products AS p
WHERE p.UnitPrice > 20
AND p.IsDiscontinued = 0
ORDER BY p.UnitPrice DESC;
-- Return only the top 5 most expensive active products
SELECT TOP (5) ProductName, UnitPrice
FROM dbo.Products
WHERE IsDiscontinued = 0
ORDER BY UnitPrice DESC;Aliasing and Basic Expressions
Use AS to give a column or table a more readable alias, such as SELECT UnitPrice AS Price, or to name a computed expression that isn't a raw column at all, such as UnitPrice * 0.9 AS DiscountedPrice. Expressions can combine arithmetic, string concatenation (with + or the CONCAT function), and built-in functions directly in the SELECT list, letting you derive new values without altering the underlying stored data. Table aliases (FROM dbo.Products AS p) become especially useful once queries involve multiple tables via JOIN.
Cricket analogy: Aliasing SELECT Runs AS 'Score' is like a commentator renaming a stat for the broadcast audience: the underlying column is still Runs, just displayed under a friendlier label, and a computed column like Runs divided by BallsFaced times 100 AS StrikeRate calculates a new value on the fly.
Avoid SELECT * in production code and views: it retrieves every column (even ones you don't need), can break when the table schema changes, and often prevents SQL Server from using a covering index efficiently.
- A SELECT statement's basic anatomy is SELECT columns FROM table [WHERE condition] [ORDER BY column].
- WHERE filters rows before they're returned; combine conditions with AND/OR and use IS NULL/IS NOT NULL for nulls.
- ORDER BY sorts results; ASC (default) or DESC controls direction.
- TOP (n) limits the number of rows returned, often paired with ORDER BY for meaningful results.
- AS creates a column alias or table alias to improve readability.
- You can compute new values directly in the SELECT list, like price * quantity AS Total.
- Avoid SELECT * in production code; explicitly list the columns you need.
Practice what you learned
1. Which clause filters rows before they appear in the result set?
2. What does TOP (5) do in a SELECT statement?
3. How do you check for NULL values in a WHERE clause?
4. What keyword creates a column alias?
5. Why is SELECT * generally discouraged in production code?
Was this page helpful?
You May Also Like
T-SQL Syntax Basics
Core T-SQL syntax rules — statements, batches, comments, identifiers, and control-of-flow — you need before writing real queries.
Data Types in SQL Server
A tour of SQL Server's core data types — numeric, character, date/time, and special types — and how to pick the right one.
Installing SQL Server and SSMS
A practical walkthrough of installing SQL Server and SQL Server Management Studio, and connecting to your first instance.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics