The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Lab (ECOM 4113)

Lab 3

SQL Subqueries and Set Operations

Eng. Ibraheem Lubbad Nested Queries (subqueries):

 A subquery is a SELECT statement that is embedded in the clause of another SELECT statement.  You can build a powerful statements out of simple ones by using subqueries.  You can write subqueries in the WHERE clause of another SQL statement to obtain values based on an unknown conditional value

Subquery Syntax:

SELECT SELECT_LIST FROM 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

SELECT TOT_CRED FROM STUDENT

WHERE DEPT_NAME='Elec. Eng.’;

Second Query

SELECT * FROM STUDENT

WHERE DEPT_NAME='Comp. Sci.' AND TOT_CRED > ALL(60,80)

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 parentheses”, which is called: sub query

Another Way Query

SELECT * FROM STUDENT OUTER QUERY WHERE DEPT_NAME='Comp. Sci.'

AND TOT_CRED > ALL ( INNER QUERY SELECT DISTINCT TOT_CRED FROM STUDENT WHERE DEPT_NAME= 'Elec. Eng.' );

 Types of Subqueries:

 Single-row subqueries: Queries that return only one row the inner SELECT statement.  Multiple-row subqueries: Queries that return more than one row from the inner SELECT statement.

A single-row subquery uses a single-row operator (>, <, =, <>, etc...). However, multiple-row subqueries use multiple-row operator (IN, ALL, ANY), instead of a single-row operator. The multiple-row comparison operators are:

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.

SELECT NAME, DEPT_NAME , SALARY FROM INSTRUCTOR WHERE SALARY < ALL (SELECT SALARY FROM INSTRUCTOR WHERE DEPT_NAME = 'Comp. Sci.’) AND DEPT_NAME <> 'Comp.Sci.';

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

SELECT NAME, DEPT_NAME, SALARY

FROM INSTRUCTOR INS1

WHERE SALARY >= ALL)SELECT SALARY FROM INSTRUCTOR INS2 WHERE INS1. DEPT_NAME = INS2. DEPT_NAME )

ORDER BY 2;

Set Operations: Set operators are used to combine the results of two or more component queries into one result. Queries containing set operators are called compound queries. In oracle, there are four Set Operators:

 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.

Example: Fine all courses taught in the fall 2009 semester.

SELECT COURSE ID FROM SECTION

WHERE SEMESTER = ’Fall’ AND YEAR= 2009;

Example: Fine all courses taught in the spring 2010 semester.

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