Data retrieval
The most frequently used operation in transactional databases is the data retrieval operation. When restricted to data retrieval commands, SQL acts as a declarative language:
SELECT
is used to retrieve zero or more rows from one or more tables in a database. In most applications,SELECT
is the most commonly used Data Manipulation Language command. In specifying aSELECT
query, the user specifies a description of the desired result set, but they do not specify what physical operations must be executed to produce that result set. Translating the query into an efficient query plan is left to the database system, more specifically to the query optimizer.- Commonly available keywords related to
SELECT
include: FROM
is used to indicate from which tables the data is to be taken, as well as how the tablesJOIN
to each other.WHERE
is used to identify which rows to be retrieved, or applied toGROUP
BY
.WHERE
is evaluated before theGROUP
BY
.GROUP BY
is used to combine rows with related values into elements of a smaller set of rows.HAVING
is used to identify which of the "combined rows" (combined rows are produced when the query has aGROUP
BY
keyword or when theSELECT
part contains aggregates), are to be retrieved.HAVING
acts much like aWHERE
, but it operates on the results of theGROUP
BY
and hence can use aggregate functions.ORDER BY
is used to identify which columns are used to sort the resulting data.
Data retrieval is very often combined with data projection; usually it isn't the verbatim data stored in primitive data types that a user is looking for or a query is written to serve. Often the data needs to be expressed differently from how it's stored. SQL allows a wide variety of formulas included in the select list to project data.
Data manipulation
First, there are the standard Data Manipulation Language (DML) elements. DML is the subset of the language used to add, update and delete data:
INSERT
is used to add zero or more rows (formally tuples) to an existing table.UPDATE
is used to modify the values of a set of existing table rows.MERGE
is used to combine the data of multiple tables. It is something of a combination of theINSERT
andUPDATE
elements. It is defined in the SQL:2003 standard; prior to that, some databases provided similar functionality via different syntax, sometimes called an "upsert".DELETE
removes zero or more existing rows from a table.
Transaction controls
Transactions, if available, can be used to wrap around the DML operations:
BEGIN WORK
(orSTART TRANSACTION
, depending on SQL dialect) can be used to mark the start of a database transaction, which either completes completely or not at all.COMMIT
causes all data changes in a transaction to be made permanent.ROLLBACK
causes all data changes since the lastCOMMIT
orROLLBACK
to be discarded, so that the state of the data is "rolled back" to the way it was prior to those changes being requested.
COMMIT
and ROLLBACK
interact with areas such as transaction control and locking. Strictly, both terminate any open transaction and release any locks held on data. In the absence of a BEGIN WORK
or similar statement, the semantics of SQL are implementation-dependent.
Data definition
The second group of keywords is the Data Definition Language (DDL). DDL allows the user to define new tables and associated elements. Most commercial SQL databases have proprietary extensions in their DDL, which allow control over nonstandard features of the database system. The most basic items of DDL are the CREATE
,ALTER
,RENAME
,TRUNCATE
and DROP
commands:
CREATE
causes an object (a table, for example) to be created within the database.DROP
causes an existing object within the database to be deleted, usually irretrievably.TRUNCATE
deletes all data from a table (non-standard, but common SQL command).ALTER
command permits the user to modify an existing object in various ways -- for example, adding a column to an existing table.