EXISTS and all
exists uses a subquery as a condition that is true only if the subquery returns rows, and if the subquery does not return any row conditions, it is false. If a customer wants to see a list of all the owners when the store is working on chair, you can use Exsist, as follows:
SELECT Ownerfirstname, Ownerlastname
From Antiqueowners
WHERE EXISTS
(SELECT *
From Antiques
WHERE ITEM = ' Chair ');
If there is a chair in the Antiques column, the subquery returns one or more rows, making the EXISTS clause true, and then having the SQL list the owner. If no search is chair, no rows are returned, and the condition is false.
All is another unusual keyword, because the all query can usually be done in a different way and may be a simpler approach. Let me give you an example:
SELECT Buyerid, ITEM
From Antiques
WHERE Price >= All
(SELECT Price
from antiques);
The above statement will return the item with the highest price and its buyer. The subquery returns all the price columns in the antiques table, the outer query queries the antiques table line-by-row, and if it is greater than the prices in the (or all) column, it is listed, which is the best item. The reason why you must use ">=" is that the highest price item is equal to the highest price in the list because the item is in the Prices column.