Ali interviewer: how to query database SQL

Look at the next database interview questions

Interview questions:

Their scores are stored in the table below. Now you need to query the highest-scoring student in each class and display the name of that student!

Database SQL classic interview questions-database query-subquery application one

Attached tables and initialization data

- 1: Create table CREATE TABLE tb_lemon_grade (

Analysis topic:

Each row of records in the data table saves the name of each student, each subject score and class. Now the need is to find the highest score of each class, so it must be grouped by class (GROUP BY), the highest score For filtering, use the aggregate function MAX()? We say that MAX() is the maximum value in the vertical direction, but now it is necessary to require the highest score, which means that the subjects of the same student must also be compared. The red and bold scores in the above figure are the highest scores of each class. This is By checking the results of mental arithmetic comparison with the naked eye, our mental arithmetic process is very simple: find all the results of this class, and then compare the scores to find the largest score (the duplicate is counted, which is equivalent to tied for the first place). Therefore, a horizontal comparison is also needed! Here is a function: GREATEST(). Look at the official help document. The following syntax description and examples tell us that the GREATEST() function can return the maximum value among multiple parameters:

Database SQL classic interview questions-database query-subquery application one

Now start to write SQL:

The first step: first find out the highest score of each class

First group, and then perform MAX() longitudinal comparison to get the maximum score of each subject, and then pass GREATEST() horizontal comparison to get the highest score of the class! SELECT class_name, GREATEST( MAX(Linux), MAX(MySQL), MAX(Java)) FROM tb_lemon_grade GROUP BY class_name;

Database SQL classic interview questions-database query-subquery application one

Step 2: How to find the highest score in each class?

In the first step, we have got the highest score of each class. Note that this highest score cannot be determined by a certain classmate, because there may be multiple classmates who have the highest scores (for example, Chen Er and Zhang San in class 18, both There is a subject with the highest score of 90 points) The process of selecting the highest score through natural thinking: I know that the highest score of the class is 90, and then I will get this score and the subjects of each classmate in this class Take notes on the score. If a student has a course with a score of 90, that’s the student we are looking for! Is it easy to understand? So, we first find the highest score of each student

Step 3: Find the highest score of each student (to get the highest score of each subject of the student horizontally)

SELECT *, GREATEST(Linux, MySQL, Java) maxScore

Database SQL classic interview questions-database query-subquery application one

The fourth step: the result set of the two sub-queries associated query!

Through two sub-queries, one is the highest score of each class, and the other is the information and highest score of each student!

SELECT t2.class_name, t2.student_name, t1.maxScore

Database SQL classic interview questions-database query-subquery application one

Let me share some of my favorite information, I hope it can help everyone

This information is organized around [software testing]. The main content includes: python automated testing exclusive video, Python automated detailed information, a full set of interview questions and other knowledge content. For friends of software testing, it should be the most comprehensive and complete preparation warehouse. This warehouse has accompanied me through a lot of bumpy roads, and I hope it can also help you. Pay attention to WeChat public account: Programmer Erhei, you can get it directly
 

Guess you like

Origin blog.csdn.net/m0_52668874/article/details/114950038