Complex Queries
Complex Queries
A nested query is a complete SQL query that is embedded within another SQL query. The
outer query is the main query, while the inner query (or subquery) provides a set of values
that the outer query can use.
This part retrieves project numbers (Pnumber) for projects managed by employees with the
last name 'Smith'.
It joins the PROJECT, DEPARTMENT, and EMPLOYEE tables to find the relevant project
numbers.
Second Nested Query:
This part retrieves project numbers (Pno) for projects where employees with the last name
'Smith' are working.
It joins the WORKS_ON and EMPLOYEE tables.
Outer Query:
The outer query selects distinct project numbers from the PROJECT table where the project
number is found in
either of the results from the nested queries.
The outer query selects the first name (Fname) and last name (Lname) of employees from the
EMPLOYEE table.
For each employee, it checks if their Ssn is in the result of the nested query.
Nested Query:
The nested query retrieves the Essn (employee social security number) from the
DEPENDENT table.
It filters dependents based on the condition that the dependent's name matches
the employee's first name and the sex matches the employee's sex.
Execution:
EXISTS Function
The EXISTS function checks whether a nested query returns any rows.
It returns TRUE if the nested query returns at least one tuple and FALSE if it returns no
tuples.
This function is often used in correlated nested queries.
Example of EXISTS
Consider the following query (Q16B):
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);
Explanation:
For each employee in the EMPLOYEE table, the nested query checks if there are any
dependents in the
DEPENDENT table that match the employee's social security number, sex, and first name.
NOT EXISTS Function
The NOT EXISTS function is the opposite of EXISTS. It returns TRUE if the nested query
returns no tuples and FALSE if it returns at least one tuple.
UNIQUE Function
The UNIQUE function checks if the result of a query contains duplicate tuples. It returns
TRUE if there are no duplicates and FALSE if duplicates exist.
Example of UNIQUE
sql
Run
Copy code
SELECT UNIQUE(column_name)
FROM table_name;
Explanation:
This query retrieves the distinct social security numbers (Essn) of employees who work on
projects with numbers 1, 2, or 3.
The IN operator is used to specify the explicit set of project numbers (1, 2, 3) that we are
interested in.
Renaming Attributes in SQL
You can rename attributes in the result set of a query using the AS keyword.
This is useful for making the output more readable or for avoiding naming conflicts.
This query retrieves the last names of employees and their supervisors from the EMPLOYEE
table.
The AS keyword is used to rename the columns in the result set:
E.Lname is renamed to Employee_name.
S.Lname is renamed to Supervisor_name.
The result will have clearer column headers, making it easier to understand the output.
If at least one matching dependent exists, the employee's name is included in the result.
In this example, the DEPARTMENT table is renamed to DEPT, and its attributes are adjusted
to match the EMPLOYEE table's join attribute.
The NATURAL JOIN automatically uses the renamed attributes for the join condition.
Outer Joins
Outer joins include all records from one table and the matched records from the other table. If
there is no match, NULL values are returned for the columns of the table that does not have a
match.
This query retrieves the last names of employees and their supervisors.
If an employee does not have a supervisor (i.e., Super_ssn is NULL), the employee will still
be
included in the result, with NULL values for the supervisor's name.
Multiway Joins
You can also join multiple tables in a single query, known as a multiway join.
In this syntax, the + operator indicates that the join is a left outer join,
meaning all records from the left table (E) will be included, along with matching records
from the right table (S).