当前位置:网站首页>[MySQL practice] query statement demonstration
[MySQL practice] query statement demonstration
2022-06-27 22:09:00 【Songdanmin】
List of articles
Simple query
Example of query statement of student course selection database :
- select form where
use Students' course selection ;
SELECT sno,sname FROM student
WHERE sdept = ' Statistics College '
SELECT DISTINCT sno FROM SC
SELECT sno,grade FROM SC
WHERE cno = 10001
ORDER BY grade DESC,sno ASC
SELECT sno,grade*0.8 FROM SC
WHERE cno = 10001 AND (grade < 70 OR grade >60)
SELECT* FROM student
WHERE sdept IN (' Statistics College ',' Graduate School of Engineering ')
ORDER BY sdept DESC
SELECT sno,cno FROM SC
WHERE grade IS NULL
SELECT SC.sno,sname,cname,grade FROM SC,Course,student
WHERE SC.sno = student.sno AND SC.cno =Course.cno
ORDER BY grade DESC
SELECT* FROM Course
WHERE cno IN (SELECT cno FROM SC WHERE sno IN(SELECT sno FROM student WHERE sdept = ' Statistics College ' )
AND ccredit > 2)
SELECT student.*,SC.* FROM student,Sc
WHERE sc.sno=student.sno
SELECT X.sno,sname,grade FROM student X,SC Y
WHERE X.sno=Y.sno AND Y.cno = '10001'
SElECT FIRST.*,SECOND.cpno Antecedent course FROM Course FIRST,Course SECOND
WHERE (FIRST.cpno=SECOND.cname AND SECOND.cpno IS NOT NULL)
nested queries
- left join Left connection
-- nesting
/*
1. There is no... In the elective course UML Therefore, it is amended as follows java
Checked the elective course java Student ID and name of the course
*/
--method1.
select sno ,sname from student where sno in
(select sno from SC where cno in (select cno from Course where cname = 'java'))
--method2.
select student.sno ,sname from student ,SC,Course where student.sno=SC.sno and SC.cno = Course.cno and Course.cname = 'java'
/*
2. Query the student ID and name of students older than Wang Hua .
*/
select sno,sname from student where sage >
(select sage from student where sname =' Wang Hua ')
/*
3. Inquire about c1 The grade of the course is lower than Zhang San's student number and grade
*/
select SC.sno ,grade from SC where grade <
(select grade from SC,student where (SC.sno=student.sno and student.sname='zeze' and SC.cno='c1'))
/*
4. Check the students younger than the students in other colleges
*/
select sname from student where sage >all (select sage from student where sdept =' Statistics College ') AND sdept!=' Statistics College '
/*
5 Check the elective course C2 Name of the student of the course
*/
--method1
select student.sname from student,sc where student.sno=SC.sno and SC.cno= 'c1'
--method2
select student.sname from student left join SC on student.sno=SC.sno left join Course on Course.cno=SC.cno where Course.cno='c1 '
/*
6. The names of students who have taken all courses have been queried
*/
select sname
from student
where not exists (select *
from Course
where not exists( select *
from SC
where SC.sno = student.sno and SC.cno = Course.cno))
/*
7. The query has at least selected students as "S2" The name of the student ID of all the courses selected by the students
*/
select sname
from student
where not exists(select *
from SC
where SC.sno = 'S2' and not exists(select *
from Course
where SC.sno = student.sno and SC.cno = Course.cno))
/*
9. Inquire about Names of students who have taken both data structure and database principle and application courses (ps: Because there is no , So there was no result )
*/
select sname from student
left join SC sc
on sc.sno = student.sno
left join Course c1
on sc.cno = c1.cno
where c1.cname = ' data structure '
intersect
select sname from student
left join SC sc
on sc.sno = student.sno
left join Course c1
on sc.cno = c1.cno
where c1.cname = ' Database principle '
Combination query
- intersection intersect
- Combine union
- remove expcept
- grouping group by
- Filter polymerization having And group by Use it together
-- Combine
/*
1. Use set operation to query the names of students who have taken both the course of data structure and the course of database principle and application .
*/
select sname
from student
where sno in( select sno
from SC
where cno in (select cno
from Course
where cname = ' data structure '))
intersect
select sname
from student
where sno in( select sno
from SC
where cno in (select cno
from Course
where cname = ' Database principle '))
/*
2. Use set operation to query the student number who has taken the course of data structure or the course of database principle and application .
*/
select sno
from SC
where cno in (select cno
from Course
where cname = ' data structure ')
union
select sno
from SC
where cno in (select cno
from Course
where cname = ' Database principle ')
/*
3. Use set operation to query the student number of students who have taken the data structure course instead of the database principle and application course .
*/
select sno
from SC
where cno in (select cno
from Course
where cname = ' data structure ')
except
select sno
from SC
where sno in (select cno
from Course
where cname = ' Database principle ')
/*
4. Count the number of students who have taken courses .
*/
select count(distinct sno) as status
from SC
/*
5. The number of elective courses with qualified results exceeds 4 Student no. of students above class 、 Total score .
(ps:having It is also a kind of filtration , Is aggregate filtering .
where For filtering , Filter the result set before production .
having stay where Filter the aggregated results after filtering the source input .
HAVING Clause allows us to filter groups of data after grouping .
WHERE Clause filters records before aggregation . That is to say, it works on GROUP BY Clause and HAVING Before clause .
and HAVING Clause filters group records after aggregation . )
*/
select SC.sno,SUM(grade) as ' Total score '
from SC
where SC.grade > =60
group by sno
having count(cno)>4
/*
6. Count the number of students in each department .
*/
select sdept ,count(sdept) as total from student
group by sdept
/*
7. Count the number of students of all ages
*/
select sage ,count(sage) as everyone from student
group by sage
/*
8. Count the number of elective courses and average score of each student
*/
select sno,count(cno) as ' Number of courses ',AVG(grade) as ' Average score '
from SC
group by sno
/*
9. Query the details of each course and the number of students selected .
*/
select Course.*,count(SC.cno) as ' The number of students who choose courses '
from Course,SC
where SC.cno = Course.cno
group by Course.cno,ccredit,cname,cpno,ctech
Continuous supplement …
边栏推荐
- [LeetCode]186. 翻转字符串里的单词 II
- [LeetCode]动态规划解拆分整数I[Silver Fox]
- ∫(0→1) ln(1+x) / (x ² + 1) dx
- [LeetCode]动态规划解分割数组II[Arctic Fox]
- GBase 8a OLAP分析函数 cume_dist的使用样例
- 软件缺陷管理——测试人员必会
- 畅游动态规划之区间DP
- Educational Codeforces Round 108 (Rated for Div. 2)
- 【Redis】零基础十分钟学会Redis
- Interview question 3 of software test commonly used by large factories (with answers)
猜你喜欢

