How do you select all columns from a table?
SELECT * FROM TableName;
How do you select specific columns from a table?
SELECT Column1, Column2 FROM TableName;
How do you limit the number of rows returned?
SELECT TOP 10 * FROM TableName;
How do you filter rows based on a condition?
SELECT * FROM TableName WHERE Column = ‘Value’;
How do you filter for multiple conditions (AND/OR)?
SELECT * FROM TableName
WHERE Column1 = ‘Value1’ AND Column2 = ‘Value2’;
How do you sort query results?
SELECT * FROM TableName ORDER BY COLUMN ASC|DESC;
What is the difference between = and LIKE?
= checks for exact match.
LIKE allows pattern matching using % (any characters) or _ (single character).
How do you check for NULL values?
SELECT * FROM TableName WHERE Column IS NULL;
SELECT * FROM TableName WHERE Column IS NOT NULL;
How do you check if a value is in a list?
SELECT * FROM TableName WHERE Column IN (‘Value1’, ‘Value2’);
How do you filter based on a range of values?
SELECT * FROM TableName WHERE Column BETWEEN 10 AND 100;
How do you count rows in a table?
SELECT COUNT(*) FROM TableName;
How do you get the sum of a column?
SELECT SUM(Column) FROM TableName;
How do you get the average of a column?
SELECT AVG(Column) FROM TableName;
How do you get the minimum and maximum values of a column?
SELECT MIN(Column), MAX(Column) FROM TableName;
How do you group results by a column?
SELECT Column, COUNT(*)
from TableName
GROUP BY Column;
How do you filter aggregated results?
SELECT Column, COUNT()
FROM TableName
GROUP BY Column
HAVING COUNT() > 1;
How do you get the length of a string?
LEN(Column)
How do you convert a string to uppercase or lowercase?
UPPER(Column)
LOWER(Column)
How do you get the current date and time?
GETDATE()
How do you extract just the date or year from a datetime?
CAST(GETDATE() AS DATE)
YEAR(Column)
How do you replace part of a string?
REPLACE(Column, ‘old’, ‘new’)