Language elements
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.
SELECT retrieves data from one or more tables , or expression.
SELECT has optional keywords and clauses that include :
FROM clause, which indicates the table(s) to retrieve data from.
The FROM clause can include optional JOIN subclauses to specify the rules for joining tables.
WHERE
The WHERE clause is used to filter records.
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.
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
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:
INSERT --> add rows to an existing table
UPDATE --> modifies a set of existing table rows
DELETE removes existing rows from a table
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.
The most basic items of DDL are the :
CREATE creates an object (a table, for example) in the database
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 Aggregate Functions
SQL aggregate functions return a single value, calculated from values in a column.
Useful aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
SQL Scalar functions
SQL scalar functions return a single value, based on the input value.
Useful scalar functions:
UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed
Some of The Most Important SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index