Language Elements: Clauses
Language Elements: Clauses
Clauses, which are constituent components of statements and queries. (In some cases,
these are optional.)
Expressions, which can produce either scalar values, or tables consisting of columns and
rows of data
Predicates, which specify conditions that can be evaluated to SQL three-valued logic
(3VL) (true/false/unknown) or Boolean truth values and are used to limit the effects of
statements and queries, or to change program flow.
Queries, which retrieve the data based on specific criteria. This is an important element
of SQL.
Statements, which may have a persistent effect on schemata and data, or may control
transactions, program flow, connections, sessions, or diagnostics.
Queries
The most common operation in SQL, the query, makes use of the declarative SELECT statement.
The FROM clause can include optional JOIN subclauses to specify the rules for joining tables.
WHERE
The WHERE clause is used to extract only those records that fulfill a specified criterion.
The following operators can be used in the WHERE clause:
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
GROUP BY
The GROUP BY clause projects rows having common values into a smaller set of rows.
GROUP BY use aggregation functions or eliminate duplicate rows from a result set. The WHERE
clause is applied before the GROUP BY clause.
HAVING
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate
functions.
The HAVING clause includes a predicate used to filter rows resulting from the GROUP BY clause.
Because it acts on the results of the GROUP BY clause, aggregation functions can be used in the
HAVING clause predicate.
ORDER BY clause identifies which column[s] to use to sort the resulting data, and in which
direction to sort them (ascending or descending).
Without an ORDER BY clause, the order of rows returned by an SQL query is undefined
Data manipulation
The Data Manipulation Language (DML) is the subset of SQL used to add, update and delete
data:
MERGE is used to combine the data of multiple tables. It combines the INSERT and UPDATE
elements
Data definition
The Data Definition Language (DDL) manages table and index structure.
ALTER modifies the structure of an existing object in various ways, for example, adding
a column to an existing table or a constraint
TRUNCATE deletes all data from a table in a very fast way, deleting the data inside the
table and not the table itself.
DROP deletes an object in the database, usually irretrievably , it cannot be rolled back.
SQL scalar functions return a single value, based on the input value.