0% found this document useful (0 votes)
11 views

Advanced Query

Uploaded by

matora
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)
11 views

Advanced Query

Uploaded by

matora
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/ 18

Advanced Query Technique

by: Humasak T.A. Simanjuntak


Contents

 Subqueries
 Union Operators

DBS/HTS/1314 2
Subquery

 Why to Use Subqueries

 To break down a complex query into a series of


logical steps
 To answer a query that relies on the results of an
other query
 Alternative to JOIN method.

The ability to use “a Query within a Query”


or “Nested Query” is the original reason
for the word “Structured” in the name SQL
( Structured Query Language)

DBS/HTS/1314 3
Subquery: Example
USE Northwind
SELECT ProductName
FROM Products
WHERE UnitPrice=
(
SELECT UnitPrice
FROM Products
WHERE ProductName='Sir Rodney''s Scones'
)
DBS/HTS/1314 4
SubQuery: definition and types

 Subquery is defined as a SELECT statement that


returns a single value and is nested inside a
SELECT, INSERT, UPDATE, or DELETE
statements or inside another subquery.
 SubQuery types:
 Subquery with IN (operate on list)
 Subquery with EXISTS (check for the existence of data)
 Nested Subquery (contains one or more subquery)
 Correlated Subquery (depends on the outer query for its
evaluation)

DBS/HTS/1314 5
Subquery restrictions
 The column list of the SELECT statement of a
subquery introduced with comparison operator can
include ONLY one column
 The column used in the WHERE clause of the
outer query should be compatible with the column
used in the SELECT list of inner query.
 The DISTINCT keyword cannot be used with
subqueries that include the GROUP BY clause.
 The ORDER BY clause, the GROUP BY clause,
and the INTO clause cannot be used in a
subquery because a subquery cannot manipulate
its result internally
 A View created with a subquery cannot be updated.

DBS/HTS/1314 6
Subquery with IN
 Returns single-column multiple values results.
 Syntax:
WHERE expression [NOT] IN (subquery)

USE pubs
SELECT pub_name
FROM publishers
WHERE pub_id IN
(SELECT pub_id
FROM titles
WHERE type = 'business')
The Inner query evaluate first and then based on it’s result the outer query will
be executed. i.e., “ Bottom-to-Top ”
DBS/HTS/1314 7
Subquery with EXISTS
 Subquery that check for the existence of data
 This type of query checks for the existence of
records in a table that is used in the inner
query, and returns either a TRUE or FALSE
value based on the existence of data. It is
implemented using the EXISTS keyword.
 The syntax is a as follows:
WHERE [NOT] EXISTS (subquery)

DBS/HTS/1314 8
Subquery with EXISTS: example

 SELECT PubName
FROM Publishers
WHERE EXISTS (
SELECT *
FROM Titles
WHERE Type=‘business’
)

DBS/HTS/1314 9
Nested subquery
 A subquery can contain one or more subqueries.
There is no restriction on the number of subqueries.
 E.g
use pubs
SELECT 'author name'=substring(Au_fname,1,1)+'. ‘ +
Au_Lname
FROM authors
WHERE au_id IN (
SELECT au_id FROM titleauthor
WHERE title_id= (SELECT title_id
FROM titles
WHERE title='Net Etiquette'
)
) DBS/HTS/1314 10
Correlated Subqueries
 A correlated subquery is defined as a query
that depends on the outer query for its
evaluation.
 In correlated subquery, the WHERE clause
reference a table in the FROM clause of the
outer query. The inner query is evaluated for
each row of the table specified in the outer
query.
 SQL server demands alias for the tables if
the correlated subquery is implemented using
the same table.
DBS/HTS/1314 11
Correlated subquery: example

 To find the name of author who earns 100%


royalty
 USE pubs
SELECT au_lname, au_fname
FROM authors
WHERE 100 IN
(SELECT royaltyper
FROM titleauthor
WHERE titleauthor.au_ID = authors.au_id)
DBS/HTS/1314 12
Correlated subquery: example

SELECT a.FirstName, a.LastName


FROM Person.Person AS a WHERE
a.LastName IN (
SELECT a.LastName FROM
HumanResources.Employee AS b
WHERE a.BusinessEntityID =
b.BusinessEntityID AND a.LastName =
'Johnson');

DBS/HTS/1314 13
Queries with Modified Comparison Operator
Operator Description
>ALL Means greater than the maximum value in the list
..column_name>ALL(10,20,30) means ‘greater than 30’
>ANY Means greater than the minimum value in the list
..column_name>ANY(10,20,30) means ‘greater than 10’
=ANY Means any of the value in the list. Same as IN clause
..column_name=ANY(10,20,30) means equal to either 10 or
20 or 30
<>ANY Means not equal to any list
..column_name<>ANY(10,20,30) means not equal to either
10 or 20 or 30
<>ALL Same as <>ANY
DBS/HTS/1314 14
Query with modified comparison
operator: example
USE pubs
SELECT Title
FROM Titles
WHERE Advance > ANY
(
SELECT Advance
FROM Publishers INNER JOIN Titles
ON Publishers.Pub_Id=Titles.Pub_Id
AND Pub_name=‘Algodata Infosystem’
)

DBS/HTS/1314 15
UNION operator

 A unique operator that is used to combine the


result set of two or more queries

DBS/HTS/1314 16
Union operator: syntax
 SELECT colum_list [INTO new_table_name]
FROM clause
[WHERE clause]
[GROUP BY clause]
[HAVING clause]
UNION[ALL]
SELECT colum_list [INTO new_table_name]
FROM clause
[WHERE clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause]
[COMPUTE clause]

DBS/HTS/1314 17
Union operator: example

use pubs
SELECT Au_fname,City,State
FROM Authors
WHERE State=‘CA’
UNION
SELECT Pub_name,City,State
FROM Publishers

DBS/HTS/1314 18

You might also like