PTA SQL 部分練習問題セット

10-1 姓が「Li」の生徒全員の名前、民族、連絡先を調べます。

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

10-2 2 科目以上受講した学生の人数と平均成績を問い合わせます。

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

10-3 各商品の販売数量をカウントする

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

10-4 最初の 3 つのコースのコース番号とコース名を問い合わせます

select cno, cname
from course
order by cno limit 3

10-5 名前に「明」という単語が含まれる男子生徒の生徒名とクラスを照会します。

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

10-6 名前2文字の生徒情報を問い合わせる

select *
from students
where sname like "__"

10-7 コース「0000001」の平均点、最高点、最低点を計算します。

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

10-8 各学生が選択した科目数と試験の合計得点をカウントし、選択した科目数に応じて昇順に並べます

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

10-9は2021年9月1日までに24歳に達する男子学生の情報です。

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

10-10 製品テーブルをクエリし、最初に製品カテゴリで昇順に並べ替え、次に製品価格で降順に並べ替えます。

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

10-11 商品テーブルの各キーワードに対応する商品の数を問い合わせる

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

10-12 製品テーブルのいくつかのフィールドをクエリする

select id, category_id, name
from sh_goods

10-13 各カテゴリの製品の最高価格を取得します。

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

10-14 商品テーブルの商品在庫の最高値と最低値を問い合わせる

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

10-15 指定した条件の商品の平均価格を求める

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

10-16 商品テーブルクエリステートメントでの演算子の使用

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 製品テーブル内の指定した価格帯の製品情報を問い合わせます

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

10-18 製品テーブルのフィールドが NULL かどうかを確認します

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

10-19 商品表から商品名に「パッド」が含まれる商品を取得します

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

10-20 商品テーブルの指定条件の商品情報を問い合わせる(複数条件問い合わせ)

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

10-21 商品テーブルの指定条件の商品情報を問い合わせる(複数条件問い合わせ)

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

10-22 各大学の学生の総数を数え、降順に並べます。

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

10-23 2 つ以上のコースを受講した学生の学生数と平均成績を問い合わせます。

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

10-24 教師テーブル内のすべての教師の部門番号を照会し、重複レコードを削除します

select DepartmentID
from Teacher
group by DepartmentID

10-25 すべての教師情報をクエリし、教師番号で昇順に並べ替えます

select *
from Teacher
order by TeacherID

10-26 生年月日が最も大きい (つまり最年少の) 学生の名前と生年月日をクエリします。

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

10-27 5人以上の生徒のクラス番号を問い合わせる

select ClassID
from Class
where StudentNum >= 5

10-28 コース「Dp010001」の最高スコアを問い合わせる

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

10-29 コース「Dp010004」の学生番号と成績を問い合わせて成績の降順に並べ、成績が同じ場合は学生番号の昇順にソートします

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

10-30 選択科目名に「データベース」という単語が含まれており、成績が 80 ~ 90 点の学生の学生番号と成績を照会します。

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

10-31 コース内の最高 2 つの成績を問い合わせる

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 女子の成績を修正

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

10-33 C言語科目を履修している女子生徒の成績記録を削除する

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 商品テーブルから在庫数が注文数量に満たない商品情報を調べる

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 「ネットワーク工学」を専攻する全学生よりも若い学生の名前を問い合わせます

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

10-37 ソフトウェアエンジニアリング専攻の最年長学生の名前を問い合わせる

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

10-38 選択科目が 2 つ以上あり、スコアが 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 学生テーブルの数学科の学生情報をstuテーブルに挿入

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

10-40 未購入の商品情報

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

10-41 S001 の学生が受講し、S003 の学生が受講していないコースを問い合わせる (MSSQL)

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

10-42 2 つ以上の選択コースを持ち、スコアが 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 履修した科目数をカウントします 複数人で同じ科目を履修した場合は、1科目のみカウントします。

select count(distinct cno) 门数
from sc

10-44 選択科目数が最も多い3コースの統計

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

10-45 「Lu Yi」と同じ学部の学生の名前を問い合わせる

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

10-46 コメント情報なしで製品 ID と名前をクエリする (マルチテーブル クエリ)

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

10-47 ユーザーが5つ星と評価した商品のレビュー情報をクエリ(複数テーブルクエリ)

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

10-48 五つ星商品に対応する商品分類情報の問い合わせ(マルチテーブルクエリ)

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 価格が500未満の商品カテゴリ名のクエリ(マルチテーブルクエリ)

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

10-50 3科目以上履修した学生の学籍番号、氏名、所属を取得します。

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

10-51 各必修科目の科目番号、科目名、選択科目数を問い合わせる

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 「大学中国語」のスコアが 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 は、各従業員の番号、名前、給与および給与レベルを示します。

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

10-54 各従業員の番号と名前、およびその上司の番号と名前を表示します (すべての従業員を表示する必要があります)。

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

10-55 お問い合わせ番号「dep01001」 講師名

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

10-56 成績を持たない学生の学生ID、名前、性別を問い合わせる

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

10-57 学生のコース選択を照会すると、結果セットには学生番号、名前、コース番号、コース名、学期、学年が含まれます。

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 平均以上の成績を収めたコースをクエリする

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 張先生の授業をすべて受講している学生に質問する

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 学生が履修していない科目番号と科目名を取得する

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

p6 パーツよりも供給量が多い 10 ~ 61 個の spj-query パーツ

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 3 種類の PC を製造しているメーカーを問い合わせる

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

おすすめ

転載: blog.csdn.net/weixin_70206677/article/details/129374916