Lab - 3 - Subqueries and Set Operations
Lab - 3 - Subqueries and Set Operations
Faculty of Engineering
Dept. of Computer Engineering
Database Lab (ECOM 4113)
Lab 3
SQL
Subqueries and Set Operations
Subquery Syntax:
SELECT SELECT_LIST
FROM TABLE
WHERE EXPR OPERATOR (SELECT SELECT_LIST
FROM TABLE );
The subquery (inner query) executes before the main query (outer
query).
The result of the subquery is used by main query
Example: Find all students who are study in departments Computer Science AND
their total credit is greater than ALL students in Elec. Eng.
First solution: retrieve targeted students with two steps; firstly, retrieve total
credit of all students in Elec. Eng. (without duplication), then, use the retrieve
values in another query. The first query
First Query
The previous solution is not wrong. However, it is not a practical solution since
you apply it with many steps and each step needs a human to do them (cannot
applied by machine since the retrieve values are dynamic and can be changed in
any time. A better solution is to embedded the first query in the second query in
“All’s condition parentheses”, which is called: sub query
Types of Subqueries:
Single-row subqueries: Queries that return only one row from the inner
SELECT statement.
Multiple-row subqueries: Queries that return more than one row from the
inner SELECT statement.
Operator Meaning
IN Equal to any member in the list
ANY Must be preceded by =, <>, >, <, <=, >=. Compares a
value to each value in a list or returned by a query.
Evaluates to FALSE if the query returns no rows.
ALL Must be preceded by =, <>, >, <, <=, >=. Compares a
value to every value in a list or returned by a query.
Evaluates to TRUE if the query returns no rows.
Example: Find the names of all instructors whose salary is greater than at least
one Instructor in the Finance department.
SELECT NAME
FROM INSTRUCTOR
WHERE SALARY > ANY (SELECT SALARY
FROM INSTRUCTOR
WHERE DEPT_NAME = 'Finance');
Example: Find all instructors whose salary is less than the salary of all instructors
in the Computer Science department and whose department name is not
Computer Science.
Example: Find the student name and department name of all student who study in
a department with any student whose name contains the letter “S”.
SELECT NAME,DEPT_NAME
FROM STUDENT
WHERE DEPT_NAME IN ( SELECT DEPT_NAME
FROM STUDENT
WHERE NAME LIKE '%S%' );
WHERE DEPT_NAME = 'COMP. SCI.'
)
AND DEPT_NAME <> 'COMP. SCI.';
Example: Find instructors whose salary is more than the salary of any employee
from department ‘Physics’
SELECT NAME,DEPT_NAME,SALARY
FROM INSTRUCTOR
WHERE SALARY > ANY ( SELECT SALARY
FROM INSTRUCTOR
WHERE DEPT_NAME = 'Physics' );
Example: Find the name and department name, for instructors whose salary is
more than all their collagenous’ salaries in the same department
UNION : return all rows from its first table and the second table
A∪B
INTERSECT : return all rows from its first table and the second table
A∩B
MINUS: return all rows from its first table that do not occur in the second
table
A-B
Notes:
Operators automatically eliminates duplicates , If we want to retain
duplicates, we must add all after Operators for example UNION all ,
INTERSECT all , MINUS all
Column count of all component queries must be the same.
Data types of retrieved columns should match or at least should
be implicitly convertible by database.
SELECT COURSE ID
FROM SECTION
WHERE SEMESTER = ’Fall’ AND YEAR= 2009;
SELECT COURSE ID
FROM SECTION
WHERE SEMESTER = ’Spring’ AND YEAR= 2010;
Example: find all courses taught either in Fall 2009 or in Spring 2010, or both.
(SELECT COURSE_ID
FROM SECTION
WHERE SEMESTER = 'Fall' AND YEAR= 2009)
UNION
(SELECT COURSE_ID
FROM SECTION
WHERE SEMESTER = 'Spring' AND YEAR= 2010);
Example: find the set of all courses taught in the Fall 2009 as well as in Spring
2010
(SELECT COURSE ID
FROM SECTION
WHERE SEMESTER = ’Fall’ AND YEAR= 2009)
INTERSECT
(SELECT COURSE ID
FROM SECTION
WHERE SEMESTER = ’Spring’ AND YEAR= 2010);
Example: find all courses taught in the Fall 2009 semester but not in the Spring
2010
(SELECT COURSE_ID
FROM SECTION
WHERE SEMESTER = 'Fall' AND YEAR= 2009)
MINUS
(SELECT COURSE_ID
FROM SECTION
WHERE SEMESTER = 'Spring' AND YEAR= 2010);
END