SHAHEED ZULFIKAR ALI BHUTTO INSTITUTE
OF
SCIENCE AND TECHNOLOGY
(SZABIST)
Course: CSCL-2203 Database Systems
Department of Computer Science
Lab Manual
1) Stage J(Journey inside-out the concept)
2) Stage a1(Apply the learned)
3) Stage v(Verify the accuracy)
4) Stage a2(Assess your work)
1
LAB # 10
Statement Purpose:
Demonstrate the effective use of Sub queries in SQL to get accurate results
Activity Outcomes:
Understanding and implementation of Sub queries.
Instructor Note:
Discussion with students regarding home activities.
51
1) Stage J (Journey)
Subquery or Inner query or Nested query is a query within another SQL query and
embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition
to further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
There are a few rules that subqueries must follow:
Subqueries must be enclosed within parentheses.
A subquery can have only one column in the SELECT clause, unless multiple columns
are in the main query for the subquery to compare its selected columns.
An ORDER BY cannot be used in a subquery, although the main query can use an
ORDER BY. The GROUP BY can be used to perform the same function as the ORDER
BY in a subquery.
Subqueries that return more than one row can only be used with multiple value
operators, such as the IN operator.
The SELECT list cannot include any references to values that evaluate to a BLOB,
ARRAY, CLOB, or NCLOB.
A subquery cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a subquery; however, the BETWEEN
operator can be used within the subquery
2) Stage a1 (apply)
Lab Activities:
Subqueries with the SELECT Statement:
Subqueries are most frequently used with the SELECT statement. The basic syntax is as
follows:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
52
Example:
SQL> SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;
Subqueries with the INSERT Statement:
Subqueries also can be used with INSERT statements. The INSERT statement uses the
data returned from the subquery to insert into another table. The selected data in the
subquery can be modified with any of the character, date or number functions.
The basic syntax is as follows:
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
Example:
Consider a table CUSTOMERS_BKP with similar structure as CUSTOMERS table. Now to
copy complete CUSTOMERS table into CUSTOMERS_BKP, following is the syntax:
SQL> INSERT INTO CUSTOMERS_BKP
SELECT * FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS) ;
Subqueries with the UPDATE Statement:
The subquery can be used in conjunction with the UPDATE statement. Either single or
multiple columns in a table can be updated when using a subquery with the UPDATE
statement.
53
The basic syntax is as follows:
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Example:
Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS
table.
Following example updates SALARY by 0.25 times in CUSTOMERS table for all the
customers whose AGE is greater than or equal to 27:
SQL> UPDATE CUSTOMERS
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 27 );
Subqueries with the DELETE Statement:
The subquery can be used in conjunction with the DELETE statement like with any
other statements mentioned above.
The basic syntax is as follows:
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Example:
Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS
table.
54
Following example deletes records from CUSTOMERS table for all the customers whose
AGE is greater than or equal to 27:
SQL> DELETE FROM CUSTOMERS
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 27 );
Examples
We use the following tables for our examples.
Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
Table Geography
Region_Name Store_Name
East Boston
East New York
West Los Angeles
West San Diego
Example : Simple subquery
To use a subquery to find the sales of all stores in the West region, we use the following
SQL statement:
SELECT SUM (Sales) FROM Store_Information
WHERE Store_Name IN
(SELECT Store_Name FROM Geography
WHERE Region_Name = 'West');
Result:
SUM (Sales)
2050
55
In this example, instead of joining the two tables directly and then adding up only the
sales amount for stores in the West region, we first use the subquery to find out which
stores are in the West region, and then we sum up the sales amount for these stores.
Notice that in this example, the inner query and the outer query are independent of
each other. This type of subquery is called a simple subquery.
Example: Correlated subquery
If the inner query is dependent on the outer query, we will have a correlated
subquery. An example of a correlated subquery is shown below:
SELECT SUM (a1.Sales) FROM Store_Information a1
WHERE a1.Store_Name IN
(SELECT Store_Name FROM Geography a2
WHERE a2.Store_Name = a1.Store_Name);
Result:
SUM (Sales)
2750
Here, the inner query is used to make sure that SQL only sums up sales amount from
stores that appear in both the Store_Information and the Geography tables.
Notice the WHERE clause in the inner query, where the condition involves a table from
the outer query.
3) Stage v (verify)
Use the following schema to answer the question.
56
1) Write a query to find the names (first_name, last_name) and the salaries of the
employees who have a higher salary than the employee whose last_name='Bull'.
2) Write a query to find the names (first_name, last_name) of all employees who
works in the IT department.
3) Write a query to find the names (first_name, last_name) of the employees who
have a manager and work for a department based in the United States
4) Write a query to find the names (first_name, last_name) of the employees who
are managers
5) Write a query to find the names (first_name, last_name), the salary of the
employees whose salary is greater than the average salary
6) Write a query to find the names (first_name, last_name), the salary of the
employees whose salary is equal to the minimum salary for their job grade
7) Write a query to find the names (first_name, last_name), the salary of the
employees who earn more than the average salary and who works in any of the
IT departments
8) Write a query to find the names (first_name, last_name), the salary of the
employees who earn more than Mr. Bell
9) Write a query to find the names (first_name, last_name), the salary of the
employees who earn the same salary as the minimum salary for all departments
10)Write a query to find the names (first_name, last_name), the salary of the
employees whose salary greater than the average salary of all departments
4) Stage a2(assess)
Students will show the solution of their home Activity
57