PTA SQL Partial Practice Problem Set

10-1 Find out the names, ethnic groups and contact numbers of all students with the surname "Li".

select sname, nation, phone
from student
where sname like "李%"

10-2 Query the student numbers and average grades of students who have taken more than 2 courses.

select sno 学号, round(avg(grade),1) 平均成绩
from score
group by sno
having count(sno) >= 2

10-3 Count the sales quantity of each commodity

select gid 商品编号, sum(quantity) 销售总数量
from recorder
group by gid

10-4 Query the course number and course name of the first three courses

select cno, cname
from course
order by cno limit 3

10-5 Query the student names and classes of boys whose names contain the word "Ming"

select sname, class
from students
where sname like "%明%"

10-6 Query the student information whose name is two characters

select *
from students
where sname like "__"

10-7 Calculate the average score, maximum score and minimum score of the course "0000001"

select avg(score) 平均分, max(score) 最高分, min(score) 最低分
from sc
where cno = 0000001

10-8 Count the number of courses selected by each student and the total score of the exam, and arrange them in ascending order according to the number of courses selected

select sno 学号, count(*) 选课门数, sum(score) 考试总成绩
from sc 
group by sno
order by sno

10-9 shows the information of male students who have reached the age of 24 by September 1, 2021.

SELECT *
FROM student
WHERE timestampdiff(year,birth,'2021-09-01') >= 24
AND sex = '男'

10-10 Query the product table, first sort by product category in ascending order, then by product price in descending order

select name, category_id, price
from sh_goods
order by category_id, price desc

10-11 Query the number of products corresponding to each keyword in the product table

select keyword, count(*) goodscount
from sh_goods
group by keyword

10-12 Query some fields in the product table

select id, category_id, name
from sh_goods

10-13 Obtain the highest price of the product under each category

select category_id, max(price) max_price
from sh_goods
group by category_id

10-14 Query the highest and lowest value of commodity inventory in commodity table

select max(stock) stock1, min(stock) stock2
from sh_goods

10-15 Obtain the average price of commodities with specified conditions

select category_id, avg(price) average
from sh_goods
group by category_id
having count(*) > 2

10-16 The use of operators in the commodity table query statement

select name, price old_price, stock old_stock, price*0.75 new_price, stock+850 new_stock
from sh_goods
where score = 5

10-17 Query the product information in the specified price range in the product table

select id, name, price
from sh_goods
where price between 2000 and 6000

10-18 Determine whether the field in the product table is NULL

select id, name, price
from goods
where price is NULL

10-19 Get the products whose product name contains "pad" in the product table

select id, name, price
from goods
where name like '%pad%'

10-20 Query the product information of the specified conditions in the product table (multi-condition query)

select id, name, price
from sh_goods
where category_id = 3 and score = 5

10-21 Query the product information of the specified conditions in the product table (multi-condition query)

select name, price, score
from sh_goods
where score = 4.5 or price < 10

10-22 Count the total number of students in each college and arrange them in descending order.

select dept 院部, count(*) 总人数
from student
group by dept
order by count(*) desc

10-23 Query the student numbers and average grades of students who have taken more than 2 courses.

select sno 学号, avg(grade) 平均分
from score
group by sno
having count(*) >= 2

10-24 Query the department numbers of all teachers in the teacher table, and eliminate duplicate records

select DepartmentID
from Teacher
group by DepartmentID

10-25 Query all teacher information, sort by teacher number in ascending order

select *
from Teacher
order by TeacherID

10-26 Query the name and date of birth of the student with the largest date of birth (that is, the youngest).

select StudentName, Birth
from Student
where Birth =(
    select max(Birth)
    from Student
)

10-27 Query the class number with more than 5 students

select ClassID
from Class
where StudentNum >= 5

10-28 Query the highest score of course "Dp010001"

select max(Grade) max_grade
from Grade
where CourseID = 'Dp010001'

10-29 Query the student numbers and grades of the course "Dp010004", and arrange them in descending order of grades, and sort them in ascending order of student numbers if they have the same grades

select StudentID, Grade
from Grade
where CourseID = 'Dp010004'
order by Grade desc, StudentID

10-30 Query the student numbers and grades of students whose elective course names contain the word "database" and whose grades are between 80 and 90 points

select sno, score
from sc
where score between 80 and 90 
and cno in(
    select cno
    from course
    where cname like '%数据库%'
)

10-31 Query the two highest grades in the course

select stu.sno,stu.sname,sc.grade from stu 
join sc on
stu.sno=sc.sno
and sc.cno='C002'
group by sno
order by grade desc
limit 2

10-32 Modify girls' grades

update sc
set grade = grade*1.05
where grade < 75 and sno in (
    select sno 
    from stu 
    where sex = 0
)

10-33 Delete the grade records of girls taking C language courses

delete from sc
where sno in(
    select sno
    from stu
    where sex = 0
)and cno in(
    select cno
    from cou
    where cname = 'C语言'
)

10-34 A1-7 Find out the product information in the product table whose stock quantity is less than the order quantity

select ProductID, ProductName
from products
where UnitsInStock < UnitsOnOrder
10-35 B1-7查找每位领导的直接下属数量
select b.EmployeeID, count(*) countSub
from employees a, employees b
where a.ReportsTo = b.EmployeeID
group by b.EmployeeID 

10-36 Query the names of students who are younger than all the students majoring in "Network Engineering"

select sname
from stu
where birdate > (
    select max(birdate)
    from stu, major
    where mname = '网络工程' and stu.mno = major.mno
)

10-37 Query the name of the oldest student in the software engineering major

select sname
from stu, major
where major.mname = '软件工程' and birdate = (
    select min(birdate)
    from stu
)

10-38 Query courses with more than 2 electives and scores above 60

select cou.cno 课程号,cname 课程名,max(grade) 最高成绩,min(grade) 最低成绩,avg(grade) 平均成绩 
from stu,cou,sc
where stu.sno = sc.sno and cou.cno = sc.cno 
group by cou.cno
having count(*) > 2 and min(grade) >= 60 and count(*) = count(grade); 

10-39 Insert the student information of the School of Mathematics in the student table into the stu table

Insert into stu
select * from student
where dept='数计学院'

10-40 Product information that has not been purchased

select gid, gname, price, stock
from good
where gid not in(
    select gid
    from recorder
)

10-41 Query the courses that S001 students take but S003 students do not take (MSSQL)

select cno 课程号
from sc
where sno = 'S001' and cno not in(
    select cno
    from sc
    where sno = 'S003'
)

10-42 Query students with more than 2 elective courses and scores above 80 (MSSQL)

select max(sname) 姓名, max(mname) 专业名, sum(credit) 总学分
from cou, stu, sc, major
where sc.sno = stu.sno and cou.cno = sc.cno and major.mno = stu.mno 
group by stu.sno
having count(cou.cno) >= 2 and min(grade) >= 80;	

10-43 Count the number of courses that students have taken. If multiple people take the same course, only one course is counted.

select count(distinct cno) 门数
from sc

10-44 Statistics of the 3 courses with the largest number of electives

select cno 课程号, count(*) 选修人数
from sc
group by cno
order by count(cno) desc
limit 3

10-45 Query the names of students in the same department as "Lu Yi"

select sname
from students
where sdept = (
    select sdept
    from students
    where sname = '陆毅'
)and sname != '陆毅'

10-46 Query the product id and name without any comment information (multi-table query)

select id, name
from sh_goods
where id not in(
    select goods_id
    from sh_goods_comment
)

10-47 Query the review information of products rated 5 stars by users (multi-table query)

select sg.name, sgc.content
from sh_goods sg, sh_goods_comment sgc
where sg.id = sgc.goods_id and score = 5

10-48 Query the product classification information corresponding to the five-star product (multi-table query)

select sg.id gid, sgc.id cid, sgc.name cname, score
from sh_goods sg join sh_goods_category sgc on sg.category_id = sgc.id
where score = 5

10-49 Query product category names whose price is less than 500 (multi-table query)

select name
from sh_goods_category
where id in (
    select category_id
    from sh_goods
    where price < 500
)

10-50 Retrieve the student number, name, and department of students who have taken more than three courses.

select sno, sname, dept
from student
where sno in(
    select sno
    from score
    group by sno
    having count(*) >= 3
)

10-51 Query the course number, course name and number of electives of each required course

select c.cno, c.cname, count(sno) total
from course c left join score s on c.cno = s.cno
where attribute = '必修'
group by c.cno

10-52 Find the names and departments of all the students whose "College Chinese" scores are above 80

select sname,dept from student,score,course
where student.sno = score.sno 
and course.cno = score.cno 
and cname='大学语文' 
and grade >80

10-53 shows the number, name, salary and salary level of each employee.

select empno,ename,sal,grade from emp,salgrade
where sal between losal and hisal 

10-54 Show the number and name of each employee and the number and name of their supervisor (required to show all employees).

select  a.empno 员工编号, a.ename 员工姓名, a.mgr 上司编号, b.ename 上司姓名  
from emp a left join emp b on a.mgr = b.empno

10-55 Inquiry number 'dep01001' teacher's head name

select DepartmentHeader from Teacher,Department
where Teacher.DepartmentID = Department.DepartmentID and TeacherID = 'dep01001'

10-56 Query the student ID, name, and gender of students without course grades

select StudentID, StudentName, Sex
from Student s
where StudentID not in(
    select StudentID
    from Grade
)

10-57 Query the student's course selection, and the result set includes student number, name, course number, course name, semester and grade.

select student.sno, sname, course.cno, cname, term, grade
from student, course, score
where student.sno = score.sno and course.cno = score.cno

10-58 Query courses with grades above average

select sno 学号, cname 课程名, grade 成绩
from cou, sc
where cou.cno = sc.cno and (
    sc.grade >(
        select avg(b.grade)
        from sc b
        where sc.sno = b.sno)
)

10-59 Query the students who take all the courses taught by Mr. Zhang

select sname 
from stu
where sno in (
    select sno 
    from sc 
    where cno in (
        select cno 
        from cou 
        where teacher='张老师'
    )
    group by sno
    having count(sno)=(
        select count(cno) 
        from cou
        where teacher='张老师'
    )
)

10-60 Retrieve course numbers and course names not taken by students

select cno, cname
from course
where cno not in(
    select cno
    from score
)

10-61 spj-query parts with higher supply quantity than p6 parts

select distinct pno 
from spj x
 where pno not in(
     select pno
     from spj y
     where y.qty<=(
        select max(qty)
        from spj
        where pno='p6'
     )
 )

10-62 6-7 Query manufacturers who produce three different types of PCs

select maker
from product, pc
where product.model = pc.model
group by maker
having count(*) >= 3

10-63 列出所有学生的选课情况(包括学号,姓名,课号,成绩),结果中包括没有选课的学生

select students.sno, sname, cno, score
from students left join sc on students.sno = sc.sno

10-64 查询所有产品名中包含’螺母’的产品种类数

select count(*)
from product
where PName = '螺母'

10-65 查询所有员工中最高工资和最低工资

select max(Salary) max_Salary, min(Salary) min_Salary
from employee

10-66 查询每个仓库的编号及员工数量

select Wno, count(Eid) Count_Eid
from employee
where Wno is not null
group by Wno

10-67 查询’A01’仓库中的职工中比’A02’所有职工薪水都高的职工编号与姓名

select Eid, EName
from employee
where Salary > (
    select max(Salary)
    from employee
    where Wno = 'A02'
)

10-68 查询销售数量最多的供应商编号

select Sid 
from orders
group by Sid
having sum(QTY) >=all (
    select sum(QTY) 
    from orders 
    group by Sid
)

10-69 查询销售过’0011’号员工销售的所有产品的员工编号和姓名

select Eid, EName
from employee
where Eid in(
    select Eid
    from orders
    where Pid in(
        select Pid
        from orders
        where Eid = '0011'
    )and Eid != '0011'
)

10-70 4-6 查询在具有最小内存容量的所有PC中具有最快处理器的PC制造商

select maker 
from product,pc
where product.model = pc.model 
order by ram , speed desc
limit 1;

10-71 5-2 查询至少生产两种不同的计算机(PC或便携式电脑)且机器速度至少为133的厂商

select maker
from product left join pc on product.model = pc.model 
left join laptop on product.model = laptop.model 
and pc.speed >= 133 and laptop.speed >= 133
group by maker
having count(*) >= 2
order by maker;

10-72 查询’A01’仓库中的职工中比’A02’任意一个职工薪水少的职工编号与姓名

select Eid,EName from employee
where Wno ='A01' and Salary < (
    select max(Salary) 
    from employee 
    where wno = 'A02'
)

10-73 86.删除所有期末成绩小于60分的选课记录

delete from sc 
where SCScore3 < 60

Guess you like

Origin blog.csdn.net/weixin_70206677/article/details/129374916