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