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
Each column has a type. Here are the standard SQL data types:

There is no boolean type. Integers are used instead.

SELECT query
The exhaustive syntax of the SELECT query is as follows:

First query
Let's create the table  with many columns: ...and let's fill it:

Let's do a simple query. The following query just returns the content of the reunion table:
 * Query:


 * 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  |
 * 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
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:

The table name can be omitted if it is not ambiguous.
 * 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  |
 * 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  |

Predicate
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:


 * 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            |
 * 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
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:


 * 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  |
 * 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
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:


 * 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   |
 * 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
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:


 * 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   |
 * 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
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:


 * 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   |
 * 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:


 * Result:


 * id_employee |firstname |lastname |phone    |mail                 |
 * 1          |Big       |BOSS     |936854270 |big.boss@company.com |
 * 1          |Big       |BOSS     |936854270 |big.boss@company.com |
 * 1          |Big       |BOSS     |936854270 |big.boss@company.com |

IS NULL
IS NULL tests if a column is filled. It is often used for foreign key columns.

FROM clause
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
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.

RIGHT OUTER JOIN
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.

LEFT OUTER JOIN
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.

INNER JOIN
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.

Alias
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:

The keyword AS can be omitted.

SELECT clause
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:


 * 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            |
 * 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:
 * : the count of rows returned,
 * : the greatest value of the column,
 * : the lowest value of the column.

Here is a new example:
 * Query:


 * Result:
 * c |latest_date |oldest_date |
 * 60 |2009-09-21 |2008-03-24  |
 * 60 |2009-09-21 |2008-03-24  |
 * 60 |2009-09-21 |2008-03-24  |

ORDER BY clause
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:


 * 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            |
 * 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:


 * 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            |
 * 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
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:


 * Result:
 * number |priority |
 * 1     |A        |
 * 3     |B        |
 * 1     |C        |
 * 1     |D        |
 * 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:


 * Result:
 * number |planned |duration |
 * 1     |0       |120      |
 * 1     |1       |30       |
 * 2     |1       |60       |
 * 1     |1       |90       |
 * 1     |1       |120      |
 * 1     |1       |90       |
 * 1     |1       |120      |

Note that there are four groups with 1 for the column  and there are two groups with 120 for the column. However, you can see that there is no group with the same combination of values from the two columns.

HAVING clause
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:


 * Result:
 * priority |
 * B       |
 * B       |
 * B       |

FETCH FIRST clause
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:


 * 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            |
 * 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

 * COUNT
 * AVG
 * MIN
 * MAX
 * SUM

Eg:  returns the number of rows in the table reunion.

---


 * See also: []