SQL语句的经典语句50例--MySQL版本的答案

--table Student
CREATETABLE Student(
SVARCHAR(10),
SnameVARCHAR(10),
SageDATE,
SsexVARCHAR(10));
--tableStudent: insert rows
INSERTINTO Student (S, Sname, Sage, Ssex) VALUES ('01', 'frank1', '1991-01-01','Male');
INSERTINTO Student VALUES ('02', 'frank2', '1992-07-07', 'Female');
INSERTINTO Student VALUES ('03', 'frank3', '1993-07-07', 'Male');
INSERTINTO Student VALUES ('04', 'frank4', '1994-07-07', 'Male');


--table Course

CREATETABLE Course(
CVARCHAR(10),
CnameVARCHAR(10),
TVARCHAR(10));
--tableCourse: insert rows
INSERTINTO Course VALUES ('01', 'Chinese', '01');
INSERTINTO Course VALUES ('02', 'Math', '02');
INSERTINTO Course VALUES ('03', 'English', 'TeacherC');
UPDATECourse SET T = '03' WHERE Cname = 'English';


--table Teacher
CREATETABLE Teacher(
TVARCHAR(10),
TnameVARCHAR(10));
--tableTeacher: insert rows
INSERTINTO Teacher VALUES ('01', 'TeacherA');
INSERTINTO Teacher VALUES ('02', 'TeacherB');
INSERTINTO Teacher VALUES ('03', 'TeacherC');




--table SC
CREATETABLE SC(
SVARCHAR(10),
CVARCHAR(10),
scoreINT);
--tableSC: insert rows
INSERTINTO SC VALUES ('01', '01', 81);
INSERTINTO SC VALUES ('01', '02', 41);
INSERTINTO SC VALUES ('01', '03', 71);
INSERTINTO SC VALUES ('02', '01', 82);
INSERTINTO SC VALUES ('02', '02', 42);
INSERTINTO SC VALUES ('02', '03', 72);
INSERTINTO SC VALUES ('03', '01', 83);
INSERTINTO SC VALUES ('03', '02', 73);
INSERTINTO SC VALUES ('03', '03', 63);
INSERTINTO SC VALUES ('04', '01', 81);


--1 student and score whose SC 01 > SC 02 
--Onetable can be as two tables      ||              IFNULL
Selecta.*, b.score, c.score from Student a , SC b, SC c where a.S = b.S and c.S = b.Sand b.C = '01' and c.C = '02' and b.score > IFNULL(c.score, 0); 


--3 student whose average score >=60 
--Groupby + Having AGV_function || CAST ( expression AS data_type)
Selecta.*, AVG(b.score) from Student a , SC b where a.S = b.S GROUP BY a.S HAVINGAVG(b.score)>60; 
Selecta.*, cast(avg(b.score) as decimal(18,2)) from Student a , SC b where a.S = b.SGROUP BY a.S HAVING cast(avg(b.score) as decimal(18,2)) < 60 ; 


--5 student and all course and all course
Selecta.*, COUNT(b.C), SUM(b.score) from Student a , SC b where a.S = b.S GROUP BYa.S order by a.S; 
Selecta.*, b.C, SUM(b.score) from Student a , SC b where a.S = b.S GROUP BY a.S, b.C; 


--6 teacher whose last name = "A"
SelectTname from Teacher where Tname LIKE '%A';
SelectCount(Tname) from Teacher where Right(Tname, 1)='A';


--7 students whose teacher is TeacherB
SelectStudent.* from Student, SC, Teacher, Course where Student.S = SC.S and Course.T= Teacher.Tname and Course.C = SC.C and Teacher.Tname = 'TeacherB';


--8 students whose teacher not include TeacherB
--Notuse like 7 (not Teacher.Tname = 'TeacherB'), but nest the statement 
SelectStudent.* from Student where S NOT IN (Select Student.S from Student, SC,Teacher, Course where Student.S = SC.S and Course.T = Teacher.T and Course.C =SC.C and Teacher.Tname = 'TeacherB');
--Ornest SQL no need Student table
SelectStudent.* from Student where Student.S not in (Select distinct SC.S from SC,Course, Teacher where SC.C = Course.C and Teacher.T = Course.T andTeacher.Tname="TeacherB");


