0% found this document useful (0 votes)
12 views5 pages

8 Dbms

This document outlines Experiment No. 8, focusing on sub queries and nested queries in Oracle 10g. It explains the definition, usage, and syntax of sub queries with SELECT, INSERT, UPDATE, and DELETE statements, along with rules governing their use. The document concludes with questions related to the concepts discussed, such as types of sub queries and specific query examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

8 Dbms

This document outlines Experiment No. 8, focusing on sub queries and nested queries in Oracle 10g. It explains the definition, usage, and syntax of sub queries with SELECT, INSERT, UPDATE, and DELETE statements, along with rules governing their use. The document concludes with questions related to the concepts discussed, such as types of sub queries and specific query examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NO: 8

TITLE: To Perform Sub Queries and Nested Queries .Aim:

To study sub queries and nested queries in oracle.

Requirements: Oracle 10g.

Theory:

A Sub query or Inner query or Nested query is a query within another SQL query and
embedded within the WHERE clause.

A sub query is used to return data that will be used in the main query as a condition to
further restrict the data to be retrieved.

Sub queries 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 sub queries must follow:

Sub queries must be enclosed within parentheses.


A sub query can have only one column in the SELECT clause, unless multiple
columns are in the main query for the sub query to compare its selected columns.
An ORDER BY cannot be used in a sub query, 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 sub query.
Sub queries 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 sub query cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a sub query; however, the BETWEEN
operator can be used within the sub query.
Sub queries with the SELECT Statement:

Sub queries 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])

● Sub queries with the INSERT Statement:

Sub queries 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 ]

● Sub queries 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.

The basic syntax is as follows:

UPDATE table

SET column_name=new_value

[ WHERE OPERATOR [ VALUE ]


(SELECT COLUMN_NAME

FROM
TABLE_NAME)[
WHERE)]

● Sub queries with the DELETE Statement:

The sub query 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)]

Conclusion:

Questions:

1. Explain types of sub queries.

2. Which is a method for writing a sub-query in a main query?


3. Write query: Display all employees who do not have any reporters.

4. Write a query to get 4th max salary from EMP table.

5. Write a query to get first 3 salaries from the EMP table.


6. What is a nested query in oracle?

7. What is sub query?

You might also like