Structured Query Language (SQL) is a language used to view or change data in databases. The statements used in this language are called SQL queries.
Basic use of SQL queries is, to:
Structured Query Language (SQL) is a language used to view or change data in databases. The statements used in this language are called SQL queries.
Basic use of SQL queries is, to:
SQL commands can be divided into five categories (assignment according to the theory of database languages in parentheses):
The term SQL refers to the English word "query". Queries are used to retrieve the data stored in a database, i.e. to make it available to the user or user software.
The result of a query looks like a table and can often be displayed, edited and used like a table.
See also: Database table
The basic commands and terms are explained using the following example:
| ER diagram: |
| ||||||||||||||||||||||||||||||||||||||||||||||||
| Relations: |
|
|
|
| |||||||||||||||||||||||||||||||||||||||||||||
lists all columns and all rows of the Student table.
Results:
| MatrNo | Name |
| 26120 | Spruce |
| 25403 | Jonas |
| 27103 | Lazy |
lists the columns VorlNr and Titel of all rows of the table Vorlesung.
Results:
| PrelNo | Title |
| 5001 | ET |
| 5022 | IT |
| 5045 | DB |
lists only different entries of the column MatrNr from the table listens. This shows the matriculation numbers of all students who listen to at least one lecture, whereby matriculation numbers that occur more than once are only output once.
Results:
| MatrNo |
| 25403 |
| 26120 |
lists the columns MatrNr and Name of all rows of the table Student. MatrNr is listed as the student number in the display result.
Results:
| Matriculation number | Name |
| 26120 | Spruce |
| 25403 | Jonas |
| 27103 | Lazy |
lists the VorlNr and title of all those lines of the table Lecture whose title is 'ET'.
The frequently used instruction structured in such a way is also called "SFW block" after the initial letters.
Results:
| PrelNo | Title |
| 5001 | ET |
lists the names of all students whose name begins with F (in the example: Fichte and Fauler).
LIKE can be used with different placeholders: _ stands for a single arbitrary character, % stands for an arbitrary string. Some database systems offer further such wildcard characters, for example for character sets.
Results:
| Name |
| Spruce |
| Lazy |
lists the first name, last name, street number, postal code and city of all students from the specified postal code area sorted by name.
The enumeration after FROM specifies the data sources: at this point, several tables can be linked together using so-called JOINs, so that data from different tables is merged and displayed.
In this example, a natural inner join is used: All records from the Professor and Lecture tables that have the same value in the PersNr field. Professors without a lecture and lectures without a professor are thus not displayed.
This is equivalent to:
Caution: Not all implementations understand both notations, the "Oracle" notation "FROM Professor, Lecture" is considered obsolete and is less common. It also does not conform to the ANSI standard and should therefore be avoided. However, it is still common for historical reasons.
Tables can be linked to each other not only via key fields, but also via any fields, as the following, technically nonsensical example shows:
The result gets the combinations of all professors and all lectures where the name of the professor differs from the title of the lecture - that's just all of them (no lecture is named like a professor):
| Title | Name |
| ET | Tesla |
| ET | Wirth |
| ET | Holidaymaker |
| IT | Tesla |
| IT | Wirth |
| IT | Holidaymaker |
| DB | Tesla |
| DB | Wirth |
| DB | Holidaymaker |
results in all data records of the Professor table connected with the data records of the Lecture table that have the same value in the PersNr field. Professors without a lecture are included, the lecture columns in the result then have the value NULL. Lectures without a professor are not included.
The following query returns only those records for which no matching record exists in the left outer compound (all professors who do not hold lectures):
The same can be achieved by means of a subquery:
counts the number of lectures per professor using the aggregate function COUNT.
Note: COUNT(Professor.PersNr) or COUNT(*) would be incorrect (zero values should not be counted).
In summary, the most important elements of an SQL SELECT query can be described something like this:
Explanation:
Set operators can be applied to multiple SELECT queries that have the same number of attributes and where the data types of the attributes match:
inserts four data records into the table Lecture. The values must match the data types of the fields VorlNr, Titel, PersNr.
then returns the result, for example (the order can also be different):
| PrelNo | Title | PersNo |
| 1001 | Databases | 15 |
| 1000 | Software development 1 | 12 |
| 1200 | Networks 1 | 20 |
| 5001 | ET | 12 |
| 5022 | IT | 12 |
| 1600 | Algorithms | 12 |
| 5045 | DB | 15 |
changes all records for which PersNr has the value 15. The value of VorlNr is increased by 1000 and the value of PersNr is set to 20.
result of a subsequent SELECT *, possibly with a different order:
| PrelNo | Title | PersNo |
| 1000 | Software development 1 | 12 |
| 1200 | Networks 1 | 20 |
| 1600 | Algorithms | 12 |
| 2001 | Databases | 20 |
| 5001 | ET | 12 |
| 5022 | IT | 12 |
| 6045 | DB | 20 |
deletes all records for which PersNr has the value 12.
Result of a subsequent SELECT *, possibly in a different order:
| PrelNo | Title | PersNo |
| 1200 | Networks 1 | 20 |
| 2001 | Databases | 20 |
| 6045 | DB | 20 |
Generalized, the change directives look like this:
INSERT statement:
UPDATE statement:
DELETE statement:
You can create the database table Lecture with the following statement:
The value NULL is not allowed in any of the fields VorlNr, Titel, PersNr. The foreign key PersNr references the primary key PersNr of the table Professor. This ensures that only data records for which the value of PersNr occurs in the Professor table as a primary key can be inserted into the Lecture table (see referential integrity).
With the statement
a database index can be defined for the Lecture table, which can be used by the database system to speed up the execution of queries. Whether this makes sense is decided by the database system independently through complex evaluations and analyses for each query again.
A view is essentially an alias for a database query. It can be used like a database table. The statement
saves the defined query as a view. The query
uses this view and could, for example, return the following result:
| Title | PersNo |
| Software development 1 | Wirth |
| Networks 1 | Holidaymaker |
| Algorithms | Wirth |
| Databases | Holidaymaker |
In summary, the most important elements of the definition of a database table, database index, or view are as follows: