Professional Documents
Culture Documents
Practical Relational Language:: Advanced SQL
Practical Relational Language:: Advanced SQL
Advanced SQL
Outline
• Handling NULL values
• Nested Queries
• JOIN Operation
• Aggregate Functions
• Grouping
Handling Null Value
• When a NULL value is involved in comparison, the result is UNKNOWN
• Three-valued logic
Handling Null Value
• Generally, in SQL and relational algebra
– ONLY the tuples evaluate to TRUE in the where
clause are selected.
• Exception: outer joins
Q2: SELECT C
Q5: R∩S
FROM R;
Handling Null Value
• Check whether an attribute value is NULL in SQL?
IS and IS NOT
OR
Pnumber IN ( SELECT Pno
FROM WORKS_ON, EMPLOYEE
WHERE Essn = Ssn AND Lname = ‘Smith’);
Nested Queries
• ALL,SOME(ANY)
– =SOME(or =ANY) is equivalent to the keyword IN
- Q10: Return 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 Salary
FROM EMPLOYEE
WHERE Dno = 5);
Nested Queries
• Correlated Nested Queries
– WHERE clause of a nested query references some attribute(s)
of a relation in the outer query
- Q11: Return the name of each employee who has a dependent with
the same first name and sex as the employee.
– SUM/MAX/MIN/AVG(empty)=NULL,
COUNT(empty)=0
Aggregate Functions in SQL
Q18: Retrieve the total number of employees in the
company.
Q20:
SELECT COUNT (DISTINCT Dlocation)
FROM DEPT_LOCATIONS;
Aggregate Functions in SQL
• Q21: Retrieve the names of employees who
have two or more dependents.
SELECT E.Lname, E.Fname
FROM EMPLOYEE AS E
WHERE ( SELECT COUNT (*)
FROM DEPENDENT
WHERE E.Ssn = Essn ) >= 2;
DEFAULT ‘Temporary’
DROP DEFAULT;