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