Professional Documents
Culture Documents
6/2/2014
Neotel
Surabhi Baoker
NGM-MEA
surabhi.baoker@tcs.com
Confidentiality Statement
Include the confidentiality statement within the box provided. This has to be legally
approved
Confidentiality and Non-Disclosure Notice
The information contained in this document is confidential and proprietary to TATA
Consultancy Services. This information may not be disclosed, duplicated or used for any
other purposes. The information contained in this document may not be released in
whole or in part outside TCS for any purpose without the express written permission of
TATA Consultancy Services.
2
InternalUse
TableofContent
1.
Introduction .............................................................................................................................................................. 4
1.1Basicfeaturesofsubqueries .................................................................................................................................. 4
2.
Multiplecolumnsubquery ....................................................................................................................................... 5
2.1Pairwisecolumncomparisons................................................................................................................................. 5
2.2Nonpairwisecolumncomparisons ........................................................................................................................ 6
3.Scalarsubqueries ......................................................................................................................................................... 7
4.Correlatedsubqueries.................................................................................................................................................. 8
5.Typesofoperatorsinsubqueries................................................................................................................................. 9
5.1TheEXISTSOperator ............................................................................................................................................... 9
5.2The NOT EXISTS Operator................................................................................................................................ 9
5.3TheWITHclause...................................................................................................................................................... 9
3
InternalUse
1.
Introduction
A sub query is a select statement written in a clause of another SQL statement. The main query within which a sub
query is written is called the parent query.
Sub queries are very useful in case of extracting data from a condition which depends on data present in some
other table or data in itself. It is useful when data to be extracted depends on unknown conditional values.
4
InternalUse
2.
Sub queries can also be used for making column wise comparisons. If desired result is based on two or more
columns and their values, multiple column sub query can be used.
The syntax of the query will be as follows:
SELECT column, column, ...
FROM table
WHERE (column, column, ...) IN
(SELECT column, column, ...
FROM table
WHERE condition);
Example:
SELECT employee_id, manager_id, department_id
FROM employees
WHERE (manager_id, department_id) IN
(SELECT manager_id, department_id
FROM employees
WHERE employee_id IN (178,174))
AND employee_id NOT IN (178,174);
5
InternalUse
6
InternalUse
SELECT
(SELECT MAX(salary) FROM employees) AS Highest_sal,
(SELECT AVG(salary) FROM employess) AS Average_sal
FROM employees
WHERE department_id = 10;
SELECT employee_id, last_name,
(CASE
WHEN department_id =
THEN Canada ELSE USA END) location
FROM employees;
(SELECT department_id FROM departments
WHERE location_id = 1800)
The scalar sub query can be used in most of the queries which are expression (expr) type. However, scalar sub
queries cannot be used in the below conditions:
7
InternalUse
The Oracle Server performs a correlated sub query when the sub query references a column from a table referred
to in the parent statement. A correlated sub query is evaluated once for each row processed by the parent
statement. Correlated sub queries are used for row-by-row processing. Each sub query is executed once for every
row of the outer query.
Correlated Sub query is executed according to the following steps:
8
InternalUse
Sub queries also facilitate use of few operators which help in limiting the search operations in the database. Each
of the operators has a unique style which makes retrieving data easy and also enhances performance.
Few are discussed below.
Example:
9
InternalUse
10
InternalUse
Thank You
Contact
For more information, contact gsl.cdsfiodg@tcs.com (Email Id of ISU)
AboutTataConsultancyServices(TCS)
TataConsultancyServicesisanITservices,consultingandbusinesssolutions
organizationthatdeliversrealresultstoglobalbusiness,ensuringalevelofcertaintyno
otherfirmcanmatch.TCSoffersaconsultingled,integratedportfolioofITandIT
enabledinfrastructure,engineeringandassuranceservices.Thisisdeliveredthroughits
TM
uniqueGlobalNetworkDeliveryModel ,recognizedasthebenchmarkofexcellencein
softwaredevelopment.ApartoftheTataGroup,Indiaslargestindustrialconglomerate,
TCShasaglobalfootprintandislistedontheNationalStockExchangeandBombay
StockExchangeinIndia.
Formoreinformation,visitusatwww.tcs.com.
ITServices
BusinessSolutions
Consulting
All content / information present here is the exclusive property of Tata Consultancy Services Limited (TCS). The content /
information contained here is correct at the time of publishing. No material from here may be copied, modified, reproduced,
republished, uploaded, transmitted, posted or distributed in any form without prior written permission from TCS.
Unauthorized use of the content / information appearing here may violate copyright, trademark and other applicable laws,
and could result in criminal or civil penalties. Copyright 2011 Tata Consultancy Services Limited