Lab 13 SQL SubQueries With ALL, ANY, EXISTS, SOME
Lab 13 SQL SubQueries With ALL, ANY, EXISTS, SOME
Lab-13
SQL Sub-Queries
Lab 13: SQL Subqueries
Contents
1. Introduction ..................................................................................................................................................... 2
If we want to find all the employees in each department who are earning more than the average salary with respect to
their department, following query will be used: ........................................................................................... 11
8. Evaluation Task (Unseen) [Expected time = 60mins for two tasks] ............................................................. 13
You have learnt the basic understanding of subqueries or inner query or nested query. Sub query
is a query within another SQL query. The inner query returns a value that is used by the outer
query. Using the sub query is equivalent to performing two sequential queries and using the
result of the first query as the search value in the second query.
• To get basic understanding of Subquery or Inner query or Nested query is a query within
another SQL query and embedded within the WHERE clause.
• To get basic understanding of Select using subquery.
• To understand the basic concept of Correlated Subquery.
• To get familiar with the basic ALL, ANY, EXISTS, SOME functionality.
4. Concept Map
4.1.Sub-Query
Results of one query can be used in another SQL statement. Subquery is useful if more than one
Department of Computer Science, Page 2
C.U.S.T.
Lab 13: SQL Subqueries
table is involved.
You can solve this problem by combining the two queries, placing one query inside the other
query.
SELECT * FROM `employees` WHERE SALARY > (SELECT SALARY FROM employees
WHERE first_name='Donald')
A subquery may return a scalar, a single column, a single row, or a table. Therefore, there are
three 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
• Multiple-column subquries: Queries that return more than one cloumns from the inner
SELECT Statement
Notice that the subquery must be enclosed in parentheses. You cannot use a scalar subquery
when a literal value is required, such as for an argument in a LIMIT clause. The outer and inner
queries can get data from different tables.
Subqueries that return more than one row are called multiple-row subqueries. You use a
multiple-row operator, instead of a single-row operator, with a multiple-row subquery. Multiple
row comparison operators are IN, ANY and ALL.
Operator Meaning
IN Equal to any member in the list
ANY Compare value to each value returned by the subquery
For example to find the employees who earn the same salary as the minimum salary for each
department, we will use the following subquery:
The inner query is executed first, producing a query result. The main query block is then
processed and uses the values returned by the inner query to complete its search condition. The
query will show the following result.
Figure 1
The ANY and ALL operators are supported syntax too and the following table summarizes the
equivalents for ANY and ALL:
Operator Meaning
<ANY Less than the highest
>ANY More than the lowest
ANY Not equal to any member in a list
=ANY Equivalent to IN
>ALL More than the highest
<ALL Less than the lowest
The following query will display the employees whose salary is less than the salary of all
employees with a job ID of ‘Sales Rep’ and whose job is not ‘Sales Rep’.
For example the following query will display employee_id, first_name, salary and department of
employees whose salary and department match both the salary and department of ‘Julia’.
Figure 2
Another method of writing a multiple column subquery is to use a subquery in the FROM clause
of a SELECT statement. A subquery in the FROM clause of a SELECT statement defines a data
source for that particular SELECT statement, and only that SELECT statement.
For example the following query will print the information of all the employees who are earning more
than average salary in their department.
Figure 3
If we want to find all the employees in each department who are earning more than the average
salary with respect to their department, following query will be used:
Figure 4
Note how the table qualifiers a and b are used in the example. This is necessary because the columns that are
used to correlate values from the inner and outer queries come from different references to the same table and
thus have the same name.
You must solve the following problems at home before the lab.
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you
Describe sub-query and its purpose; submit to lab teacher in soft form.
6. Procedure& Tools
6.1.Tools
6.2. Setting-up and Setting Up XAMPP (MySQL, Apache) [Expected time = 5mins]
This task is designed to guide you the use of different types of sub-queries.
Query to find out who earns salary greater than Donald’s salary is:
Figure 5
Query to find the employees who earn the same salary as the minimum salary for each
department is:
Figure 6
Query to display the employees whose salary is less than the salary of all employees with a job
ID of ‘Sales Rep’ and whose job is not ‘Sales Rep’ is:
Figure 7
Query to display display employee_id, first_name, salary and department of employees whose salary and
department match both the salary and department of ‘Julia’is:
Figure 8
If we want to find all the employees in each department who are earning more than the average
salary with respect to their department, following query will be used:
Figure 9
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\fs\assignments$\IDBS\Lab12
1. Show first name and salary of employees who are receiving salary equal to maximum
salary.
2. Show employee_id and salary of employees having salary more than average salary of all
employees.
3. Show name of countries whose at least one location record exists.
4. Who reports to Shelley?
5. Show the employee’s first_name, department_id, and name of manager for all
employees.
6. Report for each employee, the percentage value of his/her salary as a percentage of the
total salary paid to his/her department. Order the report by department_id and percentage
value of salary in descending order.
7. For countries containing more than one location, show location whose city name starts
with ‘A’.
8. Show name of employees who works in ‘Shipping’ department.
9. Show name of employees whose job’s start date or end date is from 2013.
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10.2. Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\fs\lectures$
11. REFERENCES: