The SQL Select Statement
The SQL Select Statement
Projection: A project operation selects only certain columns fields from a table. The result
table has a subset of the available columns and can include anything from a single column to
all available columns.
Selection: A select operation selects a subset of rows records in a table relation that satisfy a
selection condition. The ability to select rows from out of complete result set is called
Selection. It involves conditional filtering and data staging. The subset can range from no
rows, if none of the rows satisfy the selection condition, to all rows in a table.
Joining: A join operation combines data from two or more tables based on one or more
common column values. A join operation enables an information system user to process the
relationships that exist between tables. The join operation is very powerful because it allows
system users to investigate relationships among data elements that might not be anticipated
at the time that a database is designed.
Consider the above table structures. Fetching first_name name, department_id and salary for a
single employee from EMPLOYEES table is Projection. Fetching employee details whose salary is
less than 5000, from EMPLOYEES table is Selection. Fetching employee's first name, department
name by joining EMPLOYEES and DEPARTMENTS is Joining.
Basic SELECT statement
The basic syntax for a SELECT statement is presented below.
The SELECT clause is mandatory and carries out the relational project operation.
The FROM clause is also mandatory. It identifies one or more tables and/or views from which to
retrieve the column data displayed in a result table.
The WHERE clause is optional and carries out the relational select operation. It specifies which
rows are to be selected.
The GROUP BY clause is optional. It organizes data into groups by one or more column names
listed in the SELECT clause.
The optional HAVING clause sets conditions regarding which groups to include in a result table.
The groups are specified by the GROUP BY clause.
The ORDER BY clause is optional. It sorts query results by one or more columns in ascending or
descending order.
The above query contains the arithmetic expression sal ∗ 12 to calculate annual salary of each
employee.
Arithmetic operators
Operators act upon the columns knownasoperands to result into a different result. In case of multiple
operators in an expression, the order of evaulation is decided by the operator precedence. Here
are the elementary rules of precedence -
Below table shows the precedence of the operators, in such cases. Precedence Level Operator
Symbol Operation
Query a multiplies two numbers, while b shows addition of $1500 to salaries of all employees.
Query (c) shows the addition of commission component to employee's salary. As per the
precedence, first commission would be calculated on the salary, and then added to the salary.
Column Alias
An alias is used to rename a column or an expression during display. The alias to a column or an
expression appears as the heading in the output of a query. It is useful in providing a meaningful
heading to long expressions in the SELECT query. By default, the alias appears in uppercase in the
query output without spaces. To override this behavior, alias must be enclosed within double
quotes to preserve the case and spaces in the alias name.
Concatenation operators
Concatenation operator can be used to join two string values or expressions in a SELECT query.
The double vertical bar symbol is used as string concatenation operator. It is applicable only for
character and string column values resulting into a new character expression. Example
Literals
Any hard coded value, which is not stored in database, in the SELECT clause, is known s Literal. It
can be number, character, or date value. Character and date values must be enclosed within
quotes. Consider the below SQL queries.examples of using literals of different data types in SQL
queries.
The query below uses two character literals to join them together.
The query below uses character literals to pretty print the employee's salary.
Quote Operator
The quote operator is used to specify the quotation mark delimiter of your own. You can chose a
convenient delimiter, depedning on the data.
Columns with NULL value can be selected in a SELECT query and can be the part of an arithmetic
expression. Any arithmetic expression using NULL values results into NULL. For this reason,
columns with NULL value must be handled differently by specifying their alternate values using
Oracle supplied functions like NVL or NULLIF.
NUM
--------
DISTINCT Keyword
If the data is expected to have duplicate results, use DISTINCT keyword to eliminate duplicates and
diplay only the unique results in the query output. Only the selected columns are validated for
duplication and the rows will be logically eliminated from the query output. To be noted, the
DISTINCT keyword must appear just after the SELECT clause.
The simple query below demonstrates the use of DISTINCT to display unique department ids from
EMPLOYEES table.
DEPARTMENT_ID
---------------
10
20
30
40
DESCRIBE command
The structural metadata of a table may be obtained by querying the database for the list of
columns that comprise it using the DESCRIBE command. It will list the used column names, their
null property and data type.
Syntax:
For example,
DESC EMPLOYEE
will display the EMPLOYEE table structure i.e. columns, their data types, precision and nullable
property.
Loading [MathJax]/jax/output/HTML-CSS/jax.js