Sub topic 1
Anatomy of a SELECT Statement
A SELECT statement retrieves data from an Oracle database. It can return columns from a table, calculate expressions, or do both. A query usually contains these parts:
SELECT column_list
FROM table_name
WHERE condition
ORDER BY sort_expression;The clauses have different jobs:
SELECTchooses the values to return.FROMidentifies the row source, such as a table or view.WHEREkeeps only rows that satisfy a condition.ORDER BYcontrols the order of the returned rows.
The SELECT and FROM clauses form the core of the statement. The other clauses are optional.
Prerequisites
You should understand that a relational table contains rows and columns. You should also have access to an Oracle Database session through SQL*Plus, SQL Developer, or another SQL worksheet. This chapter assumes familiarity with numbers, strings, and dates.
Choosing columns with SELECT
You can select specific columns by listing their names, separated by commas:
SELECT employee_id, first_name, last_name, salary
FROM employees;Oracle returns one result column for each selected expression. In this example, each returned row contains four values from the employees table.
To return every column, use an asterisk:
SELECT *
FROM employees;Although * is convenient for exploration, explicit column names are usually better in application code. They document which values the query needs and avoid unexpected results when the table changes.
A query can also select a calculated expression:
SELECT employee_id,
salary,
salary * 12 AS annual_salary
FROM employees;The expression salary * 12 calculates a value for each row. It does not create or change a column in the table.
Naming result columns with aliases
An alias is a temporary name for a result column. Use AS for clarity:
SELECT first_name AS given_name,
last_name AS family_name,
salary * 12 AS annual_salary
FROM employees;In Oracle SQL, AS is supported for column aliases. The alias changes the heading shown in the result, not the underlying table definition.
Unquoted aliases are normally stored and displayed in uppercase. If you need spaces or exact letter casing, enclose the alias in double quotation marks:
SELECT first_name AS "Given Name"
FROM employees;Quoted identifiers require exact spelling and can make later SQL harder to read. Prefer simple aliases such as given_name unless a presentation tool requires a different heading.
Supplying a row source with FROM
FROM tells Oracle where the rows come from. A table can contain zero, one, or many rows, so the query can return a corresponding number of rows.
SELECT department_id, department_name
FROM departments;Oracle evaluates the selected expressions for each row produced by the FROM clause. A query against one table is the simplest case. Later queries can use joins to combine rows from multiple tables.
When a query needs to evaluate an expression but does not need application data, Oracle traditionally uses DUAL:
SELECT 'Hello, Oracle' AS greeting,
2 + 3 AS total
FROM dual;DUAL is an Oracle-provided table with one column named DUMMY and one row containing X. Its single row makes the query return exactly one result row.
A complete query
This query combines all four common clauses:
SELECT employee_id,
first_name,
last_name,
salary
FROM employees
WHERE department_id = 50
ORDER BY salary DESC;The query selects four values from employees, keeps employees in department 50, and sorts the remaining rows by salary from highest to lowest. DESC means descending order. The default is ascending order, written as ASC when you want to state it explicitly.
A semicolon marks the end of the statement in tools such as SQL*Plus and many SQL worksheets. It is a client command delimiter, not part of the SQL meaning sent to the database.
Exercise
Which clause identifies the table that supplies rows, and which clause chooses the values returned for each row?
Show answer
FROM identifies the row source, and SELECT chooses the returned columns or expressions.