The operation of projection
consists in selecting the name of the columns of table(s) which
one wishes to see appearing in the answer. If one wants to display
all the columns "*" should be used. The columns are given after
the SELECT clause.
-Display the Name
and the code sex of the students.
The operation of selection
consists in selecting rows (tuples) of one (or several) table(s)
which satisfy certain conditions. The conditions are expressed after
the WHERE clause.
-Display the list
of the students of male sex.
SELECT * FROM ETUDIANT WHERE Cdsexe='H';
Q02:
List teachers who have more than 2 years of
seniority in rank.
The operations of projection and selection can obviously
be used in the same SQL request.
-Display the number and the name of the students
born in 1980
SELECT Numetu, Nometu FROM ETUDIANT WHERE Dtnaiss >= '01-01-1980' AND Dtnaiss <= '12-31-1980';
Conversion
of the data and treatment of dates
A particular date must be expressed in the American
form (mm-dd-yyyy) and not in the french form (dd-mm-yyyy). One can
consider a particular date :
-As a character string. For example '06-25-1998'
(on June 25, 1998 ). One cannot , in that case, make calculation
but one can make comparisons.
-As a date. It is then necessary to use the data conversion function
"CAST " and one can then make calculations.
Syntax of the CAST function :
CAST (< col > AS < Datatype >)
<Col>: Column name or value.
It is possible, with the ORDER BY clause, to order
the results of a query according to the value of some attributes
(columns).
- Display the list of the teachers by rank and
in decreasing order of names.
SELECT Grade, Nomens FROM ENSEIGNANT ORDER BY Grade, Nomens DESC;
The preceding query, which uses the names of columns,
is equivalent to the following query which uses the numbers of the
columns which are used to order results.
SELECT Grade, Nomens FROM ENSEIGNANT ORDER BY 1, 2 DESC;