48 exercises SQL (Oracle + MySQL)

Exercise mainly involves four tables, are as follows:


student (sid, sname, sage, ssex) student table 

course (cid, cname, tid) curriculum 

score (sid, cid, score) the results table 

teacher (tid, tname) teachers table


Firstly, the table structure


CREATE TABLE student 

  ( 

     sid    INT, 

     sname varcha, 

     sage  INT, 

     ssex  varchar (8) 

  ); 

 

CREATE TABLE course 

  ( 

     cid    INT, 

     cname varcha 

     time INT 

  ); 

 

CREATE TABLE score 

  ( 

     sid    INT, 

     cid    INT, 

     score INT 

  ); 

 

CREATE TABLE teacher 

  ( 

     time INT, 

     tname varchar(16) 

  );

Insert data


--mysql、oracle

insert into student select 1,'刘一',18,'男' FROM dual union all

 select 2, 'money two', 19, 'female' FROM dual union all

 select 3, 'John Doe', 17, 'M' FROM dual union all

 select 4, 'John Doe', 18, 'M' FROM dual union all

 select 5, 'Wang Wu', 17, 'M' FROM dual union all

 select 6, 'Zhao six', 19, 'M' FROM dual; 

 

 insert into teacher select 1,'叶平' FROM dual union all

 select 2,'贺高' FROM dual union all

 select 3,'杨艳' FROM dual union all

 select 4,'周磊' FROM dual;

 

 insert into course select 1,'语文',1 FROM dual union all

 select 2,'数学',2 FROM dual union all

 select 3,'英语',3 FROM dual union all

 select 4,'物理',4 FROM dual;

 

 insert into score

 select 1,1,56 FROM dual union all 

 select 1,2,78 FROM dual union all 

 select 1,3,67 FROM dual union all 

 select 1,4,58 FROM dual union all 

 select 2,1,79 FROM dual union all 

 select 2,2,81 FROM dual union all 

 select 2,3,ROM dual union all 

 select 2,4,68 FROM dual union all 

 select 3,1,91 FROM dual union all 

 select 3,2,47 FROM dual union all 

 select 3,3,88 FROM dual union all 

 select 3,4,56 FROM dual union all 

 select 4,2,88 FROM dual union all 

 select 4,3,90 FROM dual union all 

 select 4,4,93 FROM dual union all 

 select 5,1,46 FROM dual union all 

 select 5,3,78 FROM dual union all 

 select 5,4,53 FROM dual union all 

 select 6,1,35 FROM dual union all 

 select 6,2,68 FROM dual union all 

 select 6,4,71 FROM dual;

 

 

 

--mysql

insert into student values ​​(1, 'Liu', 18, 'M'),

 (2 'money two', 19, 'female'),

 (3, 'John Doe', 17, 'M'),

 (4, 'John Doe', 18, 'M'),

 (5, 'King five', 17, 'man'),

 (6 'Zhao six', 19, 'F'); 

 

 insert into teacher values (1,'叶平'),

 (2, 'High He'),

 (3, 'Yang Yan'),

 (4, 'Zhou Lei');

 

 insert into course values (1,'语文',1),

 (2, 'mathematics', 2),

 (3, 'English', 3),

 (4, 'physical', 4);

 

 insert into score values

 (1,1,56), 

 (1,2,78), 

 (1,3,67), 

 (1,4,58), 

 (2,1,79), 

 (2,2,81), 

 (2,3,92), 

 (2,4,68), 

 (3,1,91), 

 (3,2,47), 

 (3,3,88), 

 (3,4,56), 

 (4,2,88), 

 (4,3,90), 

 (4,4,93), 

 (5,1,46), 

 (5,3,78), 

 (5,4,53), 

 (6,1,35), 

 (6,2,68), 

 (6,4,71);

Exercises Beginning


1. Query "001" Course higher than the "002" number of grades for all students to learn


select a.sid from (select sid,score from score where cid=001) a,(select sid,score from score where cid=002) b where a.sid=b.sid and a.score>b.score;

2. Query average score greater than 60 points and the number of students in the school grade point average


select sid,avg(ifnull(score,0)) from score group by sid having avg(score)>60;

 

- added: there may be a null value in MySQL how to query the field, you can do some processing

--1.isnull (exper) determines whether exper is empty, it returns 1, otherwise 0

--2.ifnull (exper1, exper2) determines whether exper1 is empty, it is replaced by exper2

--3.nullif (exper1, exper2) if expr1 = expr2 is true, then the return value is NULL, otherwise it returns the value of expr1.

3. All the students query number, name, number of elective total score


select t1.sid,t1.sname,count(t2.cid),sum(t2.score) from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname;

