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 <a_score
Write it here first
Refer
Http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html