Data Structures and Algorithms III

Data Structures and Algorithms III

Interview questions

19 questions:

1. Query the gender and course scores of students whose scores in the "Chinese" course are higher than those in the "Mathematics" course

三表联查:
select s.*,sc1.s_score'语文分数',sc2.s_score'数学分数' from score sc1 join score sc2 join student s on sc1.s_id=sc2.s_id and sc1.s_id=s.s_id where sc1.c_id=(select c_id from course where c_name='语文') and
sc2.c_id=(select c_id from course where c_name='数学') and sc1.s_score>sc2.s_score

2. Query the gender and course scores of students whose scores in the "Chinese" course are lower than those in the "Mathematics" course

3. Query the student number, student name and average score of students whose average score is greater than or equal to 60 points

4. Query the student number, name and average score of students whose average score is less than 60 points (including those with and without scores)

求平均分不能使用avg,应该使用总分/总课程数

select s.s_id,s_name,ifnull(sum(s_score),0)/(select count(*) from course) av from student s left join score sc on s.s_id=sc.s_id group by s.s_id having av<60

5. Query the student numbers, student names, total number of courses taken, and total scores of all courses for all students

select s.s_id,s_name,count(c_id)'选课总数',sum(s_score)'总成绩' from student s left join score sc on s.s_id=sc.s_id group by s.s_id

6. Query the number of teachers with the surname "Wang"

select count(*) from teacher where t_name like '王%'

7. Query the information of students who have studied with teacher "Zhang San"

select s.* from teacher t join course c join score sc join student s on t.t_id=c.t_id and c.c_id=sc.c_id and sc.s_id=s.s_id where t_name='张三'

8. Query the information of students who have studied the course numbered "01" and also studied the course numbered "02"

select s.*,sc1.s_score'01',sc2.s_score'02' from student s join score sc1 join score sc2 on sc1.s_id=sc2.s_id and sc1.s_id=s.s_id where sc1.c_id='01' and sc2.c_id='02'

9. Display the grades and average grades of all students in all courses from high to low.

s_id,  语文   数学    英语   平均成绩

select s.s_id,s_name,sum(s_score)/(select count(*) from course) av,
max(case when c_id='01' then s_score end)'语文',
max(case when c_id='02' then s_score end)'数学',
max(case when c_id='03' then s_score end)'英语'
from student s left join score sc on s.s_id=sc.s_id group by s.s_id order by av desc


分组过程中,对遍历到的每条数据,都会进行三次case  when 判断,则一个学生的三门成绩最终以三行存在,而各门课程的成绩应为三行中的最大值,所以应该使用max

Please add image description
10. Query the highest score, lowest score and average score of each subject: displayed in the following form: course ID, course name, highest score, lowest score, average score, passing rate, average rate, excellent rate, excellent rate (passing is > =60, medium: 70-80, excellent: 80-90, excellent: >=90)

select c.c_id,c_name,max(s_score)'最高分',min(s_score)'最低分',avg(s_score),
concat(round(sum(case when s_score>=90 then 1 else 0 end)/count(s_id)*100,2),'%') '优秀率',
concat(round(sum(case when s_score>=80 and s_score<90 then 1 else 0 end)/count(s_id)*100,2),'%') '优良率',
concat(round(sum(case when s_score>=70 and s_score<80 then 1 else 0 end)/count(s_id)*100,2),'%') '中等率',
concat(round(sum(case when s_score>=60 then 1 else 0 end)/count(s_id)*100,2),'%') '及格率'
from course c left join score sc on c.c_id=sc.c_id group by c.c_id

concat('','%')  -- 拼接
保留小数函数:
	round(x,d)  -- 四舍五入,d表示保留几位   x表示小数
	truncate(x,d) -- 截取几位小数  x:小数   d:截取几位小数	

11. Query the ID, name, and grades of students whose scores in each subject are 60 points or above.

1. max(case when)

select s.s_id,s_name,
max(case when c_id=(select c_id from course where c_name='语文') then s_score end) chinese,
max(case when c_id=(select c_id from course where c_name='数学') then s_score end) math,
max(case when c_id=(select c_id from course where c_name='英语') then s_score end)english
from student s join score sc on s.s_id=sc.s_id group by s.s_id having chinese>=60 and math>=60 and english>=60

或:
将成绩表视为3份,分别作为语文成绩表,且分数>=60   数学成绩表,且分数>=60   英语成绩表  且分数>=60

三张成绩表自连接,最后和学生表连接

select  s.s_id,s_name,sc1.s_score'语文',sc2.s_score'数学',sc3.s_score'英语' 
from  student s 
join score sc1 join score sc2 join score sc3 
on sc1.s_id=sc2.s_id and sc2.s_id=sc3.s_id and sc1.s_id=s.s_id 
where sc1.c_id='01' and sc1.s_score>=60 and sc2.c_id='02' and sc2.s_score>=60 and sc3.c_id='03' and sc3.s_score>=60

