SQL subquery and group by Query

Source: Internet
Author: User

1. subquery in SQL:

When we are doing SQL questions, we are most afraid of complex query statements, because most of the time we need two or three subqueries for implementation,

Most of the students are afraid of subqueries. I was confused by these questions,

However, after doing a lot of such questions, we found that there are also some rules. We can sum up a set of methods suitable for ourselves to deal with them,

The following is my solution to this type of question:

A shopping table is as follows:

Question: query the information of a shopper who has more than one type of shopping item.

 

I split the question as follows:

 

Two query statements can be obtained:

 

<1> query the shopping information of a shopper.

 

Select * from shopping info table where shopper in ......

 

<2> query shoppers with more than one type of purchased item.

 

Select shopper from shopping info table group by shopper having count (*)> = 2

 

After the statement <2> is added to the statement <1>, a correct answer is obtained.

 

Analyze the statements from the external, take out simple query statements, and then combine them.

Of course, my question here is that my temporary thinking may be relatively simple. I have seen such a statement.

Query ...... ...... ...... ...... ....... It can be said that there may be two or three or more subqueries, and a good method is required.

2. Grouping query in SQL (usually used together with Aggregate functions ):

Student Information table:

 

Question: 1. query the average score of each grade and class;

The average score of the query in article 2 is greater than 580.

1 solution: Select grade, AVG (total score) as "average score"

From student information table

Group by grade

Illustration:

 

 

 

Select AVG (total score) as "average score" from student info table

 

 

Select AVG (total score) as "average score" from student info table

Solution: Select grade, class, AVG (total score) as "average score"

From student information table

Group by grade, Class

Select AVG (total score) as "average score" from student info table

 

Select AVG (total score) as "average score" from student info table

 

Select AVG (total score) as "average score" from student info table

 

Select AVG (total score) as "average score" from student info table

2 solution:

 

Select grade, class, AVG (total score) as "average score"

From student information table

Group by grade, Class

Having average score> 580

 

Having is equivalent to where, having is used for the condition after group by, and where is used for the condition before group.

 

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.