Structured Query Language/Data Query Language
Data Query Language is used to extract data from the database. It doesn't modify any data in the database. It describes only one query: SELECT.
SQL data types
[edit | edit source]Each column has a type. Here are the standard SQL data types:
Data type | Explanation | Allowed values | Example |
VARCHAR(n) | A string with a maximum length of n | [0-9a-zA-Z]+{n} |
"foo" |
CHAR(n) | A string with a fixed length of n | [0-9a-zA-Z]{n} |
"foo" |
SMALLINT | A 16 bits signed integer | \-?[0-9]+ |
584 |
INTEGER | A 32 bits signed integer | \-?[0-9]+ |
-8748 |
FLOAT | A decimal floating point | \-?[0-9]+[\.[0-9]+]? |
48.96 |
NUMBER(n,[d]) | A number with n digits (and d decimal digits if mentioned) | \-?[0-9]+[\.[0-9]+]? |
484.65 |
DATE | A date (YYYY-MM-DD) | [0-9][0-9][0-9][0-9]\-[0-1][0-9]\-[0-3][0-9] |
2009-03-24 |
TIME | A time period of sixty minutes; one twenty-fourth of a day | [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9] |
11:24:56 |
TIMESTAMP | A date and hour | [0-9]+ |
18648689595962 |
BLOB | Any binary data | Any |
There is no boolean type. Integers are used instead.
SELECT query
[edit | edit source]The exhaustive syntax of the SELECT query is as follows:
SELECT[ ALL| DISTINCT] <column name>[[ AS] <alias>][,[ ALL| DISTINCT] <column name>[[ AS] <alias>]]*
FROM <table>[[ AS] <alias>|[[ FULL| LEFT| RIGHT] OUTER| INNER] JOIN <table> ON <expression>]
[, <table>[[ AS] <alias>|[[ FULL| LEFT| RIGHT] OUTER| INNER] JOIN <table> ON <expression>]]*
[WHERE <predicate>[{ AND| OR} <predicate>]*]
[GROUP BY <column name>[, <column name>]*
[HAVING <predicate>[{ AND| OR} <predicate>]]*]
]
[ORDER BY <column name>[ ASC| DESC][, <column name>[ ASC| DESC]]*]
[FETCH FIRST <count> ROWS ONLY];
First query
[edit | edit source]Let's create the table reunion
with many columns:
id_reunion | INTEGER |
name | VARCHAR(20) |
description | VARCHAR(255) |
priority | CHAR(1) |
planned | SMALLINT |
date | DATE |
hour | TIME |
duration | INTEGER |
# id_office | INTEGER |
pdf_report | BLOB |
...and let's fill it:
id_reunion | name | description | priority | planned | date | hour | duration | # id_office | pdf_report |
---|---|---|---|---|---|---|---|---|---|
1 | Planning | We need to plan the project. | A | 1 | 2008-03-24 | 10:30:00 | 60 | 35 | 48644...846348 |
2 | Progress | What we have done. | C | 1 | 2008-05-12 | 14:00:00 | 30 | 13 | 9862...15676 |
3 | Change | What we need to change in the project. | B | 1 | 2008-06-03 | 9:30:00 | 90 | 41 | 34876...4846548 |
4 | Presentation | Presentation of the project. | D | 0 | 2008-09-11 | 15:30:00 | 120 | 27 | |
5 | Reporting | Explanation to the new beginner. | B | 1 | 2009-03-15 | 14:00:00 | 60 | 7 | 19739...37718 |
6 | Learning | A new software version has been installed. | B | 1 | 2009-09-21 | 16:00:00 | 120 | 11 | 785278...37528 |
Let's do a simple query. The following query just returns the content of the reunion table:
- Query:
SELECT *
FROM reunion;
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |1 |Planning |We need to plan the project. |A |1 |2008-03-24 |10:30:00 |60 |35 |48644...846348 | |2 |Progress |What we have done. |C |1 |2008-05-12 |14:00:00 |30 |13 |9862...15676 | |3 |Change |What we need to change in the project. |B |1 |2008-06-03 |9:30:00 |90 |41 |34876...4846548 | |4 |Presentation |Presentation of the project. |D |0 |2008-09-11 |15:30:00 |120 |27 |NULL | |5 |Reporting |Explanation to the new beginner. |B |1 |2009-03-15 |14:00:00 |60 |7 |19739...37718 | |6 |Learning |A new software version has been installed. |B |1 |2009-09-21 |16:00:00 |120 |11 |785278...37528 | |----------------------------------------------------------------------------------------------------------------------------------------------------|
The form of the result depends on the client application. It can be returned as a text output (backend), a HTML page (thin client), a program object (middleware) etc... The statements, queries, clauses (SELECT, FROM...), instructions and operators are not case sensitive but they are commonly written in uppercase for readability.
The SELECT and FROM clauses are the two required clauses of a SELECT query:
- FROM : list the tables the query uses to return the data,
- SELECT : list the data to return.
WHERE clause
[edit | edit source]The WHERE clause doesn't influence the columns the query returns but the rows. It filters the rows applying predicates on it. A predicate specifies conditions that can be true or false. SQL can handle conditions whose result is unknown. For example, the following query returns the reunions which have a B priority level:
- Query:
SELECT *
FROM reunion
WHERE reunion.priority = 'B';
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |3 |Change |What we need to change in the project. |B |1 |2008-06-03 |9:30:00 |90 |41 |34876...4846548 | |5 |Reporting |Explanation to the new beginner. |B |1 |2009-03-15 |14:00:00 |60 |7 |19739...37718 | |6 |Learning |A new software version has been installed. |B |1 |2009-09-21 |16:00:00 |120 |11 |785278...37528 | |----------------------------------------------------------------------------------------------------------------------------------------------------|
The table name can be omitted if it is not ambiguous.
Predicate
[edit | edit source]Compared to the second operand, the first operand can be :
- equal :
=
- different :
<>
- lesser :
<
- lesser or equal :
<=
- greater :
>
- greater or equal :
>=
The following query returns the reunions which have another priority level than B:
- Query:
SELECT *
FROM reunion
WHERE priority <> 'B';
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |1 |Planning |We need to plan the project. |A |1 |2008-03-24 |10:30:00 |60 |35 |48644...846348 | |2 |Progress |What we have done. |C |1 |2008-05-12 |14:00:00 |30 |13 |9862...15676 | |4 |Presentation |Presentation of the project. |D |0 |2008-09-11 |15:30:00 |120 |27 |NULL | |----------------------------------------------------------------------------------------------------------------------------------------------------|
Operators
[edit | edit source]The WHERE clause can have several conditions using the operators AND (all the conditions must be true) and OR (only one condition needs to be true). The operator OR is inclusive (several conditions can be true). The order of evaluation can be indicated with brackets. NOT inverts a condition. The following query returns the reunions which have a B priority level and last more than an hour or which take place on 2008/05/12:
- Query:
SELECT *
FROM reunion
WHERE (priority = 'B' AND NOT duration <= 60) OR date = '2008-05-12';
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |2 |Progress |What we have done. |C |1 |2008-05-12 |14:00:00 |30 |13 |9862...15676 | |3 |Change |What we need to change in the project. |B |1 |2008-06-03 |9:30:00 |90 |41 |34876...4846548 | |6 |Learning |A new software version has been installed. |B |1 |2009-09-21 |16:00:00 |120 |11 |785278...37528 | |----------------------------------------------------------------------------------------------------------------------------------------------------|
LIKE
[edit | edit source]LIKE allows simplified regular expression matching. It can be applied on the text columns (CHAR, VARCHAR,...).
- Alphanumerical characters only match identical text,
%
is a wildcard that matches any text,_
is a wildcard that matches any single character,
The following query returns the reunions which end with "ing" and which contain " the " in its description:
- Query:
SELECT *
FROM reunion
WHERE name LIKE '%ing' AND description LIKE '% the %';
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |1 |Planning |We need to plan the project. |A |1 |2008-03-24 |10:30:00 |60 |35 |48644...846348 | |5 |Reporting |Explanation to the new beginner. |B |1 |2009-03-15 |14:00:00 |60 |7 |19739...37718 | |----------------------------------------------------------------------------------------------------------------------------------------------------|
BETWEEN and IN
[edit | edit source]BETWEEN matches a range of values that can be numbers, dates or times. IN matches a list of allowed values. The following query returns the reunions which take place between 2008-04-01 and 2009-04-01 and have an A, B or D priority level:
- Query:
SELECT *
FROM reunion
WHERE date BETWEEN '2008-04-01' AND '2009-04-01' AND priority IN ('A', 'B', 'D');
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |3 |Change |What we need to change in the project. |B |1 |2008-06-03 |9:30:00 |90 |41 |34876...4846548 | |4 |Presentation |Presentation of the project. |D |0 |2008-09-11 |15:30:00 |120 |27 |NULL | |5 |Reporting |Explanation to the new beginner. |B |1 |2009-03-15 |14:00:00 |60 |7 |19739...37718 | |----------------------------------------------------------------------------------------------------------------------------------------------------|
EXISTS
[edit | edit source]EXISTS is usually used with a subselect. This predicate is true if the list (i.e. the result set of a subselect) is not empty. This keyword allows to filter the returned rows using data that are not directly associated to the returned rows (i.e. they are not joined, not linked, not related... to the returned rows) so you can not use junction in this case. For instance, we want to retrieve all the reunions for which there is at least one reunion two times longer:
- Query:
SELECT *
FROM reunion r1
WHERE EXISTS (
SELECT r2.id_reunion
FROM reunion r2
WHERE r2.duration = r1.duration * 2
);
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |1 |Planning |We need to plan the project. |A |1 |2008-03-24 |10:30:00 |60 |35 |48644...846348 | |2 |Progress |What we have done. |C |1 |2008-05-12 |14:00:00 |30 |13 |9862...15676 | |5 |Reporting |Explanation to the new beginner. |B |1 |2009-03-15 |14:00:00 |60 |7 |19739...37718 | |----------------------------------------------------------------------------------------------------------------------------------------------------|
The duration of another reunion is used in this query whereas there is no join, no link and no relationship between the two rows. This condition can not be done without EXISTS. Note that the subselect uses the alias r1 whereas this alias is defined in the main query.
EXISTS is also used to match a lack of data. Let's remember the employee table and the members table:
|
|
The following query returns the employees who are not linked to any project (i.e. the ones there is no relationship for them in the members table):
- Query:
SELECT *
FROM employees e
WHERE NOT EXISTS (
SELECT m.id_employee
FROM members m
WHERE m.id_employee = e.id_employee
);
- Result:
|------------------------------------------------------------------| |id_employee |firstname |lastname |phone |mail | |------------|----------|---------|----------|---------------------| |1 |Big |BOSS |936854270 |big.boss@company.com | |------------------------------------------------------------------|
IS NULL
[edit | edit source]IS NULL tests if a column is filled. It is often used for foreign key columns.
FROM clause
[edit | edit source]The FROM clause defines the tables that are used for the query but it can also join tables. A JOIN builds a super table with the columns of two tables to be used for the query. To explain what a join is, we consider two archaic tables without primary keys nor foreign keys:
|
|
We want to associate values from columns of different tables matching values on a given column in each table.
FULL OUTER JOIN
[edit | edit source]A JOIN is made matching a column on a table to a column on the other table. After a FULL OUTER JOIN, for a given value (red), for a given row with this value on one table ([ red | 9999 ]), one row is created for each row that matches on the other table ([ red | OOOOOO ] and [ red | LLLLLL ]). If a value exists in only one table, then a row is created and is completed with NULL columns.
FROM table_1 FULL OUTER JOIN table_2 ON table_1.common_value = table_2.common_value
|
|
RIGHT OUTER JOIN
[edit | edit source]The RIGHT OUTER JOIN is like the FULL OUTER JOIN but it doesn't create row for values that don't exist on the left table.
FROM table_1 RIGHT OUTER JOIN table_2 ON table_1.common_value = table_2.common_value
|
|
LEFT OUTER JOIN
[edit | edit source]The LEFT OUTER JOIN is like the FULL OUTER JOIN but it doesn't create row for values that don't exist on the right table.
FROM table_1 LEFT OUTER JOIN table_2 ON table_1.common_value = table_2.common_value
|
|
INNER JOIN
[edit | edit source]The INNER JOIN is like the FULL OUTER JOIN but it creates row only for values that exist on both the left table and the right table.
FROM table_1 INNER JOIN table_2 ON table_1.common_value = table_2.common_value
|
|
Alias
[edit | edit source]The FROM clause can declare several tables, separated by ,
and aliases can be defined for table name with the keyword AS, which allows the user to make several joins with the same tables. The following query is equivalent to the INNER JOIN above:
- Query:
SELECT *
FROM table_1 AS t1, table_2 AS t2
WHERE t1.common_value = t2.common_value
The keyword AS can be omitted.
SELECT clause
[edit | edit source]The SELECT clause doesn't influence the data processed by the query but the data returned to the user. *
return all the data processed after joining and filtering. Otherwise, the SELECT clause lists expressions separated by ,
.
The expressions can be a table name, a table name and a column name separated by a dot or simply a column name if it is not ambiguous. The SELECT clause also allows evaluated expressions like addition, subtraction, concatenation, ... An expression can be followed by an alias with the keyword AS. The keyword AS can be omitted.
Here is an example:
- Query:
SELECT reunion.id_reunion, concat(name, ' : ', reunion.description) n, priority AS p, planned * 10 AS plan, duration + 10 AS reunion_length
FROM reunion;
- Result:
|-------------------------------------------------------------------------------------------| |id_reunion |n |p |plan |reunion_length | |-----------|------------------------------------------------------|--|-----|---------------| |1 |Planning : We need to plan the project. |A |10 |70 | |2 |Progress : What we have done. |C |10 |40 | |3 |Change : What we need to change in the project. |B |10 |100 | |4 |Presentation : Presentation of the project. |D |0 |130 | |5 |Reporting : Explanation to the new beginner. |B |10 |70 | |6 |Learning : A new software version has been install... |B |10 |130 | |-------------------------------------------------------------------------------------------|
The expressions can be also the following aggregation functions:
count(*)
: the count of rows returned,max(<column_name>)
: the greatest value of the column,min(<column_name>)
: the lowest value of the column.
Here is a new example:
- Query:
SELECT count(*) * 10 AS c, max(date) AS latest_date, min(reunion.date) oldest_date
FROM reunion;
- Result:
|-----------------------------| |c |latest_date |oldest_date | |---|------------|------------| |60 |2009-09-21 |2008-03-24 | |-----------------------------|
ORDER BY clause
[edit | edit source]The ORDER BY clause sorts the rows returned by the query by one or several columns. The sort is done with the first column mentioned. The second column is used to sort the rows which have the same value in the first column and so on. The keywords ASC or DESC can be added after each column. ASC indicates an ascending sort. DESC indicates a descending sort. Default is a descending sort. Let's do two simple requests, the first sorting by only one column and the second sorting by two columns:
- Query:
SELECT *
FROM reunion
ORDER BY priority ASC;
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |1 |Planning |We need to plan the project. |A |1 |2008-03-24 |10:30:00 |60 |35 |48644...846348 | |3 |Change |What we need to change in the project. |B |1 |2008-06-03 |9:30:00 |90 |41 |34876...4846548 | |5 |Reporting |Explanation to the new beginner. |B |1 |2009-03-15 |14:00:00 |60 |7 |19739...37718 | |6 |Learning |A new software version has been installed. |B |1 |2009-09-21 |16:00:00 |120 |11 |785278...37528 | |2 |Progress |What we have done. |C |1 |2008-05-12 |14:00:00 |30 |13 |9862...15676 | |4 |Presentation |Presentation of the project. |D |0 |2008-09-11 |15:30:00 |120 |27 |NULL | |----------------------------------------------------------------------------------------------------------------------------------------------------|
- Query:
SELECT *
FROM reunion
ORDER BY priority ASC, duration DESC;
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |1 |Planning |We need to plan the project. |A |1 |2008-03-24 |10:30:00 |60 |35 |48644...846348 | |6 |Learning |A new software version has been installed. |B |1 |2009-09-21 |16:00:00 |120 |11 |785278...37528 | |3 |Change |What we need to change in the project. |B |1 |2008-06-03 |9:30:00 |90 |41 |34876...4846548 | |5 |Reporting |Explanation to the new beginner. |B |1 |2009-03-15 |14:00:00 |60 |7 |19739...37718 | |2 |Progress |What we have done. |C |1 |2008-05-12 |14:00:00 |30 |13 |9862...15676 | |4 |Presentation |Presentation of the project. |D |0 |2008-09-11 |15:30:00 |120 |27 |NULL | |----------------------------------------------------------------------------------------------------------------------------------------------------|
GROUP BY clause
[edit | edit source]The GROUP BY clause is used for aggregation operations. It gathers the rows into groups, for instance, all the rows that have the same value in a given column. After gathering rows into groups, any aggregation operation is applied on each group instead of a unique big group of rows. As a consequence, an aggregation operation will return as many result as the number of groups. Groups can be formed with all the rows that have the same value for a given column or the same combination of values for several given columns. For instance, we want to know the number of reunions for each type of priority:
- Query:
SELECT count(*) as number, priority
FROM reunion
GROUP BY priority;
- Result:
|-----------------| |number |priority | |-------|---------| |1 |A | |3 |B | |1 |C | |1 |D | |-----------------|
Due to the GROUP BY clause, the aggregation function count(*) doesn't return a global count but a count for each priority level (A, B, C and D).
- Query:
SELECT count(*) as number, planned, duration
FROM reunion
GROUP BY planned, duration;
- Result:
|--------------------------| |number |planned |duration | |-------|--------|---------| |1 |0 |120 | |1 |1 |30 | |2 |1 |60 | |1 |1 |90 | |1 |1 |120 | |--------------------------|
Note that there are four groups with 1 for the column planned
and there are two groups with 120 for the column duration
. However, you can see that there is no group with the same combination of values from the two columns.
HAVING clause
[edit | edit source]The HAVING clause is used with the GROUP BY clause. The HAVING clause contains a predicate and removes from the returned rows the groups for which the predicate is false. For example, we want to retrieve only the priorities for which there are at least two reunions with the same priority level:
- Query:
SELECT priority
FROM reunion
GROUP BY priority
HAVING count(*) > 1;
- Result:
|---------| |priority | |---------| |B | |---------|
FETCH FIRST clause
[edit | edit source]The FETCH FIRST clause is used to limit the number of returned rows. Only the first rows are returned. The number of returned rows is the number indicated in the clause.
- Query:
SELECT *
FROM reunion
FETCH FIRST 4 ROWS ONLY;
- Result:
|----------------------------------------------------------------------------------------------------------------------------------------------------| |id_reunion |name |description |priority |planned |date |hour |duration |id_office |pdf_report | |-----------|-------------|-------------------------------------------|---------|--------|-----------|---------|---------|----------|----------------| |1 |Planning |We need to plan the project. |A |1 |2008-03-24 |10:30:00 |60 |35 |48644...846348 | |2 |Progress |What we have done. |C |1 |2008-05-12 |14:00:00 |30 |13 |9862...15676 | |3 |Change |What we need to change in the project. |B |1 |2008-06-03 |9:30:00 |90 |41 |34876...4846548 | |4 |Presentation |Presentation of the project. |D |0 |2008-09-11 |15:30:00 |120 |27 |NULL | |----------------------------------------------------------------------------------------------------------------------------------------------------|
This clause is often used not to return useless rows for test or to improve the performance.
Now you can explore all the data of an already existing database.
SQL Functions
[edit | edit source]- COUNT
- AVG
- MIN
- MAX
- SUM
Eg:
SELECT '''COUNT(*)''' FROM reunion
returns the number of rows in the table reunion.
---
- See also: [[1]]