List of language weaknesses --cwe, a website worth learning

Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip

单元测试界的高富帅,Pytest框架,手把手教学,以后测试报告就这么做~

Go from introduction to actual combat - execute only once (note)

二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)

Codeforces Round #716 (Div. 2)

GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析

使用Fiddler模拟弱网测试(2G/3G)
![\W and [a-za-z0-9_], \Are D and [0-9] equivalent?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?
![[leetcode] dynamic programming solution partition array ii[arctic fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[leetcode] dynamic programming solution partition array ii[arctic fox]
随机推荐
Software test automation test -- interface test from entry to proficiency, learn a little every day
[LeetCode]100. 相同的树
. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
CUDA error:out of memory caused by insufficient video memory of 6G graphics card
Quick excel export
qt 大文件生成md5校验码
[MySQL] database function clearance Tutorial Part 2 (window function topic)
[LeetCode]515. Find the maximum value in each tree row
Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function
Codeforces Round #721 (Div. 2)
清华大学教授:软件测试已经走入一个误区——“非代码不可”
Selenium上传文件有多少种方式?不信你有我全!
Go from introduction to actual combat - only any task is required to complete (notes)
使用Fiddler模拟弱网测试(2G/3G)
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
How to design an elegant caching function
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
[LeetCode]161. 相隔为 1 的编辑距离
mysql 大于 小于 等于符号的表示方法
Analysis of stone merging