--10 student learn course='01' but Not course '02'
--student have course '01' and he is not in the group course '02'
SelectStudent.* ,SC.C from Student, SC where Student.S = SC.S and SC.C = '01' andStudent.S Not In (Select SC.S from SC where SC.C = '02');
--OrExit function
SelectStudent.* ,SC.C from Student, SC where Student.S = SC.S and SC.C = '01' and NotEXISTS (Select SC.S from SC where SC.C = '02');


--11 list all student not join all courses       
--Havingis based on Group, so be careful of group argument
SelectDistinct Student.* from Student, SC where Student.S = SC.S group by SC.S havingCount(SC.C) < (select Count(1) from course);


--12 students whose course in the course of "01" 
SelectDistinct Student.* from Student, SC where Student.S = SC.S and Student.S<> '01' and SC.C in (Select C from SC where S='01'); 


--13 students whose course as same as course of "01" 
--SelectCount(C) is right one! && Count (Select C) is wrong!
SelectDistinct Student.* from Student, SC where Student.S = SC.S and Student.S<> '01' and SC.C in (Select C from SC where S='01') Group by SC.C HavingCount(SC.C) = (Select Count(C) from SC where S='01') ; 


SelectDistinct Student.* from Student, SC where Student.S = SC.S and Student.S<> '01' and SC.C in (Select C from SC where S='01') and Count(SC.C) =(Select Count(C) from SC where S='01') Group by SC.C; --Wrong!!!


--15 student (and average score) whose at leasttwo course < 60
SelectStudent.S, Student.Sname, Avg(SC.Score) from Student, SC where  (SC.S =Student.S) and (Student.S in (Select Distinct S from SC where SC.Score < 60group by SC.S Having Count(SC.C)>0)) Group by SC.S;


--16 student whose course "01"< 60
SelectDistinct Student.S, Student.Sname from Student, SC where (SC.Score < 60) and(SC.score < 60) and (Student.S = SC.S)Order by Student.S;


--17 list all students' average score
Select Student.Sname, Avg(SC.Score) from Student, SC where Student.S = SC.SGroup by SC.S Order by Avg(SC.Score) DESC;


--18,23:18 list all course' max/min/avg andperformance(underQualify:0-59; qualify:59-79; Good:80-100)
SelectCourse.C, Course.Cname, Max(SC.Score), Min(SC.Score), Avg(SC.Score), 
((SelectCount(SC.Score) from SC,Course where SC.Score < 60 and SC.C =Course.C)/(Select Count(SC.Score) from SC,Course where SC.C = Course.C)*100) ASunderQualifyRate,  
((SelectCount(SC.Score) from SC,Course where SC.Score > 59 and SC.Score <79 andSC.C = Course.C )/(Select Count(SC.Score) from SC,Course where SC.C =Course.C)*100) As qualifyRate,  
((SelectCount(SC.Score) from SC,Course where SC.Score > 80 and SC.C =Course.C)/(Select Count(SC.Score) from SC,Course )*100 where SC.C = Course.C)As Good  
from Course, SC where SC.C = Course.C Group by Course.C;
 
--21 list all average score for all course
SelectTeacher.*, avg(SC.score) from Teacher, Course, SC where Teacher.T = Course.Tand Course.C = SC.C group by Teacher.T Order by avg(SC.score) DESC;


--22 list 2nd & 3rd student for all courses
SelectStudent. (Select Count(1) from SC where SC.Score > ) as place from Student,SC as 
selecta,b,rank from 
   (select ff.Score,if(@pa=ff.Score,@rank:=@rank+1,@rank:=1) asrank,@pa:=ff.Score 
   FROM
   (select Score from SC group by C order by Score desc) ff,(select@rank:=0,@pa=null) tt) result
havingrank <=2;
               


