More Complex SQL Retrieval Queries
More Complex SQL Retrieval Queries
https://www.brainkart.com/article/More-Complex-SQL-Retrieval-Queri
es_11415/
http://masud.lecture.ub.ac.id/files/2018/05/SQL2.pdf
1. Comparisons Involving NULL and Three-Valued Logic
• SQL has various rules for dealing with NULL values.
• NULL is used to represent a missing value, but that it usually has one of three different interpretations
• value unknown (exists but is not known)
• Unknown value. A person’s date of birth is not known, so it is represented by NULL in the database.
• value not available (exists but is purposely withheld)
• Unavailable or withheld value. 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.
• value not applicable (the attribute is undefined for this tuple).
• 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
• When a NULL is involved in a comparison operation, the result is considered to be UNKNOWN (it may
be TRUE or it may be FALSE)
• Hence, SQL uses a three-valued logic with values TRUE, FALSE, and UNKNOWN instead of the standard
two-valued (Boolean) logic with values TRUE or FALSE
• SQL allows queries that check whether an attribute value is NULL
• Rather than using = or <> to compare an attribute value to NULL, SQL uses the comparison operators IS or IS
NOT
• Retrieve the names of all employees who do not have supervisors.
• SELECT Fname, Lname FROM EMPLOYEE WHERE Super_ssn IS NULL;
2. Nested Queries, Tuples, and Set/Multiset Comparisons
• Some queries require that existing values in the database be fetched and then used in a comparison condition
• which are complete select-from-where blocks within the WHERE clause of another query.
• That other query is called the outer query.
• Introduces the comparison operator IN, which compares a value v with a set (or multiset) of values V
evaluates to TRUE if v is one of the elements in V.
Queries
• Retrieve the name and address of all employees who work for the
'Research' department.
• Retrieve the name of each employee who has a dependent with the
same first name as the employee.
• The EXISTS function in SQL is used to check whether a subquery returns any rows.
• It returns a Boolean value, either TRUE if the subquery returns one or more rows,
or FALSE if the subquery does not return any rows.
• Suppose you have a database with two tables: Customers and Orders.
• Query -You want to find all customers who have placed at least one order
SELECT CustomerID, CustomerName
FROM Customers
WHERE EXISTS (
SELECT 1
FROM Orders
WHERE Orders.CustomerID = Customers.CustomerID );
SQL JOIN
• A JOIN clause is used to combine rows from two or more
tables, based on a related column between them.
Inner JOIN
CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, EmployeeName VARCHAR(50),
DepartmentID INT );