SQL Select Statement
SQL Select Statement
A SELECT statement retrieves information from the database. With a SELECT statement, you can use the following capabilities:
Projection: Choose the columns in a table that are returned by a query. Choose as
few or as many of the columns as needed.
Selection: Choose the rows in a table that are returned by a query. Various criteria
can be used to restrict the rows that are retrieved.
Joining: Bring together data that is stored in different tables by specifying the link
between them.
A FROM clause, which identifies the table containing the columns that are listed in the SELECT clause
A keyword refers to an individual SQL element. For example, SELECT and FROM are keywords.
A clause is a part of a SQL statement. For example, SELECT employee_id, last_name, ... is a clause.
A statement is a combination of two or more clauses. For example, SELECT * FROM employees is a SQL statement.
You can also display all columns in the table by listing all the columns after the SELECT keyword.
For example, the following SQL statement displays all columns and all rows of the DEPARTMENTS table:
SELECT
In the SELECT clause, specify the columns that you want, in the order in which you want them to appear in the output. For example, to display location before department number going from left to right you use the following statement.
Character and Date column headings are left-aligned Number column headings are right-aligned Default heading display: Uppercase
Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators.
Operator
+ * / Add
Description
Note: With the DATE and TIMESTAMP data types, you can use the addition
and subtraction operators only.
The above example uses the addition operator to calculate a salary increase of $300 for all employees. The slide also displays a SALARY+300 column in the output.
Note that the resultant calculated column SALARY+300 is not a new column in the EMPLOYEES table.
Note: The Oracle server ignores blank spaces before and after the arithmetic
operator.
Operator Precedence
Multiplication and division occur before addition and subtraction. Operators of the same priority are evaluated from left to right. Parentheses are used to override the default precedence or to clarify the statement.
The first example in the slide displays the last name, salary, and annual compensation of employees. It calculates the annual compensation by multiplying the monthly salary by 12, plus a one-time bonus of $100.
The second example in the slide displays the last name, salary, and annual compensation of employees. It calculates the annual compensation as follows: adding a monthly bonus of $100 to the monthly salary, and then multiplying that subtotal by 12.
Null Values
If a row lacks a data value for a particular column, that value is said to be null or to contain a null. A null is a value that is unavailable, unassigned, unknown, or inapplicable. A null is not the same as a zero or a space. Zero is a number, and a space is a character Columns of any data type can contain nulls. However, some constraints (NOT NULL and PRIMARY KEY) prevent nulls from being used in the column.
In the COMMISSION_PCT column in the EMPLOYEES table, notice that only a sales manager or sales representative can earn a commission. Other employees are not entitled to earn commissions. A null represents that fact.
SELECT last_name, job_id, salary, commission_pct FROM employees; SELECT last_name, 12*salary*commission_pct FROM employees;
In the above example, employee King does not get any commission. Because the COMMISSION_PCT column in the arithmetic expression is null, the result is null.
Column Aliases
When displaying the result of a query, SQL*Plus normally uses the name of the selected column as the column heading. This heading may not be descriptive and, therefore, maybe difficult to understand. You can change a column heading by using a column alias. Specify the alias after the column in the SELECT list using a space as a separator. By default, alias heading appears in uppercase.
If the alias contains spaces or special characters (such as # or $), or if it is case-sensitive, enclose the alias in double quotation marks (" ").
Concatenation Operator
You can link columns to other columns, arithmetic expressions, or constant values to create a character expression by using the concatenation operator (||). Columns on either side of the operator are combined to make a single output column.
In the example, LAST_NAME and JOB_ID are concatenated, and they are given the alias Employees. Notice that the employee last name and job code are combined to make a single output column.
The above example displays last names and job codes of all employees. The column has the heading Employee Details. Notice the spaces between the single quotation marks in the SELECT statement. The spaces improve the readability of the
output.
In the following example, the last name and salary for each employee are concatenated with a literal to give the returned rows more meaning:
You can choose any convenient delimiter, single-byte or multibyte, or any of the following character
SELECT department_name || q'[, it's assigned Manager Id: ]' || manager_id AS Department and Manager FROM departments;
In the example shown, the string contains a single quotation mark, which is normally interpreted as a delimiter of a character string. By using the q operator, however, the brackets [] are used as the quotation mark delimiter. The string between
Duplicate Rows
SELECT department_id FROM employees;
Unless you indicate otherwise, SQL*Plus displays the results of a query without eliminating duplicate rows. The above example displays all the department numbers from the EMPLOYEES table.
To eliminate duplicate rows in the result, include the DISTINCT keyword in the SELECT clause immediately after the SELECT keyword. In the following example, the EMPLOYEES table actually contains 107 rows, but there are only 12 unique department numbers in the table.
SELECT FROM
You can specify multiple columns after the DISTINCT qualifier. The DISTINCT qualifier affects all the selected columns, and the result is every distinct combination of the columns.
In SQL*Plus, you can display the structure of a table by using the DESCRIBE command. The command displays the column names and the data types, and it shows you whether a column must contain data (that is, whether the column has a NOT NULL constraint).
DESCRIBE departments;