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

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. I encountered a problem in SQL, first, let's take a brief look at score1 and groupby in the two-dimensional table for grouping by a certain dimension. For example: calculate the total score of each student, SELECTstudent, SUM (

Mysql commonly used query: group by, left join, subquery, having where I went to two relatively popular Internet companies for an interview a few days ago and encountered problems in SQL, first, let's take a look at the summary table score 1. group by is grouped by a certain dimension. For example, calculate the total score of each student, SELECT student, SUM (

Mysql common query: group by, left join, subquery, having where

I went to an interview with two cool Internet companies a few days ago and encountered problems in SQL. Well, unfortunately, let's take a look at it.

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 where expression and how to write it after having?
  • Where is used for columns in a table,QueryData
  • Having plays a role in columns in the query results,FilterData
For 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

Refer

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


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.