Mysql common query: groupby, left join, subquery, havingwhere_MySQL

Source: Internet
Author: User
Mysql common query: groupby, left join, subquery, havingwhere I went to two relatively popular Internet companies for an interview a few days ago and encountered problems in SQL. well, unfortunately, I 'd like to sort it out first.

Score

1. use group

Group by dimension

For example:

Calculate the total score of each student.

SELECT student, SUM (score) FROM score group by student

Average score for each student

SELECT student, AVG (score) FROM score group by student

You can also follow the class and course

2. differences between having and where: having is similar to where. it can filter data, how to write the expression after where, and how to write the where statement for columns in the table after having, QueryData having plays a role in columns in the query results, FilterData example:

Find out more students

SELECT student, SUM (score <60) as gk FROM score group by student HAVING gk> 1

3. subquery

(1) where subquery

(The inner query result is considered as a comparison condition for the outer query)

Students whose average score is lower than that of 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

Write it here first

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.