--24 rank student average score
SelectSC.S, SC_B.AvgScore, (@rank:=@rank+1) as rank From SC, (Select S, avg(Score) asAvgScore from SC group by C Order by avg(Score) DESC) as SC_B ,(select@rank:=0) as tt where SC.S = SC_B.S;
--Howto realized function "rank", there (1) & (2)
--(1)usePL SQL
selecta,b,rank from (select ff.a,ff.b,if(@pa=ff.a,@rank:=@rank+1,@rank:=1) asrank,@pa:=ff.a FROM (select a,b from ranktbl group by a,b order by a asc,bdesc) ff,(select @rank:=0,@pa=null) tt) result having rank <=2;
--(2)createvirtual table, count  
SELECTa, b, (SELECT COUNT(distinct b) FROM ranktbl where grp =ranktbl.grp and b



--28 count for male & female
--Createvirtual table  || sum-if condition || Case-when 
Selectm.ms, f.fs from (Select Count(S) as ms from Student where Ssex='Male') as m,(Select Count(S) as fs from Student where Ssex='Female') as f;
SelectSum(if ((Ssex='Male'), 1, 0)) as ms, Sum(if( (Ssex='Female'), 1, 0)) as fs fromStudent;
Selectcount(S), Ssex from Student group by Ssex;
SelectCount(case Ssex when 'Male' then 'Male' end) as 'Male', Count(case Ssex when'Female' then 'Female' end) as 'Female' from Student;


--29 Student whose name contains "frank"
--Locate(findStr, OriginalStr)
Select* from Student where Sname Like '%frank%';
Select* from Student where LOCATE('frank', Sname) >0 ;


--25-27,30-38: 33 students whose average score> 70
SelectStudent.*, Avg(SC.Score) from Student, SC where Student.S = SC.S group by Shaving Avg(SC.Score)>70; 


--40,42:40 top student with score under teacherTeacherB
--onlyone top student
SelectSC.Score, Student.Sname from Student, SC, Course, Teacher where Teacher.T =Course.T and Course.C = SC.C and Student.S = SC.S and Teacher.Tname ='TeacherB' Order by SC.Score DESC LIMIT 1;
--Manytop student 
SelectSC.Score, Student.Sname from Student, SC where Student.S = SC.S  and SC.Score = (Select max(SC.score) from SC,Course, Teacher where SC.C =Course.C and Course.T=Teacher.T and Teacher.Tname = 'TeacherB');
SelectSC.Score, Student.Sname from Student, SC, Course, Teacher where Teacher.T =Course.T and Course.C = SC.C and Student.S = SC.S and Teacher.Tname ='TeacherB' and  SC.Score = (Select max(SC.score) from SC,Course, Teacherwhere SC.C = Course.C and Course.T=Teacher.T and Teacher.Tname = 'TeacherB');


--41 student with same score 
--condition in a created table || create virtual table 
SelectSC.S, SC.C, SC.Score from SC where SC.Score in (Select SC.Score from SC groupby SC.Score Having Count(SC.S)>1);
SelectSC.* from SC, (Select Score from SC group by SC.Score Having Count(SC.S)>1 )as m Where  SC.Score =  m.Score;


--39,43:43 student count for each course(>3people)
SelectSC.C, Count(SC.S) from SC group by SC.C Having Count(SC.S)> 3 Order byCount(SC.S) Desc, SC.C ASC; 


--44-45:45 students select all course
Select* from Student where S in (select Distinct SC.S from SC group by S HavingCount(SC.C) = (select Count(Course.C) from Course ) );


--46 students with age 
--Onlyconsider year element (no Month & day )
SelectSname,(EXTRACT(YEAR from CURDATE()) - EXTRACT(YEAR from Sage)) as Age fromStudent;
--ConsiderMonth & day as well as Year to calculate the age
-- If(cond, trueResult, falseResult) expression
selectSname,(EXTRACT(YEAR from CURDATE()) - EXTRACT(YEAR from Sage))-(If(DateDiff(STR_TO_DATE(CONCAT(EXTRACT(MONTH from Sage),"-",EXTRACT(DAY from Sage),"-", EXTRACT(YEAR fromCURDATE())), '%m-%d-%Y'), CURDATE())>0, 1,0)) As Accurate_Age from Student;


--47-50:48 student whose birthday is next week
--Datefunction: WEEKOFYEAR, STR_TO_DATE, EXTRACT, CURDATE
Select* from Student where WEEKOFYEAR(CURDATE())+1 =WEEKOFYEAR(STR_TO_DATE(CONCAT(EXTRACT(MONTH from Sage),"-",EXTRACT(DAY from Sage),"-", EXTRACT(YEAR fromCURDATE())), '%m-%d-%Y')) ;


--End

你可能感兴趣的:(DB)