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