SQL Step-by-Step (EXISTS) and all

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.