You are on page 1of 13

SUBQUERY

 A subquery is a “query within a query”.

 It is a SELECT statement that is nested within


another
SELECT statement. The result of the inner one is
passed as an argument to the outer one.

The subquery (inner query) executes once before the


main query (outer query).
Syntax for SubQuery
 Synatx :
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list FROM table);

 The result of the subquery is used by the main query.


 Enclose subqueries in parentheses.
 Place subqueries on the right side of the comparison condition.
 Use single-row operators with single-row subqueries, and use
multiple-row operators with
multiple-row subqueries.
Types of Subquery
 Single – row subquery => returns only – one row.
 Multiple – row subquery => returns multiple rows.

 Correlated subquery => subquery is executed once for every row produced by the
outer query.

 Use single-row comparison operators


such as > , < , <= , >= , = , <> for single row subquery.

 Use multiple-row comparison operators


such as IN , ANY , ALL for multiple row subquery.

Multiple row subquery operator should be prefixed with any of the single –row
comparison operator.
Multiple row subquery operators
<ANY => <MAX
>ANY=> >MIN
=ANY => Same as the IN operator
>ALL => >MAX
<ALL => <MIN
Other Database Objects
 Views
 Sequence
 Synonyms
 Index
Views
 A logical table which is a combination of one or more table.

 A view derives its data from the tables on which it is based.

 Advantages:
 Restrict database access
 To make complex queries easy
 To provide data independence
 To present different views of the same data
Types of Views
 Simple view => only one base table
=> DML possible through view

 Complex view => more than one base table


=> DML not possible through view

Syntax:

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view


[(alias)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];
Sequence
 Automatically generates primary key values
 It is a sharable objects
 Speeds up the efficiency of accessing sequence values when cached in the
memory.
 Replaces the application code.
 Syntax
CREATE SEQUENCE sequence_name
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE }]
[{CACHE n | NOCACHE];
Synonyms
 Other name given for the schema object.
 Syntax:
CREATE [PUBLIC] SYNONYM synonym_name
FOR object;
 Why synonym:
 - To shorten lengthy object name
 - Easy reference for the objects owned by another user.

 To drop a synonym:
Drop synonym synonym_name
Index
 Speeds up the retrieval of rows by using pointers
 Reduces Disk I/O.
 Types:
 Unique Index => Automatically created
 Non-Unique Index => Manually created.
 Syntax:
 Create index <indexname> on <tablename>(columnname);

 Drop index:
 Drop index <indexname>;
FlashBack
 Flashback query allows a user to view the data quickly and
easily the way it was at a particular time in the past, even
when it is modified and committed, be it a single row or the
whole table.
 Flashback technologies are applicable in repairing the
following user errors.
 Erroneous or malicious DROP TABLE statements

 Erroneous or malicious update, delete or insert transactions

 Erroneous or malicious batch job or wide-spread

application errors
Configuration before using Flashback Queries

 SQL> show parameter UNDO;


NAME TYPE VALUE
------------------------------------ ----------- ----------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS
 In addition to the above your DBA will have to grant:
 FLASHBACK privilege to the user for all or a subset of
objects
 Execute privileges on the dbms_flashback package.
Types of flashback
 Time based flashback
 SCN(System Change Number) based flashback

 Use “ as of timestamp” clause or


use “ as of SCN “ clause

You might also like