12. Query the ID, name, and grades of students who have at least one subject with a score of 90 or above.

select s.s_id,s_name,
max(case when c_id='01' then s_score end)'语文',
max(case when c_id='02' then s_score end)'数学',
max(case when c_id='03' then s_score end)'英语'
from student s join score sc on s.s_id=sc.s_id group by sc.s_id having max(s_score)>90

13. Query the ID, name, and total score of students whose scores in all subjects are 240 or above, and sort them in descending order.

select s.s_id,s_name,sum(s_score) sum from student s join score sc on s.s_id=sc.s_id group by s.s_id having sum>=240 order by sum desc

14. Query the student ID and name of the student with the highest score in each subject and write them in a table

create table tname as select....

1. 查询每门科目的最高分
2. 根据科目id和对应的最高分到成绩表中查询对应的学生id
3. 根据学生id到学生表查询学生数据

create table new_table as
select s.s_id,s_name,tmp.c_id,tmp.max from score sc join student s join (select c_id,max(s_score)max from score group by c_id)tmp on sc.c_id=tmp.c_id and sc.s_score=tmp.max and sc.s_id=s.s_id

15. Query the student ID and name of the student with the lowest score in each subject

16. Query the names of teachers who currently have no courses scheduled.

思路:查询老师教授的课程,筛选课程id为null值的数据

select t_name from teacher t left join course c on t.t_id=c.t_id where c_id is null

17. Convert the English name in the previous teacher list to uppercase

转换大小写函数:
	upper(col)    lower(col)
update teacher set t_name=upper(t_name)

18. Query duplicate names in the student table

姓名重复的判断:判断姓名出现的次数,若大于1,则重复
select s_name from student group by s_name having count(s_id)>1

19. Statistics of the number of students in each subject score band: course number, course name, [100-85], [85-70], [70-60], [0-60] and percentage

select c.c_id,c_name,count(s_id),
sum(case when s_score>=85 and s_score<=100 then 1 else 0 end)'[100-85]',
sum(case when s_score>=70 and s_score<85 then 1 else 0 end)'[85-70]',
sum(case when s_score>=60 and s_score<70 then 1 else 0 end)'[70-60]',
sum(case when s_score<60 then 1 else 0 end)'[0-60]',
concat(round(sum(case when s_score>=85 and s_score<=100 then 1 else 0 end)/count(s_id)*100,0),'%') '[100-85]百分比',
concat(round(sum(case when s_score>=70 and s_score<85 then 1 else 0 end)/count(s_id)*100,0),'%') '[85-70]百分比',
concat(round(sum(case when s_score>=60 and s_score<70 then 1 else 0 end)/count(s_id)*100,0),'%') '[70-60]百分比',
concat(round(sum(case when s_score<60 then 1 else 0 end)/count(s_id)*100,0),'%') '[0-60]百分比'
from course c left join score sc on c.c_id=sc.c_id group by c.c_id
Single case – written test
  • Topic: Please write lazy singleton
  • Classification of singletons:
    • Lazy Singleton
    • Hungry Chinese singleton
Lazy singleton:
  • Singleton: There is only one instance of a class in the entire application

    class T{
          
              多例: 多个实例
        	private T(){
          
          }
        	private static T t;
            public static T getInstance(){
          
          
                //判断当前类的实例是否已经存在,若存在,直接返回;若不存在,则创建实例并返回
                if(t==null)
                    t = new T();
                return t;
            } 
    }  
    
    class Test:
    	main:
    		T t = T.getInstance();
    
    
    T类的实例在整个应用程序中只能有一个,则为单例
    
  • How to implement a lazy-style singleton

    核心点:
    	1. 构造方法私有化
    	2. 提供公有的static方法用于向外部提供实例
    		方法内部:
    			判断引用是否为null,null,创建实例并返回;不为null,直接返回实例
    	3. 在成员变量位置声明私有的static的引用(只声明不初始化) -- 懒汉式体现
    	4. 多线程并发访问存在线程安全问题,将方法变为同步的
    	
    public class LazySingleton {
          
          
        private LazySingleton(){
          
          }
        private static LazySingleton lazySingleton;
        public synchronized static LazySingleton getInstance(){
          
          
            if (lazySingleton==null)
                lazySingleton = new LazySingleton();
            return lazySingleton;
        }
    }
    
    Hungry Chinese singleton
    public class HungrySingleton{
          
          
        private HungrySingleton(){
          
          }
        private static HungrySingleton singleton = new HungrySingleton();
        
        public static HungrySingleton getInstance(){
          
          
            return singleton;
        }   
    }
    

I'm a general ; I've always been there. !

Guess you like

Origin blog.csdn.net/letterljhx/article/details/127009807