SQL Commands

This section contains a selection of important keywords that are sufficient for simple queries. Further commands can be found in the literature.

 

SELECT und FROM - Basic data query modules

SELECT *

FROM <table>

The asterisk (*) in SELECT * instructs the database to return all columns of the table specified in the FROM clause. The order of the columns in the output corresponds to the display in the database.

If only certain columns are to be output, they can be separated by commas. The order of the columns can be defined as desired:

SELECT <column1>, <column2>, <column3>, ...

FROM <table>

Query without repetitions:

If not all columns are selected, it may happen that some records look the same:

SELECT AMOUNT FROM CHECKS

AMOUNT

------

150

245,34

150

25

25,1

With the keyword DISTINCT, such duplications can be avoided and identical data rows can only be output once.

SELECT DISTINCT AMOUNT FROM CHECKS

AMOUNT

------

150

245,34

25

25,1

 

WHERE

Previously, all data records in a table were output. However, if a particular element or group of elements is searched in a database, one or more conditions are required. These conditions are contained in the WHERE clause.

SELECT <column1>, <column2>, <column3>, ...

FROM <table>

WHERE <search condition>

NAME

FRAME

MATER­IAL MILAGE TYPE
TREK 2300 23 CARBON FIBER 3500 RACING
BURLEY 22 STEEL 2000 TANDEM
GIANT 19 STEEL 1500 COM­MUTER
FUJI 20 STEEL 500 TOURING
SPECIA­LIZED 16 STEEL 100 MOUN­TAIN
CANNON­DALE 23 ALUMI­NUM 3000 RACING

If you want to select a specific bicycle from the table, enter the following statement:

SELECT NAME, FRAME, MATERIAL, MILAGE, TYPE

FROM BICYCLE

WHERE NAME = 'BURLEY'

NAME

FRAME

MATER­IAL MILAGE TYPE
BURLEY 22 STEEL 2000 TANDEM

All bicycles with a mileage greater than 1500:

SELECT NAME, FRAME, MATERIAL, MILAGE, TYPE

FROM BICYCLE

WHERE MILAGE > 1500

 

ORDER BY

The SELECT FROM statement only generates a list in which the results of the query are displayed in the order in which the data was entered.

With the ORDER BY clause, the results can be put in a certain order. For example, the following instruction arranges the list of bicycles according to mileage:

SELECT NAME, FRAME, MATERIAL, MILAGE, TYPE

FROM BICYCLE

WHERE MILAGE >= 1500

ORDER BY MILAGE DESC

NAME

FRAME

MATER­IAL MILAGE TYPE
TREK 2300 23 CARBON FIBER 3500 RACING
CANNON­DALE 23 ALUMI­NUM 3000 RACING
BURLEY 22 STEEL 2000 TANDEM
GIANT 19 STEEL 1500 COM­MUTER

The keyword DESC indicates that you want to sort in descending order. In ascending order, either the keyword is omitted or ASC is used.

If several expressions have to be sorted, separate them with commas. The table is then sorted in this order.

 

Aggregate functions

Aggregate or group functions return a value based on the values in a column. The most frequently used aggregate functions (display color) are:

  • COUNT
  • SUM
  • AVG
  • MAX
  • MIN

For example, the following expression is used to determine the number of records in a table:

SELECT COUNT(*)

FROM BICYCLE

You can also use the keyword DISTINCT:

SELECT COUNT(DISTINCT TYPE)

FROM BICYCLE

This expression counts all values that occur in the TYPE column, but does not count the same values twice. The result 5 would be applied to the following table. RACING exists twice, but is only counted once.

NAME

FRAME

MATER­IAL MILAGE TYPE
TREK 2300 23 CARBON FIBER 3500 RACING
BURLEY 22 STEEL 2000 TANDEM
GIANT 19 STEEL 1500 COM­MUTER
FUJI 20 STEEL 500 TOURING
SPECIA­LIZED 16 STEEL 100 MOUN­TAIN
CANNON­DALE 23 ALUMI­NUM 3000 RACING

The number of bicycles made of STEEL is determined as follows: mountain

SELECT COUNT(*)

FROM BICYCLE

WHERE MATERIAL = 'STEEL'

The queries in the MILAGE column of the table above can be demonstrated very well:

SELECT

COUNT(NAME) AS Quantity,

SUM(NAME) AS Total,

AVG(NAME) AS Average,

MAX(NAME) AS Maximum,

MIN(NAME) AS Minimum

FROM BICYCLE

Quant­ity Total Average Maximum Min­imum
6 10600 1766,66 3500 100

If further information, such as MATERIAL or TYPE, is to be added, it must be included in the GROUP BY clause. Otherwise the query leads to an error.

 

GROUP BY

The GROUP BY clause specifies the columns to be used for grouping. This allows queries to be applied to individual groups rather than to the entire result.

For example, if you want to determine how many bicycles there are per material, use the following expression:

SELECT MATERIAL, COUNT(NAME) AS QUANTITY

FROM BICYCLE

GROUP BY MATERIAL

The result is displayed:

MATERIAL QUANTITY
CARBON FIBER 1
STEEL 4
ALUMINUM 1

 

HAVING

Like the WHERE clause, the HAVING clause can further restrict the result of the GROUP BY command. Aggregate functions can also be used in the restriction:

SELECT MATERIAL, COUNT(Name) AS QUANTITY

FROM BICYCLE

GROUP BY MATERIAL

HAVING COUNT(Name) > 1

MATERIAL QUANTITY
STEEL 4