4. Query teacher surnamed "Li" in the number of


select count(distinct tname) from teacher where tname like '李%';

or

select count(distinct tname) from teacher where substr(tname,0,1)='李';

5. Query never learned "flat leaf" class teacher of students school, name


select sid,sname from student where sid not in 

(select distinct t1.sid from score t1 left join course t2 on t1.cid=t2.cid where t2.tid=(select tid from teacher where tname='叶平'));

6. Query learned "001" and I have learned a number "002" course students of school, name


select sid,sname from student where sid in (select sid from score where cid=001 or cid=002 group by sid having count(cid)>1);

or

select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid where t2.cid=001 and exists (select * from score t3 where t2.sid=t3.sid and t3.cid=002);

7. Query learned all the lessons, "Ye Ping" teachers teach the students the school, name


select sid,sname from student where sid in 

(select sid from score t1,course t2,teacher t3 where t1.cid=t2.cid and t2.tid=t3.tid and t3.tname='叶平' group by t1.sid having count(distinct t2.cid)=

(select count(distinct course.cid) from course left join teacher on course.tid=teacher.tid where teacher.tname='叶平'));

8. All students about courses number "002" results "001" number is lower than the course curriculum of school, name


SELECT a.sid,a.sname from 

(select t1.sid,t1.sname,t2.score from student t1,score t2 where t1.sid=t2.sid and t2.cid=001) a,

(select t1.sid,t1.sname,t2.score from student t1,score t2 where t1.sid=t2.sid and t2.cid=002) b

where a.sid=b.sid and b.score < a.score;

or

select sid,sname from (

select t1.sid,t1.sname,t2.score,(select t3.score from score t3 where t1.sid=t3.sid and t3.cid=002) score2

from student t1,score t2 where t1.sid=t2.sid and t2.cid=001) where score2 < score;

9. (1) queries all course grades are less than 60 minutes the students school, name


select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having max(t2.score)<60;

(2) Search course, there are scores less than 60 points the students of school, name


select sid,sname from student where sid not in (select t1.sid from student t1 left join score t2 on t1.sid=t2.sid and t2.score>60);

10. The inquiry did not learn the full lesson of all students of school, name


select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having count(t2.cid)<(select count(cid) from course);

11. The query has at least the same number of students to learn a lesson and learn the names and numbers "001," the students have learned


select distinct t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid where t2.cid in (select distinct cid from score where sid=001) and t1.sid <>001;

12. The "score" table "Ye Ping" teacher of the class grades are changed course grade point average for this purpose 


update score set score=(select avg(score) from score where cid=

(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平')) where cid=

(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平');

13. Queries and "002" number of students learning the same course with other students to learn numbers and names; 


SELECT t1.sid,t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid in 

(select distinct cid from score where sid=002) and t1.sid != 002 group by t1.sid,t1.sname 

having count(distinct t2.cid) = (select count(distinct cid) from score where sid=002);

14. Delete study "flat leaf" class teacher SC table records; 


delete from score where cid in (select t1.cid from course t1 left join teacher t2 on t1.tid=t2.tid where t2.tname='叶平'); 

15. Insert Table to score some records that meet the following requirements: None went to number "003" course students learn numbers, the average score of the 2nd class 


insert into score(sid,cid,score) select sid,'002',(select avg(score) from score where cid='002') from student 

where sid not in (select sid from score where cid='003');

16. The average score for all students in descending show "language", "Mathematics", "English", "physical" four course grade, displayed as follows: Student ID, language, mathematics, English, physics, the effective number of courses, the effective average 


select t.sid student ID,

(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='语文') 语文,

(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='数学') 数学,

(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='英语') 英语,

(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='物理') 物理,

count (*) number of effective curriculum, avg (t.score) Average effective from score t group by t.sid order by avg (t.score) desc;

17. Inquiries about the highest and lowest points all subjects: the display in the following form: Course ID, highest score, lowest score 


select cid,max(score),min(score) from score group by cid;

18. The average scores by the subjects and from low to high pass rates in descending order of the percentage of 


select t.cid,avg(t.score),100*((select count(*) from score t1 where t1.cid=t.cid and score >= 60)/

(select count(*) from score t2 where t.cid=t2.cid)) 及格率 from score t 

group by t.cid order by avg (t.score) asc, desc passing rate;

or

select t.cid,avg(t.score),100*(sum(case when score >= 60 then 1 else 0 end)/count(*)) 及格率 from score t 

group by t.cid order by avg (t.score) asc, desc passing rate;

19. The details of the curriculum and grade point average passing rate as a percentage (with a "1 line" display): Language 001, Mathematics 002, English 003, Physics 004


select (select avg(score) from score where cid=001) avg1,

100*((select count(*) from score where cid=001 and score >= 60)/(select count(*) from score where cid=001)) pass1,

(select avg(score) from score where cid=002) avg2,

100*((select count(*) from score where cid=002 and score >= 60)/(select count(*) from score where cid=002)) pass2,

(select avg(score) from score where cid=003) avg3,

100*((select count(*) from score where cid=003 and score >= 60)/(select count(*) from score where cid=003)) pass3,

(select avg(score) from score where cid=004) avg4,

100*((select count(*) from score where cid=004 and score >= 60)/(select count(*) from score where cid=004)) pass4 from dual;

or

select sum(case when cid=001 then score else 0 end)/sum(case when cid=001 then 1 else 0 end) avg1,

100*(sum(case when score >= 60 and cid=001 then 1 else 0 end)/sum(case when cid=001 then 1 else 0 end)) pass1,

sum(case when cid=002 then score else 0 end)/sum(case when cid=002 then 1 else 0 end) avg2,

100*(sum(case when score >= 60 and cid=002 then 1 else 0 end)/sum(case when cid=002 then 1 else 0 end)) pass2,

sum(case when cid=003 then score else 0 end)/sum(case when cid=003 then 1 else 0 end) avg2,

100*(sum(case when score >= 60 and cid=003 then 1 else 0 end)/sum(case when cid=003 then 1 else 0 end)) pass3,

sum(case when cid=004 then score else 0 end)/sum(case when cid=004 then 1 else 0 end) avg2,

100*(sum(case when score >= 60 and cid=004 then 1 else 0 end)/sum(case when cid=004 then 1 else 0 end)) pass4 from score;

20. The queries are different teachers teach different courses to low average display 


select t2.tid,t2.tname,t1.cid,t1.cname,avg(t3.score) from course t1,teacher t2,score t3 

where t1.tid=t2.tid and t1.cid=t3.cid group by t2.tid,t2.tname,t1.cid,t1.cname order by avg(t3.score) desc;

21. The inquiry follows the course grade to students in the first three transcripts of six: the language (001), mathematics (002), English (003), physics (004) 

    [Student ID], [student's name], language, mathematics, English, physics, grade point average 


--mysql

select t1.sid,t1.sname,

(select a.score from score a where a.sid=t1.sid and a.cid=001) score1,

(select b.score from score b where b.sid=t1.sid and b.cid=002) score2,

(select c.score from score c where c.sid=t1.sid and c.cid=003) score3,

(select d.score from score d where d.sid=t1.sid and d.cid=004) score4,

(select avg(e.score) from score e where e.sid=t1.sid and e.cid in (001,002,003,004)) avg_all

from student t1 order by avg_all desc limit 2,4;

 

--oracle

select sid,sname,score1,score2,score3,score4,avg_all from (

select rownum n,t.* from (

select t1.sid,t1.sname,

(select a.score from score a where a.sid=t1.sid and a.cid=001) score1,

(select b.score from score b where b.sid=t1.sid and b.cid=002) score2,

(select c.score from score c where c.sid=t1.sid and c.cid=003) score3,

(select d.score from score d where d.sid=t1.sid and d.cid=004) score4,

(select avg(e.score) from score e where e.sid=t1.sid and e.cid in (001,002,003,004)) avg_all

from student t1 order by avg_all desc) t ) where n between 3 and 6;

22. statistics from all subjects, the number of points for each segment: course ID, course name, [100-85], [85-70], [70-60], [<60]


select t2.cid course ID, t2.cname course name, sum (case when t1.score> 85 then 1 else 0 end) '[100-85]',

sum(case when t1.score between 70 and 85 then 1 else 0 end) '[85-70]',

sum(case when t1.score between 60 and 70 then 1 else 0 end) '[70-60]',

sum(case when t1.score <60  then 1 else 0 end) '[ <60]' from score t1,course t2 

where t1.cid=t2.cid group by t2.cid,t2.cname;

23. student grade point average and ranking query


select 1+(select count(distinct avg_score) from 

(select avg(score) avg_score from score group by sid) t1 where t1.avg_score > t2.avg_score2) 名次,

t2.sid,t2.avg_score2 from 

(select sid,avg(score) avg_score2 from score group by sid) t2 order by t2.avg_score2 desc;

Search record 24. The top three all subjects without considering :( tied for the situation)


- Note: MySQL can not use the top N, limit can not be directly used in / any / all subqueries, etc.

select t1.sid,t1.cid,t1.score from score t1 where exists  

(select count(1) from score where t1.cid=score.cid and t1.score < score having count(1) < 3) 

order by t1.cid,t1.score desc;

or

select t1.sid,t1.cid,t1.score from score t1 where  

(select count(sid) from score where t1.cid=score.cid and t1.score < score) < 3

order by t1.cid,t1.score desc;

25. The number of queries for each course is elective students


select cid,count(sid) from score group by cid;

26. The only check out one of the elective courses of all students in school number and name


select sid,sname from student where sid in 

(select sid from score group by sid having count(cid) = 1);

27. queries male and female persons


select ssex,count(sid) from student group by ssex;

28. Queries surname "Chang" in the list of students


select sid,sname from student where sname like '张%';

or

select sid,sname from student where substr(sname,1,1)='张';

- Note that () function is calculated from the beginning substr 1 in MySQL, Oracle and is zero

29. queries the same name list of students, the number of the same name and statistics


select sname,count(sid) from student group by sname having count(sid)>1;

30 list of students born in 2001. (Note: the type of student Sage column table is int)


select sid,sname from student where year(now())-2001+1=sage;

31. Queries grade point average in each course, results in ascending order according to grade point average, grade point average is the same, according to the course numbers in descending order


select cid,avg(score) from score group by cid order by avg(score),cid desc;

32. All students grade point average greater than 85 inquiries number, name and grade point average


select t1.sid,t1.sname,avg(t2.score) from student t1,score t2 

where t1.sid=t2.sid group by t1.sid having avg(t2.score) >85;

33. Course name query is "physical" and scores below 60 student names and scores


select t1.sname,ifnull(t2.score,0) from student t1,score t2,course t3 where t1.sid=t2.sid and t2.cid=t3.cid

and t3.cname='物理' and t2.score < 60;

34. Query all students Elective


select t1.sid,t1.sname,t3.cid,t3.cname from student t1,score t2,course t3 

where t1.sid=t2.sid and t2.cid=t3.cid;

35. Any inquiries in the name of a course grade of 70 points or more, the course name and scores


select t1.sname,t3.cname,t2.score from student t1,score t2,course t3

where t1.sid=t2.sid and t2.cid=t3.cid and t2.score >= 70;

36. The inquiry failed courses, course number and press descending order

https://www.xiaohongshu.com/discovery/item/5d7490d300000000020192bc

select cid from score where score < 60 order by cid desc;

37. Queries courses numbered 003 students and course grade school number and names of 80 points or more


select t1.sid,t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid=003 and t2.score >= 80;

38. In the course of seeking election the number of students


select count(t.sid) from (

select sid from score group by sid having count(cid) > 0) t;

or

select count(distinct sid) from score;

39. Queries student elective, "Ye Ping" teacher grant program, the highest-achieving students names and scores

Zhengzhou infertility hospital: http: //www.zzchyy110.com/

select t1.sname,max(t2.score) from student t1,score t2,course t3,teacher t4 

where t1.sid=t2.sid and t2.cid=t3.cid and t3.tid=t4.tid and t4.tname='叶平';

40. The queries are different grades of students in the same course the student number, course number, student achievement


select t1.sid,t1.cid,t1.score,t2.sid,t2.cid,t2.score from score t1,score t2 

where t1.cid <> t2.cid and t1.score=t2.score; 

41. The top two in each door inquiry best work results


- similar to the 24 questions

select t1.cid,t1.sid,t1.score from score t1 where 

(select count(t2.sid) from score t2 where t1.cid=t2.cid and t1.score < t2.score) < 2 

order by t1.cid,t1.score desc;

42. statistics for each course elective number of students (more than 4 courses only statistics). Requirements output number and the number of elective courses, according to the number of query results in descending order, if the same number of people, according to the course numbers in ascending order


select cid,count(sid) from score group by cid having count(sid) > 4 

order by count(sid) desc,cid asc;

43. retrieve at least two courses of elective student number


select sid from score group by sid having count(cid) >=2;

44. The details of the curriculum of all students of the elective course number and course name


select t2.cid,t2.cname from score t1,course t2 

where t1.cid=t2.cid group by t2.cid,t2.cname having count(t1.sid)=(select count(*) from student);

45. Queries never learned, "Ye Ping" the name of any student in a course taught by a teacher


select t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid in 

(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平');

46. ​​Student ID query failed two or more courses of the students and their grade point average


select sid,avg(ifnull(score,0)) from score where sid in (

select sid from score where score < 60 group by sid having count(cid) > 1) group by sid;

Students learn to retrieve No. 47. "004" course score less than 60, in descending order by score


select sid from score where cid=004 and score < 60 order by score desc;

"001" Course 48. Delete "002" student achievement


delete from score where sid=002 and cid=001;

The code above is his knock, if inappropriate expression or better, the trouble message, learn together, thank you!



Guess you like

Origin blog.51cto.com/14510269/2437738