Lecture #5. SQL Language
Lecture #5. SQL Language
2
SQL
Structured Query Language (SQL) is a declarative programming language that
is typically used in relational database or data stream management systems.
It was developed by IBM in the early 1970s and is now an official standard
recognized by the American National Standards Institute (ANSI) and the
International Organization for Standardization (ISO).
History and versions
Year Name Comments
● Boolean
● Character types such as char, varchar, and text.
● Binary, Enumerations types
● Numeric types such as integer, monetary and floating-point
number.
● Temporal types such as date, time, timestamp, and interval
● UUID for storing Universally Unique Identifiers
● Array for storing array strings, numbers, etc.
● JSON, XML stores JSON/XML data
● hstore stores key-value pair
● Special types such as network address and geometric data.
DQL: SELECT
● The SELECT clause, which indicates projected data.
● The 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.
● The WHERE clause includes a comparison predicate, which restricts the rows returned by the query. The
WHERE clause eliminates all rows from the result set where the comparison predicate does not evaluate to
True.
● The GROUP BY clause projects rows having common values into a smaller set of rows. GROUP BY is often
used in conjunction with SQL aggregation functions or to eliminate duplicate rows from a result set. The
WHERE clause is applied before the GROUP BY clause.
● 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.
● The 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.
● The DISTINCT keyword eliminates duplicate data.
SELECT: the basic order of execution
SELECT: the simplified syntax
SELECT [DISTINCT | ALL] <expression>[[AS] <output_name>][, …]
[FROM <table>[, <table>… | <JOIN clause>…]
[WHERE <condition>]
[GROUP BY <expression>|<output_name>|<output_number> [,…]]
[HAVING <condition>]
[ORDER BY <expression>|<output_name>|<output_number> [ASC | DESC] [NULLS FIRST |
LAST] [,…]]
[OFFSET <expression>]
[LIMIT <expression>];
SELECT: The Execution Algorithm
The logical sequence of operations performed by the SELECT command is as
follows:
1) select all records from all source tables. If there are sub-queries in the
FROM clause, then they are evaluated first;
2) form all possible combinations of these records and discard those for
which the JOIN conditions are not satisfied, and in the case of external
joins, set some fields in such combinations to NULL;
3) filter out combinations that do not satisfy the predicate in the phrase
WHERE;
SELECT: The Execution Algorithm
The logical sequence of operations performed by the SELECT command is as
follows:
4) build groups according to the values of the expressions in the list GROUP
BY;
5) leave only groups that meet the HAVING conditions;
6) calculate expressions in the selection list;
7) exclude duplicate lines if the DISTINCT keyword is present;
8) apply set-theoretic operations UNION, EXCEPT and INTERSECT;
9) sort lines according to the ORDER BY clause;
10) leave only records that meet the conditions in the phrases OFFSET and
LIMIT.
SELECT: simple examples
select * from books; -- n rows
select 1;
select 2+2;
If DISTINCT is present, duplicates are removed from the output dataset. If the
word ALL is specified (this is the default mode), then all rows are returned.
SELECT: DISTINCT clause
SELECT: FROM clause
Row sources are specified after the FROM keyword. This part of the query is
called the FROM clause.
You can select data from zero, one or more sources. If there is no source of
strings, then the FROM clause should not be present. The following string
sources are possible:
● table;
● view;
● function;
● subquery;
● VALUES phrase
SELECT: FROM clause
Tables as a source: