Database-data query

  1. Query the names of all students in the grade, and sort them in ascending order by number
    Select sid, sname
    From STUDENTS
    Where grade='1'
    Order by sid;

  2. Query the course scores for which the student's elective scores are qualified, and convert the scores into points
    Select sid,cid,(score-60)*0.1+1 as jidian
    From CHOICES
    Where score>=60; –Excluding the elective records with null scores

  3. Query class is the name of the course or
    the Select CNAME
    the From COURSES
    the Where hour or hour = 30 = 60;

  4. Query all course numbers that contain "number" in the course name
    Select cid
    From COURSES
    Where cname Like'%数%';

  5. Query the course numbers of all course selection records (not repeated display)
    select distinct cid
    from CHOICES;

  6. Statistics of the average salary of all teachers
    Select AVG(salary) as avgSalary
    From TEACHERS;

  7. Query the number, name and average score of all students, and sort them in descending order of the total average score.
    Select sid, Avg(score) as avgScore
    From CHOICES
    Group by sid
    Order by avgScore Desc;

  8. Count the number of electives and average grades of each course
    Select cid, COUNT(sid) as numOfPerson, AVG(score) as avgScore
    From CHOICES
    Group by cid;

  9. Query the number of students who have taken at least two courses
    Select sid
    From CHOICES
    Group by sid
    Having COUNT(sid)>=2;

  10. Query the course names and grades of all courses selected by students with the number S244157
    Select cname, score
    From COURSES,CHOICES
    Where COURSES.cid=CHOICES.cid and sid='S244157';

  11. Query the numbers of all students who have selected the database
    Select sid
    From CHOICES, COURSES
    Where CHOICES.cid=COURSES.cid and cname='database';

  12. Find the pairs of students who have chosen the same course
    Select A.sid as stu1,B.sid as stu2
    From CHOICES A,CHOICES B
    Where A.cid=B.cid and A.no<B.no;

  13. Find the number of the course selected by at least two students
    Select cid
    From CHOICES
    Group by cid
    Having COUNT(*)>=2;

  14. Query the student ID of a course selected by the student who has taken the number S244157 (including the student S244157)
    Select distinct A.sid-If a student has taken multiple courses selected by S244157, the student ID will appear more Times, so use distinct
    From CHOICES A,CHOICES B
    Where A.cid = B.cid and B.sid='S244157';

  15. Inquire about students' basic information and elective course numbers and grades
    Select STUDENTS.*,cid,score
    From STUDENTS Left Outer Join CHOICES On (CHOICES.sid=STUDENTS.sid);

  16. Query the name of the student with the student number S244157 and the name and grade of the elective course
    Select sname, cname, score
    From STUDENTS,CHOICES,COURSES
    Where STUDENTS.sid=CHOICES.sid and CHOICES.cid=COURSES.cid
    and STUDENTS.sid='S244157 ';

  17. Query the data of all students in the same grade of the student with student number S244157
    Select A.*
    From STUDENTS A, STUDENTS B
    Where A.grade=B.grade and B.sid='S244157';

  18. Query the detailed information of all students who have selected courses
    Select *
    From STUDENTS
    Where sid In(select sid
    from CHOICES);
    or:
    Select distinct STUDENTS.* --If you are querying student numbers, write the name as (only write a distinct)
    From STUDENTS, CHOICES-distinct STUDENTS.sid,STUDENTS.sname
    Where STUDENTS.sid=CHOICES.sid; --Use connection to remove student records for unselected courses

  19. Query the number of courses that no students choose
    Select cid
    From COURSES
    Where cid Not In(
    Select cid
    From CHOICES);

  20. Query the student number and name of the students who have taken the elective course named C language
    Select STUDENTS.sid, STUDENTS.sname
    From STUDENTS,CHOICES, COURSES
    Where STUDENTS.sid=CHOICES.sid and CHOICES.cid=COURSES.cid
    and cname='C language ';

  21. Find the elective record with the worst elective course score
    Select *
    From CHOICES
    Where score=(
    Select MIN(score)
    From CHOICES);

  22. Find the elective record with the worst grades in elective courses
    Select *
    From CHOICES
    Where score=(
    Select MIN(score) --null returns unknown when compared with any number, so it is not the minimum value
    From CHOICES);
    – or
    Select *
    From CHOICES
    Where score<=All(
    Select score - the query result contains null, there is no value less than null,
    From CHOICES - ∴ must be removed to find the minimum value
    Where score is not null);
    error:
    Select *
    From CHOICES
    Where score= MIN(score);-the aggregation should not appear in the WHERE clause, unless the aggregation is in the HAVING clause or the subquery contained in the select list, and the column to be aggregated is an external reference

  23. Find out the same course name as the course hour of the course "C language" or course "operating system" (including both)
    Select cname
    From COURSES
    Where hour =some( –or change "=some" to in
    Select hour
    From COURSES
    Where cname ='C language' or cname='operating system');

  24. Inquire about the names of all the students taking the course number C153488.
    Select sname
    From STUDENTS,CHOICES
    Where STUDENTS.sid=CHOICES.sid and cid='C153488';

  25. Query the names of students who have taken all courses.
    Select sname
    From STUDENTS
    Where Not Exists(
    Select *
    From COURSES as X
    Where Not Exists(
    Select *
    From CHOICES as Y
    Where Y.sid=STUDENTS.sid and Y.cid=X.cid
    )
    );

  26. Use set operations to query the student number of the elective course "data structure" or course "database".
    Select sid
    From CHOICES
    Where cid=(
    Select cid
    From COURSES
    Where cname='Data Structure')
    Union
    Select sid
    From CHOICES
    Where cid=(
    Select cid
    From COURSES
    Where cname='Database');

  27. Realize set intersection operation, query the number of students who take both the elective course "Computer Network" and the elective course "C Language".
    Select sid
    From CHOICES
    Where cid=(
    Select cid
    From COURSES
    Where cname='Computer Network')
    Intersect
    Select sid
    From CHOICES
    Where cid=(
    Select cid
    From COURSES
    Where cname='C Language');

  28. Realize the set subtraction operation, query the number of the students who take the elective course C language but not the elective course "computer network".
    Select sid
    From CHOICES
    Where cid=(
    Select cid
    From COURSES
    Where cname='C Language')
    Except
    Select sid
    From CHOICES
    Where cid=(
    Select cid
    From COURSES
    Where cname='Computer Network');

Guess you like

Origin blog.csdn.net/qq_44378854/article/details/107042048