SQL Step-by-Step (21) More subqueries

Source: Internet
Author: User
Tags sql query

More subqueries

We can use the Select query statement to include a select subquery. For example, first of all, we list all customers who buy valuables, and the standard of valuables is 100 yuan more than the average price of all the items sold. The specific statements are as follows:

SELECT ownerid

From antiques

WHERE price

(SELECT AVG. Price)

From Antiques);

The

Face query statement calculates the average price of an item plus 100 yuan, and searches for all ownerid in the antiques table that is greater than this value. Here you can use the distinct ownerid to eliminate the phenomenon of replication.

The following statement lists all the people who have purchased items in the Antiqueowners table LastName:

SELECT ownerlastname

from antiqueowners

WHERE ownerid =


(SELECT DISTINCT buyerid

from antiques);

This subquery returns a series of customers, and the LastName of the antique owner is displayed only if the ID of the owner of the item appears in the subquery list.

To update This example, let's say that there is a customer who has bought a bookcase and his firstname has made an error in the database. John:

Update antiqueowners

SET Ownerfirstname = ' John '

WHERE ownerid =


(SELECT buyerid

From antiques

where ITEM = ' Bookcase ');

The subquery in the

above statement first searches for the buyerid of the customer who bought the bookcase, and then updates his firstname in the outer query.







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.