Basic query: SELECT columns FROM table WHERE condition is true
The FROM
clause is like declaring variables that range over tuples of
a relation:
SELECT E.ENO, E.TOPIC -- choose columns ENO, TOPIC
FROM EXERCISES E -- table exercises, E being an 'alias' for the current row
WHERE E.CAT = 'H' -- where the column CAT contains the value 'H'
If name of tuple variable ('E' in the code above) is not given explicitly, the variable will have the name of the relation ('EXERCISES' in the code above).
A reference to attribute A of variable R may be written as A
if R is
the only tuple variable with an attribute named A.
It is almost always an error if there are two tuple variables that aren't linked via join conditions.
Don't join more tables than needed.
In some scenarios, we might have to consider more than one tuple of the same relation to get a result tuple.
Duplicates have to be explicitly eliminated, using DISTINCT
.
Sufficient condition for superfluous DISTINCT, where K is set of attributes uniquely determined by result.
FROM
,
then DISTINCT
is superfluous.Typical mistakes:
DISTINCT
monotonic: if further rows get inserted, the queries yield a superset of rows
non-monotonic: 'there is no', 'does not exist', 'for all', 'min/max'. testing whether or not a query yields an empty result.
example to select students without any homework result:
SELECT FIRST, LAST
FROM STUDENTS
WHERE SID NOT IN (SELECT SID
FROM RESULTS
WHERE CAT = 'H')
conceptually, the subquery (SELECT SID...
) is evaluated before main
query.
constructs:
NOT IN
: something is not in result of subqueryNOT EXISTS
: result of subquery is emptyEXISTS
. In
logic, ∀X(φ) ↔ ¬∃ X (¬ φ)
.∀X (φ₁ → φ₂)
becomes ¬∃ X (φ₁ ∧ ¬ φ₂)
Comparing values:
ALL
: compare with all values in a set, has to be true for allANY
/SOME
: has to be true for at least some values in a set
(also, x IN s
== x = ANY s
)POINTS = (SELECT ...)
) must
return a single row