Your first SELECT: columns, aliases and expressions
Choose columns, rename them, calculate new ones and join text together. Plus the order SQL Server really processes a query in, and why an alias cannot be used in WHERE.
SELECT. Its simplest form names the columns you want and the table they come from. The result is always a new table: rows and columns that exist only until SSMS shows them.
SELECT CategoryId, NameFROM dbo.Categories;CategoryId Name
---------- --------
1 Tools
2 Building
3 Timber
4 Fixings
5 Paint
6 Safety
7 PlumbingSELECT * and why to avoid it in real code
SELECT * means “every column”. It is perfect for a quick look at a table in SSMS. In application code, list the columns: the query then returns the same shape even after someone adds a column, it reads less data, and anyone reading it can see what is used.
Aliases: renaming columns
AS gives a column a new name in the result. The table is not changed. An alias with a space or a reserved word needs square brackets.
SELECT Name AS Product, Price AS [Unit price], UnitFROM dbo.ProductsWHERE CategoryId = 3;Product Unit price Unit ------------------- ---------- ----- Timber 2x4 2.4 m 6.80 each Plywood sheet 18 mm 42.00 sheet
Calculated columns
SELECT Name, Price, Stock, Price * Stock AS StockValueFROM dbo.ProductsWHERE CategoryId = 5;Name Price Stock StockValue ------------------ ----- ----- ---------- Masonry paint 10 L 54.00 25 1350.00 Paint roller set 12.50 30 375.00
+ - * / and % (remainder). The usual precedence applies: *, / and % before + and -, with parentheses to change it.
Joining text together
+ or CONCAT to join strings. CONCAT also converts numbers for you and treats NULL as empty text, which + does not (more on NULL in two lessons). You can also put fixed values, called literals, in the SELECT list.
SELECT FirstName + N' ' + LastName AS FullName, CONCAT(FirstName, N' joined on ', JoinedOn) AS Note, N'BuildMart' AS ShopFROM dbo.CustomersWHERE CustomerId <= 3;FullName Note Shop ----------- -------------------------- --------- Asha Patel Asha joined on 2023-03-02 BuildMart Ben Carter Ben joined on 2023-05-19 BuildMart Chloe Evans Chloe joined on 2023-06-07 BuildMart
CONCAT turned the JoinedOn date into text by itself. With +, SQL Server would try to turn the text into a date instead, and fail.
SELECT 2 + 3 * 4 AS Answer, (2 + 3) * 4 AS WithBrackets, 17 % 5 AS Remainder;Answer WithBrackets Remainder
------ ------------ ---------
14 20 2The order SQL Server reads a query in
SELECT first, but it is not processed first. The logical processing order is:
FROM: which table(s) the rows come from.WHERE: keep only the rows that pass the test.SELECT: work out the columns and expressions, and apply aliases.ORDER BY: sort the result (lesson 6).
SELECT Name, Price * Stock AS StockValueFROM dbo.ProductsWHERE StockValue > 1000;Msg 207, Level 16, State 1, Line 1 Invalid column name 'StockValue'.
WHERE Price * Stock > 1000). ORDER BY runs after SELECT, so it is the one clause where the alias does work.
Style that pays off
Write keywords in capitals ( SELECT,FROM,WHERE) and names as they are defined. SQL Server does not care; readers do.Put each clause on its own line. End statements with a semicolon. It is optional for most statements today, but required before some (such as a WITHcommon table expression), and Microsoft recommends it.Comment with --to the end of the line, or/* … */across lines.
Name, the Price, and the price including VAT as PriceIncVat.
Show a hint
Show the solution
SELECT Name, Price, Price * 1.2 AS PriceIncVatFROM dbo.ProductsWHERE CategoryId = 1;Name Price PriceIncVat ---------------- ----- ----------- Claw hammer 18.50 22.200 Cordless drill 89.00 106.800 Tape measure 8 m 9.75 11.700
Remember
The SELECT list decides the shape of the result: which columns, in what order, with what names. SELECT *is for quick looks; list columns in real code.ASrenames a column in the result only; use[brackets]for names with spaces or reserved words.Expressions such as Price * Stockare calculated per row and stored nowhere.Logical order: FROM → WHERE → SELECT → ORDER BY, so aliases cannot be used in WHERE.