50 SQL statements involved in one question

Source: Internet
Author: User
50 SQL statements involved in one question

Student (s #, sname, sage, ssex) Student table
Course (C #, cname, T #) Curriculum
SC (s #, C #, score) Orders table
Teacher (T #, tname) Instructor table

Problem:
1. query the student ID of all students whose score is higher than that of the "002" course;
Select a. s # From (select s #, score from SC where C # = '001') A, (select s #, score
From SC where C # = '002') B
Where a. score> B. score and a. s # = B. S #;
2. query the student ID and average score of students whose average score is greater than 60;
Select s #, AVG (score)
From SC
Group by S # Having AVG (score)> 60;
3. query the student ID, name, number of course selections, and total score of all students;
Select student. s #, student. sname, count (SC. C #), sum (score)
From student left Outer Join SC on student. s # = SC. s #
Group by student. s #, sname
4. query the number of teachers surnamed "Li;
Select count (distinct (tname ))
From teacher
Where tname like 'Li % ';
5. query the student ID and name of the student who has not learned the course "ye ping;
Select student. s #, student. sname
From student
Where s # Not in (select distinct (SC. s #) from SC, course, teacher where SC. C # = course. C # and teacher. T # = course. T # and teacher. tname = 'peiping ');
6. query the student ID and name of the student who has learned the course 001 and has also learned the course 002;
Select student. s #, student. sname from student, SC where student. s # = SC. s # And SC. C # = '001' and exists (select * from SC as SC _2 where SC _2.s # = SC. s # And SC _2.c # = '002 ');
7. query the student ID and name of all students who have learned the course taught by instructor ye Ping;
Select s #, sname
From student
Where s # In (select s # from SC, course, teacher where SC. C # = course. C # and teacher. T # = course. T # and teacher. tname = 'peiping 'group by S # Having count (SC. C #) = (select count (C #) from course, teacher where teacher. T # = course. T # And tname = 'peiping '));
8. query the student ID and name of all students whose score is smaller than the score of course no. "002" than the course No. "001;
Select s #, sname from (select student. s #, student. sname, score, (select score from SC SC _2 where SC _2.s # = student. s # And SC _2.c # = '002 ') score2
From student, SC where student. s # = SC. s # And C # = '001') S_2 where score2 <score;
9. query the student ID and name of all students whose course scores are less than 60;
Select s #, sname
From student
Where s # Not in (select student. s # from student, SC where s.s # = SC. s # And score> 60 );
10. query the student ID and name of the student who has not completed all the courses;
Select student. s #, student. sname
From student, SC
Where student. s # = SC. s # group by student. s #, student. sname having count (C #) <(select count (C #) from course );

11. query the student ID and name of at least one course with the student ID "1001;
Select s #, sname from student, SC where student. s # = SC. s # And C # in select C # from SC where s # = '123 ';
12. query the student ID and name of at least one student whose student ID is 001;
Select distinct SC. s #, sname
From student, SC
Where student. s # = SC. s # And C # In (select C # from SC where s # = '001 ');
13. Change the average score of the Course taught by instructor ye Ping in the SC table;
Update SC set score = (select AVG (SC _2.score)
From SC SC _2
Where SC _2.c # = SC. C #) from course, teacher where course. C # = SC. C # and course. T # = teacher. T # and teacher. tname = 'peiping ');
14. query the student ID and name of other students whose courses are identical to those of "1002;
Select s # from SC where C # In (select C # from SC where s # = '123 ')
Group by S # Having count (*) = (select count (*) from SC where s # = '123 ');
15. Delete the SC table record for learning "ye ping;
Delect SC
From course, teacher
Where course. C # = SC. C # And course. T # = teacher. T # And tname = 'peiping ';
16. insert some records into the SC table. These records must meet the following requirements: Student IDs, 2, and,
Average score of course no;
Insert SC select s #, '002 ', (select AVG (score)
From SC where C # = '002') from student where s # Not in (select s # from SC where C # = '002 ');
17. Show the "Database", "enterprise management", and "English" course scores of all students based on average scores in the following format: Student ID ,, database, enterprise management, English, number of valid courses, average effective score
Select s # As student ID
, (Select score from SC where SC. s # = T. S # And C # = '004 ') as database
, (Select score from SC where SC. s # = T. S # And C # = '001') as Enterprise Management
, (Select score from SC where SC. s # = T. S # And C # = '006 ') as English
, Count (*) as valid course count, AVG (T. Score) as average score
From SC as t
Group by S #
Order by AVG (T. Score)
18. query the highest score and lowest score of each subject: displayed as follows: course Id, highest score, lowest score
Select L. C # As course Id, L. score as highest score, R. score as lowest score
From SC l, SC as R
Where l. C # = r.c # And
L. Score = (select max (IL. Score)
From SC as Il, student as Im
Where l. C # = Il. C # And IM. s # = Il. s #
Group by IL. C #)
And
R. Score = (select Min (IR. Score)
From SC as IR
Where r.c # = IR. C #
Group by IR. C #
);
19. The average score of each subject ranges from high to high and the pass rate.
Select T. C # As course number, max (course. cname) as course name, isnull (AVG (score), 0) as average score
, 100 * sum (case when isnull (score, 0)> = 60 then 1 else 0 end)/count (*) as pass percentage
From SC T, Course
Where T. C # = course. C #
Group by t.c #
Order by 100 * sum (case when isnull (score, 0)> = 60 then 1 else 0 end)/count (*) DESC
20. query the average score and pass percentage of the following courses (shown in "1 line"): Enterprise Management (001), Marx (002), oo & UML (003 ), database (004)
Select sum (case when C # = '001' then score else 0 end)/sum (Case C # When '001' then 1 else 0 end) as average score of Enterprise Management
, 100 * sum (case when C # = '001' and score> = 60 then 1 else 0 end) /sum (case when C # = '001' then 1 else 0 end) as enterprise management pass percentage
, Sum (case when C # = '002' then score else 0 end)/sum (Case C # When '002' then 1 else 0 end) as Marx average score
, 100 * sum (case when C # = '002 'and score> = 60 then 1 else 0 end) /sum (case when C # = '002' then 1 else 0 end) as Marx pass percentage
, Sum (case when C # = '003 'Then score else 0 end)/sum (Case C # When '003' Then 1 else 0 end) as UML average score
, 100 * sum (case when C # = '003 'and score> = 60 then 1 else 0 end) /sum (case when C # = '003 'Then 1 else 0 end) as UML pass percentage
, Sum (case when C # = '004 'then score else 0 end)/sum (Case C # When '004 'then 1 else 0 end) as database average score
, 100 * sum (case when C # = '004 'and score> = 60 then 1 else 0 end) /sum (case when C # = '004 'Then 1 else 0 end) as database pass percentage
From SC
21. query the average scores of different courses taught by different teachers from high to low.
Select max (Z. T #) as instructor ID, max (Z. tname) as instructor name, C. C # As course Id, max (C. cname) as course name, AVG (score) as average score
From SC as t, course as C, teacher as Z
Where t.c # = C. C # And c. t # = Z. T #
Group by C. C #
Order by AVG (score) DESC
22. query the transcript of the following course scores of 3rd to 6th students: Enterprise Management (001), Marx (002), UML (003), Database (004)
[Student ID], [Student name], enterprise management, Marx, UML, database, average score
Select distinct top 3
SC. s # As student ID,
Student. sname as student name,
T1.score as enterprise management,
T2.score as Marx,
T3.score as UML,
T4.score as database,
Isnull (t1.score, 0) + isnull (t2.score, 0) + isnull (t3.score, 0) + isnull (t4.score, 0) as total score
From student, SC left join SC as T1
On SC. s # = t1.s # And t1.c # = '001'
Left join SC as T2
On SC. s # = t2.s # And t2.c # = '002'
Left join SC as T3
On SC. s # = t3.s # And t3.c # = '003'
Left join SC as T4
On SC. s # = t4.s # And t4.c # = '004'
Where student. s # = SC. s # And
Isnull (t1.score, 0) + isnull (t2.score, 0) + isnull (t3.score, 0) + isnull (t4.score, 0)
Not in
(Select
Distinct
Top 15 with ties
Isnull (t1.score, 0) + isnull (t2.score, 0) + isnull (t3.score, 0) + isnull (t4.score, 0)
From SC
Left join SC as T1
On SC. s # = t1.s # And t1.c # = 'k1'
Left join SC as T2
On SC. s # = t2.s # And t2.c # = 'k2'
Left join SC as T3
On SC. s # = t3.s # And t3.c # = 'k3'
Left join SC as T4
On SC. s # = t4.s # And t4.c # = 'k4'
Order by isnull (t1.score, 0) + isnull (t2.score, 0) + isnull (t3.score, 0) + isnull (t4.score, 0) DESC );

23. Print the score of each subject in Statistics. Number of students in each score segment: course Id, course name, [100-85], [85-70], [70-60], [<60]
Select SC. C # As course Id, cname as course name
, Sum (case when score between 85 and 100 then 1 else 0 end) as [100-85]
, Sum (case when score between 70 and 85 then 1 else 0 end) as [85-70]
, Sum (case when score between 60 and 70 then 1 else 0 end) as [70-60]
, Sum (case when score <60 then 1 else 0 end) as [60-]
From SC, Course
Where SC. C # = course. C #
Group by SC. C #, cname;

24. query the average score and rankings of Students
Select 1 + (select count (distinct average score)
From (select s #, AVG (score) as average score
From SC
Group by S #
) As T1
Where average score> T2. average score) as ranking,
S # As student ID, average score
From (select s #, AVG (score) average score
From SC
Group by S #
) As T2
Order by average score DESC;

25. query the top three records of each subject's score (excluding the parallel score)
Select t1.s # As student ID, t1.c # As course Id, score as score
From SC T1
Where score in (select top 3 score
From SC
Where t1.c # = C #
Order by score DESC
)
Order by t1.c #;
26. query the number of students selected for each course
Select C #, count (s #) from SC group by C #;
27. Check the student ID and name of all students who have selected only one course.
Select SC. s #, student. sname, count (C #) as Course selections
From SC, student
Where SC. s # = student. s # group by SC. s #, student. sname having count (C #) = 1;
28. query the number of boys and girls
Select count (ssex) as boys from student group by ssex having ssex = 'male ';
Select count (ssex) as number of girls from student group by ssex having ssex = 'female ';
29. query the student list with the last name "Zhang"
Select sname from student where sname like 'sheet % ';
30. query the list of same-name students and count the number of students with the same name
Select sname, count (*) from student group by sname having count (*)> 1 ;;
31. List of students born on April 9, 1981 (note: the type of the sage column in student table is datetime)
Select sname, convert (char (11), datepart (year, SAGE) as age
From student
Where convert (char (11), datepart (year, SAGE) = '123 ';
32. query the average scores of each course. The results are sorted in ascending order based on the average scores. The average scores are the same and the course numbers are sorted in descending order.
Select C #, AVG (score) from SC group by C # order by AVG (score), C # DESC;
33. query the student ID, name, and average score of all students whose average score is greater than 85
Select sname, SC. s #, AVG (score)
From student, SC
Where student. s # = SC. s # group by SC. s #, sname having AVG (score)> 85;
34. query the names and scores of students whose course names are "databases" and whose scores are less than 60.
Select sname, isnull (score, 0)
From student, SC, Course
Where SC. s # = student. s # And SC. C # = course. C # And course. cname = 'database' and score <60;
35. query the Course selections of all students;
Select SC. s #, SC. C #, sname, cname
From SC, student, Course
Where SC. s # = student. s # And SC. C # = course. C #;
36. query the name, course name, and score of any course whose score is over 70;
Select distinct student. s #, student. sname, SC. C #, SC. Score
From student, SC
Where SC. score> = 70 and SC. s # = student. s #;
37. query failed courses and arrange them in ascending order by course number
Select C # from SC where SCOR e <60 order by C #;
38. query the student ID and name of the student whose course number is 003 and whose score is higher than 80;
Select SC. s #, student. sname from SC, student where SC. s # = student. s # And score> 80 and C # = '003 ';
39. Number of students selected for the course
Select count (*) from SC;
40. query the names of the students with the highest scores and their scores of the students who take the courses taught by "ye ping ".
Select student. sname, score
From student, SC, course C, teacher
Where student. s # = SC. s # And SC. C # = C. C # and C. T # = teacher. T # and teacher. tname = 'peiping 'and SC. score = (select max (score) from SC where C # = C. C #);
41. query each course and the number of optional students
Select count (*) from SC group by C #;
42. query the student's student ID, course number, and student score with the same course score
Select distinct a.s #, B. score from SC a, SC B where a. Score = B. Score and A. C # <> B .C #;
43. query the first two of the best scores for each course
Select t1.s # As student ID, t1.c # As course Id, score as score
From SC T1
Where score in (select Top 2 score
From SC
Where t1.c # = C #
Order by score DESC
)
Order by t1.c #;
44. count the number of optional students in each course (only when the number of students exceeds 10 ). The course number and number of electives must be output. The query results are sorted in descending order of the number of students. If the number of students is the same, the query results are sorted in ascending order of the course number.
Select C # As course number, count (*) as students
From SC
Group by C #
Order by count (*) DESC, C #
45. Search for student IDs that take at least two courses
Select s #
From SC
Group by S #
Having count (*)> = 2
46. query the course number and name of all optional courses for all students
Select C #, cname
From Course
Where C # In (select C # from SC group by C #)
47. query the names of students who have not completed any course taught by instructor ye Ping.
Select sname from student where s # Not in (select s # from course, teacher, SC where course. T # = teacher. T # And SC. C # = course. C # And tname = 'peiping ');
48. query the student ID and average score of two or more failed courses
Select s #, AVG (isnull (score, 0) from SC where s # In (select s # from SC where score <60 group by S # Having count (*)> 2) group by S #;
49. Search for students whose scores are less than 60 in the "004" course in descending order.
Select s # from SC where C # = '004 'and score <60 order by score DESC;
50. Delete the score of course 001 of "002"
Delete from SC where s # = '001' and C # = '001 ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.