Keep these patterns handy. The cheat sheet covers every SELECT pattern taught in this chapter.
All columns
SELECT *
FROM students;
* means every column. Use for exploration; avoid in production code.
Specific columns
SELECT name, city
FROM students;
List only the columns you need. Faster, clearer intent.
Alias with AS
SELECT name AS student_name,
score AS total
FROM students;
AS renames the output column. Does not change the table.
Alias with spaces (SQL Server)
SELECT name AS [Full Name],
score AS [Total Score]
FROM students;
Square brackets allow spaces in aliases — SQL Server syntax.
DISTINCT — remove duplicates
SELECT DISTINCT city
FROM students;
Returns each unique value once. On two columns, removes rows where BOTH match.
Arithmetic expression
SELECT name,
salary * 12 AS annual
FROM employees;
Any arithmetic is valid per row. Use 3.0 not 3 for decimal division.
String functions (SQL Server)
SELECT UPPER(name),
LOWER(city),
LEN(name)
FROM students;
LEN() counts characters. UPPER/LOWER convert case. Original data unchanged.
Concatenation (SQL Server)
SELECT name + ' — ' + city
AS info
FROM students;
SQL Server uses + for string concat. SQLite/PostgreSQL use ||.
CASE expression
SELECT name,
CASE WHEN score >= 80
THEN 'Pass'
ELSE 'Fail'
END AS result
FROM students;
CASE WHEN is like IF/ELSE inside a column. Multiple WHEN branches allowed.
NULL handling (SQL Server)
SELECT name,
ISNULL(score, 0) AS score,
COALESCE(city, 'Unknown') AS city
FROM students;
ISNULL(col, default) — SQL Server. COALESCE works on multiple values.
TOP n (SQL Server)
SELECT TOP 5 *
FROM students
ORDER BY score DESC;
TOP goes right after SELECT. Combine with ORDER BY for meaningful top-N.
Literal value column
SELECT name,
'India' AS country
FROM students;
A string/number literal repeats the same value for every row.
TOP n PERCENT
SELECT TOP 20 PERCENT *
FROM students
ORDER BY score DESC;
Returns the top 20% of rows by count. If the table has 100 rows, returns 20. Always combine with ORDER BY.
TOP n WITH TIES
SELECT TOP 3 WITH TIES *
FROM students
ORDER BY score DESC;
If the 3rd and 4th rows share the same ORDER BY value, both are included. Prevents arbitrary tie-breaking.
IIF — two-branch shorthand
SELECT name,
IIF(score >= 75, 'Pass', 'Fail')
AS result
FROM students;
IIF(condition, true_value, false_value) is SQL Server shorthand for a two-branch CASE. More concise but less portable.
SELECT without FROM
SELECT 1 + 1; -- 2
SELECT GETDATE(); -- now
SELECT UPPER('hello'); -- HELLO
SELECT LEN('SQL Server'); -- 10
SQL Server allows SELECT without FROM for scalar expressions. Useful for quick calculations or testing a function without a table.
CAST — type conversion
-- INT to FLOAT for real division
SELECT CAST(score AS FLOAT) / 3
FROM students;
-- Date to text
SELECT CAST(GETDATE() AS VARCHAR(20));
CAST(expr AS type) is ANSI standard — works across all databases. Use when you need to change a value's data type.
CONVERT — SQL Server with style
-- Style 103 = DD/MM/YYYY
SELECT CONVERT(VARCHAR, GETDATE(), 103);
-- Style 112 = YYYYMMDD
SELECT CONVERT(VARCHAR, GETDATE(), 112);
CONVERT is SQL Server-specific and adds a style parameter for date formatting. Use CAST for portability; CONVERT when you need a date format code.
LEFT / RIGHT / SUBSTRING
SELECT LEFT(name, 3) AS first3,
RIGHT(name, 2) AS last2,
SUBSTRING(name, 2, 3) AS mid
FROM students;
-- Arjun → 'Arj', 'un', 'rju'
SUBSTRING(col, start, length) — start is 1-based. LEFT/RIGHT are shortcuts for the start/end.
REPLACE / LTRIM / RTRIM
SELECT REPLACE(city, 'Delhi', 'NCR'),
LTRIM(RTRIM(name)) AS clean
FROM students;
REPLACE swaps substrings. LTRIM/RTRIM strip leading/trailing spaces. SQL Server 2017+ adds TRIM() for both at once.
SELECT *
Avoid in prod
Fetches every column including ones you don't need. Increases network transfer, breaks views when schema changes.
Use during exploration only.
Specific columns
Best practice
Fetching only needed columns reduces I/O. With a covering index, SQL Server can satisfy the query entirely from the index.
Always prefer in production queries, views, and stored procedures.
SELECT TOP n
Fast
SQL Server stops scanning as soon as it has the required rows — with a sorted index, TOP 1 is near-instant.
Always combine TOP with ORDER BY so you get the intended rows.
Functions on columns
Can block indexes
Functions in SELECT list are fine. Functions wrapped around columns in WHERE prevent index use on large tables.
Functions in SELECT: free. Functions in WHERE: expensive on large tables.
DISTINCT
Sort cost
DISTINCT requires sorting or hashing all rows. Costly on millions of rows.
Use when duplicates genuinely exist. If they shouldn't, fix the query upstream.
Computed columns
Free in SELECT
Arithmetic in SELECT (salary * 12, ROUND(score, 2)) runs per row with no extra overhead.
Move expensive functions to persisted computed columns if used in WHERE repeatedly.
Same concept, different syntax across databases.