Professional Documents
Culture Documents
SQL Quaries
SQL Quaries
SUB QUERIES
A sub query can be defined as a group of nested SELECT statements inside a
SELECT, INSERT, UPDATE or DELETE statement. Sub query can also be used inside
the WHERE or HAVING clauses of the outer SELECT, INSERT, UPDATE or DELETE
statements. SELECT statements containing one or more sub queries are called
nested queries.
The command syntax is:
Sub queries that operate on lists: this type of query returns single-columnmultiple values results and are implemented using the IN clause. The syntax
is as follows:
Subqueries that check for the existence of data: this type of query checks for
the existence of records in a table that are used in the inner query, and
Visit:
WHERE
[NOT] EXISTS (subquery)
SubQueries
With IN
A subquery introduced with IN returns zero or more values. Consider the
following example where all author IDS, from the TITLEAUTHOR table, are displayed
whose books are sold:
SELECT au_id
FROM titleauthor
WHERE title_id IN (SELECT title_id FROM sales)
SQL Server returns a list of all title IDs to the main query then lists all the authors,
whose books are sold, in the result set.
Consider the following example where the server returns a list of publisher IDs to the
main query, and then determines whether each publishers pub_id is in that list:
SELECT publisher=pub_name
FROM publishers
WHERE pub_id IN ( SELECT pub_id FROM titles WHERE type=business)
The inner query is evaluated first and then the result set is sent to the outer query.
Consider another subquery with the IN clause:
SELECT type=type, Average=AVG(ytd_sales)
FROM titles
WHERE type IN (SELECT type FROM titles
WHERE title= the busy executives database guide or title=Is Anger the Enemy?)
GROUUP BY type
The inner query is evaluated first and then the result set is sent to the outer query.
The NOT IN clause is used in the same way as the IN clause. Consider the following
example.
Visit:
1. SELECT pub_name
FROM publishers
WHERE EXISTS (SELECT * FROM titles WHERE type=business)
2. SELECT pub_name
FROM publishers
WHERE EXISTS (SELECT * FROM publishers WHERE City=Paris)
Aggregate functions can also be used in subqueries. Consider the following example
which displays the titles of all those books for which the advance is more than the
average advance of business related books.
SELECT Title=title
FROM titles
WHERE advance>(SELECT AVG (advance) from titles
WHERE type=business)
Subquery Restrictions:
The restrictions imposed are:
The column list of the select statement of a subquery introduced with the
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 the inner query.
The DISTINCT keyword cannot be used with the subqueries that include the
GROUP BY clause.
The ORDER BY clause, GROUP BY clause and INTO keyword cannot be used in
a subquery, because a subquery cannot manipulate its result internally.
Do not specify more than one column name in the subquery introduced with
the EXISTS keyword.
A view created with a subquery cannot be updated.
Visit:
Example
SELECT title_id=title_id, Title=title
FROM title
WHERE prie>ALL(SELECT price
FROM titles
WHERE pub_id=0736)
SELECT title_ID = title_id, Title = title
FROM titles
WHERE price >ANY (SELECT price
FROM titles
WHERE pub_id = `0736`)
Description
Lists all the titles along with their IDs
from the titles table where price is
greater than the maximum price of books
published by the publisher with publisher
ID 0736.
Lists all the titles along with their titles
IDs from the titles table where price is
greater than the minimum price of books
published by the publisher with publisher
ID 0736.
SELECT columns_list
INTO new_table_name
FROM table_names
WHERE conditions
Visit:
UNION
SQL Server provides a unique operator known as the UNION operator that is used
to combine the result set or more queries.
The syntax is:
By default, the result set of the UNION operator removes the duplicate rows from the
queries combined, until an ALL clause is specified with the UNION operator.
The queries combined with the UNION operator must contain an equal number of
columns or expressions, and they must be compatible with each other
There are two tables: region_east and region_west. The region_east table contains
the employee id, name and address of the employees of the eastern region. The
Visit:
emp_add
- - - - - - - - - - -- - - - - - - - 45, Canara Street
12, Blue Lines
98, Civil Lines
The following statement displays employee ids and names of the both the regions:
east and the west.
SELECT emp_id emp_name FROM region_east
Union
SELECT emp_id, emp_name FROM region_west
SELECT Employee ID =emp_id, Employee Name=emp_name FROM region_east
Union
SELECT emp_, emp_name FROM region_west
Rules Regarding the use of the UNION Operator
The restrictions imposed by SQL Server on the use of the UNION
operator are listed below:
Visit:
Visit: