Database practice questions 4--sql simple query (including answers)

Experimental purposes
1. Master the basic grammar of the SELECT statement and the expression method
of query conditions ; 2. Master the query condition expressions and usage methods;
3. Master the functions and usage methods of the GROUP BY clause;
4. Master the functions and usage of the HAVING clause Method;
5. Grasp the function and usage of ORDER BY clause.

1. List all students whose surname is not Liu;

select *
from student
where sname not like '刘%'

2. List the students whose last name is "Shen" and their full name is 3 Chinese characters;

select *
from student
where sname  like '沈%' and LEN(sname)=3

3. Display the basic information of students born after 1985;

select * from student
where year(getdate())-sage>1985

4. Query the basic information of all courses whose course name contains the "data" string;

select * from course
where cname like '数据%'

5. Show the student ID, name, gender, age and department of the student whose eighth or ninth digit is 1, 2, 3, 4 or 9;

select * from student
where sno like '_______[12349]%' 
and sno like '________[12349]%'

6. List the students who have taken the '1' course, in descending order of grades;

select sno,grade from sc
where cno='1'
order by grade desc

7. List the student numbers of all students who have taken courses "1" and "2" at the same time;

select sno from sc
where cno ='1' and 
sno in(select sno from sc where cno='2')

8. List all the information in the course schedule, arranged in ascending order of prerequisite courses;

select * from course 
order by cpno asc

9. List all students whose age is above the average, displayed in descending order of age;

select * from student
where sage>(select AVG(sage)from student)
order by sage desc

10. Display the student ID, name, gender, year of birth, and department of all students in ascending order of the year of birth, and specify the column headings in the result set as "student ID, name, gender, year of birth, department";

select sno 学号,sname 姓名,ssex 性别,
YEAR(GETDATE ())-sage 出生年份,
sdept 院系 
from student
order by YEAR(GETDATE ())-sage asc

11. Display the student number, course number and grades of students whose course grades are between 70-80 in descending order of course number and grade;

select * from sc
where grade>=70 and grade<80
order by cno desc,grade desc

12. Display the total number of students and average age in the student information table. The column headings in the result set are designated as "total number of students, average age";

select COUNT(sno)学生总人数,
		AVG(sage)平均年龄
from student

13. Show the number of elective courses for each student whose number of elective courses is greater than 3;

select Sno,COUNT(sno)
from sc
group by sno having COUNT(sno)>3

14. Display the total number of elective courses, the highest score, the lowest score and the average score in descending order of the course number;

select cno,COUNT(*),MAX(grade),
MIN(grade),AVG(grade)
from sc
group by cno
order by cno desc

15. Show the student number and average grade of each student whose average score is greater than the average score of "200515001" students;

select sno,AVG(grade)
from sc
group by sno
having AVG(grade)>(select AVG(grade) 
from sc where sno='200515001')

16. Shows the number and passing rate of each elective course;

select COUNT(case when grade>=60 
then 1 end)各课程及格人数,
CAST(count(case when grade>=60 
then 1 end)/count(*)as float)及格比率
from sc
group by cno

17. Show the student number with the largest number of elective courses and the student number with the least number of elective courses;

select sno 学号,COUNT(cno)选修课程数
from sc
group by sno
having COUNT(cno)>=all (select COUNT(cno)
from sc
group by sno)
or COUNT(cno)<=all(select COUNT(cno)
from sc
group by sno)

18. Display the number of male and female students in each faculty, and the column headings in the result set are designated as "Department Name, Number of Boys, Number of Girls";

select sdept as 院系名称,
COUNT(ssex) as 男生人数
from student
where ssex='男'
group by sdept

19. List the student numbers and average grades of students who have failed two or more courses (including two);

select sno,AVG(grade)
from sc
where grade<60
group by sno
having COUNT (cno)>=2

Guess you like

Origin blog.csdn.net/ssdssa/article/details/108985057