Department of Information Technology: S.Y. Btech (It) Sub: Dbms Lab Experiment No: 5 To Study and Implement Subqueries
Department of Information Technology: S.Y. Btech (It) Sub: Dbms Lab Experiment No: 5 To Study and Implement Subqueries
Department of Information Technology: S.Y. Btech (It) Sub: Dbms Lab Experiment No: 5 To Study and Implement Subqueries
Product Table
ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice,
UnitsInStock
SELECT ProductName
FROM Products
WHERE UnitPrice =
(
SELECT UnitPrice
FROM Products
WHERE ProductName = ’Sir Rodney’’s Scones’
)
The embedded SELECT statement first identifies the UnitPrice value for Sir Rodney’s
Scones, which is $10. The $10 value is then used in the outer SELECT statement to return
the product name of all products whose unit price equals $10.
If a table appears only in a subquery and not in the outer query, then columns from that
table cannot be included in the output (the select list of the outer query).
Types of Subqueries
Subqueries can be specified in many places within a SELECT statement. Statements that
include a subquery usually take one of the following formats, however:
WHERE <expression> [NOT] IN (<subquery>)
WHERE <expression> <comparison_operator> [ANY | ALL] (<subquery>)
WHERE [NOT] EXISTS (<subquery>)
1
Department of Information Technology
The result of a subquery introduced with IN (or with NOT IN) is a list of zero or more
values. After the subquery returns the result, the outer query makes use of it. In the
following example, a subquery is nested inside the WHERE clause and the IN keyword is
used:
SELECT Pub_name
FROM Publishers
WHERE Pub_id IN
(
SELECT Pub_id
FROM Titles
WHERE Type = ’business’
)
You can evaluate this statement in two steps. First, the inner query returns the
identification numbers of the publishers that have published business books. Second,
these values are substituted into the outer query, which finds the names that match the
identification numbers in the Publishers table.
Subqueries introduced with the NOT IN keywords also return a list of zero or more
values. The query is exactly the same as the one in subqueries with IN, except that NOT
IN is substituted for IN.
SELECT Title
FROM Titles
WHERE Advance > ANY
(
SELECT Advance
FROM Publishers INNER JOIN Titles
ON Titles.Pub_id = Publishers.Pub_id
2
Department of Information Technology
SELECT Pub_name
FROM Publishers
WHERE EXISTS
(
SELECT * FROM Titles
WHERE Titles.Pub_id = Publishers.Pub_id
AND Type = ’business’
)
To determine the result of this query, consider each publisher’s name in turn. In this case,
the first publisher’s name is Algodata Infosystems, which has identification number
1389. Are there any rows in the Titles table in which Pub_id is 1389 and the type is
business? If so, Algodata Infosystems should be one of the values selected. The same
process is repeated for each of the other publishers’ names.
The NOT EXISTS keywords work like EXISTS, except the WHERE clause in which
NOT EXISTS is used is satisfied if the subquery returns no rows.
3
Department of Information Technology
------------------------------------------------EXERCISE------------------------------------------------------
Implement the SQL statements for the following questions
2. Find out the movie number which has been issued to Allan.
3. Display all the movie title whose price is same as the price of movie ‘The Firm’
4. Find the customer name and area with invoice number ‘I10’.
5. Find the names and movie numbers of all the customers who have been issued a
movie.
8. List the movie number, movie name issued to all the customers.
9. Find the customer names and phone numbers who have been issued movies
before 01-aug-95.
10. Find the type and movie number of movie issued to cust_id ‘A01’ and ‘A02’.