MySQL frequently uses queries: group BY, left connection, subquery, having where

Source: Internet
Author: User

A few days ago went to two more cattle internet companies interview. In the SQL this block has encountered a problem, eh. Unfortunately, first of all, a simple comb

Score Table Score



1. GROUP BY use

Group BY one dimension

Like what:

Ask for a total score for each student

SELECT Student,sum (score) from score GROUP by student

Ask for the average score of each student

SELECT Student,avg (score) from score GROUP by student

can also be in accordance with the class, the course to ask


2. The difference between having and where

Having is similar to where, being able to filter the data, how to write the expression after the where, and how to write after having had
    • where for columns in the table play a role. querying data
    • Having the right columns in Query results play a role, Filter Data
Like what:

The students who had two doors and above were found

SELECT student,sum (score<60) as GK from score GROUP by student have gk>1

3, sub-query

(1) Where sub-query

(The result of the inner query is considered as the comparative condition of the outer query)

Students with an average of less than each course

SELECT student, course, score
From Score, (SELECT course as A_COURSE,AVG (score) as A_score from score GROUP by course) as Avg_score
WHERE course = A_course and Score<a_score


Just write it down.

can participate in

Http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html




MySQL frequently uses queries: group BY, left connection, subquery, having where

Related Article

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.