Advanced SQL Flashcards
(129 cards)
3 interpretations of null value
value unknown, value unavailable or withheld, not applicable attribute
unknown value
exists but is not known
A person’s date of birth is not known, so it is represented by NULL in the database.
Unavailable or withheld value
exists but is purposely withheld.A person has a home phone but does not want it to be listed, so it is withheld and represented as NULL in the database.
Not applicable attribute
An attribute LastCollegeDegree would be NULL for a person who has no college degrees because it does not apply to that person.
sql and meanings of NULL
It is often not possible to determine which of the meanings is intended; for example, a NULL for the home phone of a person can have any of the three meanings. Hence, SQL does not distinguish between the different meanings of NULL.
each individual NULL value is considered to be
different from every other NULL value in the various database records
When a NULL is involved in a compari- son operation, the result is considered to be
UNKNOWN (it may be TRUE or it may be FALSE)
SQL uses a ______ logic with values ____
three valued
true false unknown
In select-project-join queries, the general rule is that only those combinations of tuples that evaluate the logical expression in the WHERE clause of the query to ___ are selected
true
Tuple combinations that evaluate to FALSE or UNKNOWN are
not selected
SQL allows queries that check whether an attribute value is NULL. Rather than using = or to compare an attribute value to NULL, SQL uses
comparison operators IS or IS NOT
because SQL considers each NULL value as being distinct from every other NULL value, so equality comparison is not appropriate.
Retrieve the names of all employees who do not have supervisors.
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;
nested query
queries require that existing values in the database be fetched and then used in a comparison condition
complete select-from-where blocks within the WHERE clause of another query (outer query)
comparison operator IN
compares a value v with a set (or multiset) of values V and evaluates to TRUE if v is one of the elements in V.
The first nested query selects the project numbers of projects that have an employee with last name ‘Smith’ involved as manager, while the second nested query selects the project numbers of projects that have an employee with last name ‘Smith’ involved as worker. In the outer query, we use the OR logical connective to retrieve a PROJECT tuple if the PNUMBER value of that tuple is in the result of either nested query.
SELECT DISTINCT Pnumber FROM PROJECT WHERE Pnumber IN (SELECT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Lname = 'Smith' ) OR Pnumber IN (SELECT Pno FROM WORKS_ON, EMPLOYEE WHERE Essn = Ssn AND Lname = 'Smith' );
If a nested query returns a single attribute and a single tuple, the query result will be a
single (scalar) value
in cases of single value results its permissible to
use = instead of IN for the comparison operator
nested query will return
table (relation), which is a set or multiset of tuples
SQL allows the use of tuples of values in comparisons by
placing them within parentheses.
query will select the Essns of all employees who work the same (project, hours) combination on some project that employee ‘John Smith’ (whose Ssn = ‘123456789’) works on
SELECT DISTINCT Essn FROM WORKS_ON WHERE (Pno, Hours) IN ( SELECT FROM WHERE Pno, Hours WORKS_ON Essn=‘123456789’ );
In addition to the IN operator, a number of other comparison operators can be used to compare a single value v (typically an attribute name) to a set or multiset v (typ- ically a nested query)
=ANY (or =SOME)
ALL
= ANY (or = SOME) operator
returns TRUE if the value v is equal to some value in the set V and is hence equivalent to IN. The two keywords ANY and SOME have the same effect. Other operators that can be combined with ANY (or SOME) include >, >=,
keyword ALL
can also be combined with each of these operators. For example, the comparison condition (v > ALL V) returns TRUE if the value v is greater than all the values in the set (or multiset) V
returns the names of employees whose salary is greater than the salary of all the employees in department 5
SELECT Lname, Fname FROM EMPLOYEE WHERE Salary > ALL ( SELECT FROM WHERE Salary EMPLOYEE Dno=5 );