Advanced SQL Flashcards
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 );
The rule is that a reference to an unqualified attribute refers to the relation declared in
innermost nested query
To refer to an attribute of the PROJECT relation specified in the outer query, we
specify and refer to an alias (tuple variable) for that relation
Retrieve the name of each employee who has a dependent with the same first name and is the same sex as the employee.
SELECT E.Fname, E.Lname FROM EMPLOYEE AS E WHERE E.Ssn IN ( SELECT Essn FROM DEPENDENT AS D WHERE E.Fname=D.Dependent_name AND E.Sex=D.Sex );
Correlated nested query
Whenever a condition in the WHERE clause of a nested query references some attrib- ute of a relation declared in the outer query
the nested query is evaluated
once for each tuple (or combination of tuples) in the outer query
SELECT E.Fname, E.Lname FROM EMPLOYEE AS E WHERE E.Ssn IN ( SELECT Essn FROM DEPENDENT AS D WHERE E.Fname=D.Dependent_name AND E.Sex=D.Sex );
Explain thought process
For each EMPLOYEE tuple, evaluate the nested query, which retrieves the Essn values for all DEPENDENT tuples with the same sex and name as that EMPLOYEE tuple; if the Ssn value of the EMPLOYEE tuple is in the result of the nested query, then select that EMPLOYEE tuple
Retrieve the name of each employee who has a dependent with the same first name and is the same sex as the employee. (without nesting)
SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E, DEPENDENT AS D
WHERE E.Ssn=D.Essn AND E.Sex=D.Sex
AND E.Fname=D.Dependent_name;
In general, a query written with nested select-from-where blocks and using the = or IN comparison operators can
always be expressed as a single block query
EXISTS
used to check whether the result of a correlated nested query is empty (contains no tuples) or not
result of EXISTS
Boolean value TRUE if the nested query result contains at least one tuple, or FALSE if the nested query result contains no tuples
SELECT E.Fname, E.Lname FROM EMPLOYEE AS E WHERE EXISTS ( SELECT * FROM DEPENDENT AS D WHERE E.Ssn=D.Essn AND E.Sex=D.Sex AND E.Fname=D.Dependent_name);
EXPLAIN THOUGHT PROCESS
For each EMPLOYEE tuple, evaluate the nested query, which retrieves all DEPENDENT tuples with the same Essn, Sex, and Dependent_name as the EMPLOYEE tuple; if at least one tuple EXISTS in the result of the nested query, then select that EMPLOYEE tuple.
EXISTS(Q) returns TRUE if
there is at least one tuple in the result of the nested query Q, and it returns FALSE otherwise
NOT EXISTS(Q) returns TRUE if
there are no tuples in the result of nested query Q, and it returns FALSE otherwise
Retrieve the names of employees who have no dependents. (Using exists)
SELECT Fname, Lname FROM EMPLOYEE WHERE NOT EXISTS ( SELECT * FROM DEPENDENT WHERE Ssn=Essn );
SELECT Fname, Lname FROM EMPLOYEE WHERE NOT EXISTS ( SELECT * FROM DEPENDENT WHERE Ssn=Essn );
EXPLAIN
or each EMPLOYEE tuple, the correlated nested query selects all DEPENDENT tuples whose Essn value matches the EMPLOYEE Ssn; if the result is empty, no dependents are related to the employee, so we select that EMPLOYEE
tuple and retrieve its Fname and Lname.
List the names of managers who have at least one dependent.
using exists
SELECT Fname, Lname FROM EMPLOYEE WHERE EXISTS ( SELECT * FROM DEPENDENT WHERE Ssn=Essn ) AND EXISTS ( SELECT * FROM DEPARTMENT WHERE Ssn=Mgr_ssn );
SELECT Fname, Lname FROM EMPLOYEE WHERE EXISTS ( SELECT * FROM DEPENDENT WHERE Ssn=Essn ) AND EXISTS ( SELECT * FROM DEPARTMENT WHERE Ssn=Mgr_ssn );
Explain
the first selects all DEPENDENT tuples related to an EMPLOYEE, and the sec- ond selects all DEPARTMENT tuples managed by the EMPLOYEE. If at least one of the first and at least one of the second exists, we select the EMPLOYEE tuple
Retrieve the Social Security numbers of all employees who work on project numbers 1, 2, or 3
use explicit set of values
SELECT DISTINCT Essn
FROM WORKS_ON
WHERE Pno IN (1, 2, 3);
In SQL, it is possible to rename any attribute that appears in the result of a query by
adding the qualifier AS followed by the desired new name
AS construct can be used to alias both
attribute and relation names, and it can be used in both the SELECT and FROM clauses
retrieve the last name of each employee and his or her supervisor, while renaming the resulting attribute names as Employee_name and Supervisor_name.
SELECT E.Lname AS Employee_name, S.Lname AS Supervisor_name
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
joined table (or joined relation)
specify a table resulting from a join operation in the FROM clause of a query
retrieves the name and address of every employee who works for the ‘Research’ department
use join statement
SELECT Fname, Lname, Address
FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)
WHERE Dname=‘Research’;
The attributes of a joined table are
all the attributes of the first table, EMPLOYEE, followed by all the attributes of the second table, DEPARTMENT
NATURAL JOIN
Names of joining attribute same
NATURAL JOIN EMPLOYEE TO DEPARTMENT
FROM (EMPLOYEE NATURAL JOIN DEPARTMENT)
INNER JOIN
default join where a tuple is included in result only if a matching tuple exists in the other relation