Anda di halaman 1dari 8

Using Subqueries to solve queries

Welcome to the seventh module of this course. For this lesson, a


subquery which help solve queries is discuss as well using subqueries
to copy data from one table to another table, update values using the
data of other table and delet exisiting data using the values from other
table.

Subquery
In Oracle, a subquery is a query within a query. You can create
subqueries within your SQL statements. These subqueries can
reside in the WHERE clause, the FROM clause, or the SELECT
clause.

A subquery is a SELECT statement that is embedded in the clause of


another SELECT statement. You can build powerful statements
out of simple ones by using subqueries. They can be very useful
when you need to select rows from a table with a condition that
depends on the data in the table itself.

You can place the subquery in a number of SQL clauses, including the
following:

• WHERE clause

• HAVING clause

• FROM clause

After completing this lesson, the student should be able to:

– Apply subquery in DML language to insert, update and


delete values using data on another table.

– Apply subquery in a Basic select statement

– Apply subquery using Single row and Multiple row


function.

– Make used of IN, ANY and ALL

1
Applying Subqueries in DML

• You can use subqueries in DML statements to:

– Copy data from one table to another

– Retrieve data from an inline view

– Update data in one table based on the values of another table

– Delete rows from one table based on rows in a another table

Copying Rows from Another Table

• You can use the INSERT statement to add rows to a table where the
values are derived from existing tables. In place of the VALUES
clause, you use a subquery.

• Syntax

INSERT INTO table [ column (, column) ] subquery;

• In the syntax:
table is the table name
column is the name of the column in the table to
populate
subquery is the subquery that returns rows into the
table
• Example:

INSERT INTO COPY_EMPLOYEES


(ID, LASTNAME, JOB_ID, SALARY)
SELECT EMPLOYEE_ID, LASTNAME,JOB_ID,
SALARY
FROM EMPLOYEES
WHERE SALARY >= 10000;

 In the example: values that match the where condition inside


the subquery will be added to COPY_EMPLOYEES table.

2
 Do not use the VALUES clause.

 Match the number of columns in the INSERT clause with that


in the subquery.

Inserting rows using a Subquery as a target

• You can use a subquery in place of the table name in the INTO
clause of the INSERT statement.

• The select list of this subquery must have the same number of
columns as the column list of the VALUES clause.

• Any rules on the columns of the base table must be followed in


order for the INSERT statement to work successfully. For
example, you cannot put in a duplicate employee ID or leave
out a value for a mandatory NOT NULL column.

• This application of subqueries helps avoid having to create a


view just for performing an INSERT.

• Example:

INSERT INTO
(SELECT EMPLOYEE_ID,LASTNAME,JOB_ID,SALARY
FROM EMPLOYEES
WHERE SALARY >= 10000)
VALUES(10,'CRUZ','IT_MGR',25000);

Updating values based on another table 

Example:

UPDATE COPY_EMPLOYEES
SET JOB_ID=
(SELECT JOB_ID
FROM EMPLOYEES
WHERE EMPLOYEE_ID = 104)
WHERE ID=174;

Updating values based on another table: set to NULL

 Example:

3
UPDATE COPY_EMPLOYEES
SET LASTNAME = (SELECT LASTNAME
FROM EMPLOYEES
WHERE LASTNAME='REYES')
WHERE ID=205;

Deleting rows based on another table

 You can use subqueries to delete rows from a table based on values
from another table

 Example:

DELETE FROM COPY_EMPLOYEES


WHERE JOB_ID= (SELECT JOB_ID
FROM EMPLOYEES
WHERE JOB_ID ='AD_PRES');

Using a Subquery to solve other queries

Syntax: Subquery

SELECT COLUMN_LIST
FROM TABLE
WHERE CONDITION
SELECT COLUMN_LIST
FROM TABLE

Rules for Using Subqueries

• Enclose subqueries in parentheses.

• Place subqueries on the right side of the comparison condition for


readability. (However, the subquery can appear on either side of the
comparison operator.)

• Use single-row operators with single-row subqueries and multiple-row


operators with multiple-row subqueries.

Types of Subqueries

4
Single-Row Subqueries

• Return only one row

• Use single-row comparison operators

Multiple-Row Subqueries

• Return more than one row

• Use multiple-row comparison operators

5
LESSON SUMMARY:

• A subquery is used to enhanced the SELECT statement.


– Can be applied on:
• Single row function;
• Multiple row; and
• IN / ANY and ALL operator
• Instead of using view to insert data subquery can also be applied to:
– Get data on another table
– Update row using data on another table
– Delete rows using values on another table.

6
Activities/Exercises
• Download the Laboratory Exercise 6: Applying Subqueries
• Copy and paste the three (3) table EMPLOYEES,
DEPARTMENTS and LOCATIONS
• Perform the PL/SQL needed per number in order for you to
answer the questions in eacg number. Write the excat
answer

Glossary
 ALL - must be preceded by comparison operator. Returns true if at
least oe element exist in the result.

 ANY – must be preceded by comparison operator. Returns true if


the relation is TRUE for all elements in the result

 IN – equal tpo any member in the lists.

 Multiple-Row Subqueries - return more than one row and use


multiple-row comparison operators

 Single-Row Subqueries - return only one row and use singlerow


comparison operators

7
References
Textbook:
• Oracle Database 11g 2nd Edition K Gopalakrishnan ( 2012)

References:
• Carlos, Peter (2009). Database Systems
• Connoly, Thomas & Begg, Carolyn (2010). Database
Systems : A practical approach to design,
implementation and management
• Sciore, Edward (2009). Database Design and Implementation
• Bulusu, Lakshman (2008). Oracle PL/SQL : Expert
Techniques for Developers and Database Administrators
• Loshin, David (2008). Master Data Management

Other Suggested Readings (e.g. periodicals, articles, websites, IT


applications/software, etc.):
• www.oracle.com
• www.apex.oracle.com
• SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
• SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/S QL
• Database Administration. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/Database_administ
ration.aspx
• SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspxLearning Icons

Anda mungkin juga menyukai