Advanced Query
Advanced Query
Subqueries
Union Operators
DBS/HTS/1314 2
Subquery
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
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
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
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