SQL SELECT & Filtering Essentials
SELECT Statement
- Retrieves data from table(s)
- Syntax:
SELECT column_A, column_B … FROM table_name; - Use
SELECT * to return all columns
DISTINCT
- Eliminates duplicate rows in result set
- Place DISTINCT immediately after SELECT
- Works on one or multiple columns
WHERE Clause
- Filters rows that satisfy a condition
- Syntax:
SELECT … FROM table_name WHERE condition; - Also valid with UPDATE, DELETE, etc.
ORDER BY
- Sorts results
- Append to query:
ORDER BY column_A [, column_B …] ASC | DESC; - Default order is ascending
LIKE & Wildcards
- Pattern matching inside WHERE
- Wildcards: % (zero + chars), _ (exactly one char)
- Example:
WHERE column LIKE 'A%';
Logical Operators (AND / OR / NOT)
- Combine multiple conditions in WHERE
- AND → all conditions must be true
- OR → at least one condition true
- NOT → negates a condition
LIMIT / FETCH
- Restricts number of rows returned → improves performance on large tables
- MySQL:
SELECT … LIMIT n; - Oracle:
FETCH FIRST n ROWS ONLY or use ROWNUM n
Aggregate Functions
- Operate on sets, return single value (nulls ignored except by COUNT)
• MIN() → smallest value
• MAX() → largest value
• COUNT() → number of rows
• SUM() → total of numeric column
• AVG() → average of numeric column - Commonly paired with GROUP BY
IN Operator
- Shorthand for multiple OR comparisons
- Syntax:
WHERE column IN (value_A, value_B, …);
BETWEEN Operator
- Selects values inside an inclusive range
- Works with numbers, text, dates
- Syntax:
WHERE column BETWEEN lower_val AND upper_val;
Aliases (AS)
- Temporary names for columns or tables → improves readability
- Syntax:
SELECT column AS alias_name FROM table_name; - Exist only for the duration of the query