Oracle SQL SELECT Statement Fundamentals: Complete Guide
The SELECT statement is the foundation of every SQL query in Oracle. Whether you are retrieving data, performing calculations, or transforming results, understanding SELECT mechanics is essential for writing clear, efficient, and maintainable queries.
By the end of this guide you will master SELECT syntax, understand column aliases, DISTINCT behavior, arithmetic expressions, string concatenation, operator precedence, and best practices that professionals use every day.
1. SELECT Statement Structure and Mandatory Clauses
Only two clauses are mandatory. The remaining clauses are optional and must appear in a fixed order.
| Clause | Required? | Purpose |
|---|---|---|
SELECT | Yes | Columns or expressions to retrieve |
FROM | Yes | Table(s) to retrieve from |
WHERE | No | Filter rows before grouping |
GROUP BY | No | Collapse rows into groups |
HAVING | No | Filter groups after grouping |
ORDER BY | No | Sort the final result |
-- Minimum valid SELECT statement
SELECT last_name, salary
FROM employees;
-- Full clause order (must appear exactly in this sequence)
SELECT department_id, AVG(salary)
FROM employees
WHERE salary > 5000
GROUP BY department_id
HAVING AVG(salary) > 8000
ORDER BY department_id;
2. Selecting All Columns vs Specific Columns
-- All columns
SELECT * FROM employees;
-- Specific columns in any order you prefer
SELECT last_name, employee_id, salary
FROM employees;
-- Same column can appear multiple times
SELECT last_name, salary, salary * 12 AS annual_salary
FROM employees;
3. Column Aliases — AS keyword is optional
-- With AS (recommended for clarity)
SELECT last_name AS name,
salary * 12 AS annual_salary
FROM employees;
-- Without AS — space is enough
SELECT last_name name,
salary * 12 annual_salary
FROM employees;
4. Aliases with Spaces or Special Characters
-- Alias with space or special characters — use double quotes
SELECT salary * 12 AS "Annual Salary" FROM employees;
-- Preserving exact case or using special symbols
SELECT salary AS "Salary ($)" FROM employees;
• Double quotes (
" ") → for column aliases and identifiers• Single quotes (
' ') → for string literals only
5. Arithmetic Expressions and NULL Behavior
SELECT last_name,
salary,
salary * 12 AS annual_sal,
salary * 12 + NVL(commission_pct, 0) * salary * 12 AS total_annual
FROM employees;
NVL or COALESCE to provide default values when needed.
6. String Concatenation with ||
SELECT first_name || ' ' || last_name AS full_name
FROM employees;
-- Oracle treats NULL as an empty string in concatenation
SELECT 'Hello' || NULL || ' World' FROM dual; -- Returns: Hello World
7. DISTINCT — Removing Duplicate Rows
SELECT DISTINCT department_id FROM employees;
-- UNIQUE is a synonym for DISTINCT in Oracle
SELECT UNIQUE department_id FROM employees;
8. DISTINCT with Multiple Columns
SELECT DISTINCT department_id, job_id
FROM employees;
9. Operator Precedence
SELECT 2 + 3 * 4 FROM dual; -- Returns 14 (multiplication first)
SELECT (2 + 3) * 4 FROM dual; -- Returns 20 (parentheses override)
10. General SQL Statement Rules
- SQL keywords and identifiers are case-insensitive
- String literals inside single quotes are case-sensitive
- Extra whitespace, tabs, and newlines are ignored
- Use a semicolon to terminate statements in tools like SQL*Plus
11. Interactive Practice Quiz
Conclusion
You now have a solid understanding of Oracle SQL SELECT statement fundamentals. These concepts form the foundation for writing powerful, readable, and efficient queries in any Oracle environment.
Practice these patterns regularly, pay attention to clause order, alias usage, and DISTINCT behavior, and you will write cleaner SQL with confidence.
No comments:
Post a Comment