当前位置:网站首页>[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 …
边栏推荐
- 01 golang environment construction
- [LeetCode]513. Find the value in the lower left corner of the tree
- [LeetCode]动态规划解拆分整数I[Silver Fox]
- 单元测试界的高富帅,Pytest框架,手把手教学,以后测试报告就这么做~
- 如何做好功能测试?你确定不想知道吗?
- qt 大文件生成md5校验码
- 石子合并问题分析
- [LeetCode]508. 出现次数最多的子树元素和
- Gbase 8A OLAP analysis function cume_ Example of dist
- 年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数
猜你喜欢
Go from introduction to practice -- definition and implementation of behavior (notes)
Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
Management system itclub (Part 2)
Remote invocation of microservices
[leetcode] dynamic programming solution split integer i[silver fox]
不外泄的测试用例设计秘籍--模块测试
[leetcode] dynamic programming solution partition array i[red fox]
Experience sharing of meituan 20K Software Test Engineers
The create database of gbase 8A takes a long time to query and is suspected to be stuck
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
随机推荐
QT base64 encryption and decryption
软件缺陷管理——测试人员必会
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
Experience sharing of meituan 20K Software Test Engineers
GBase 8a数据库用户密码安全相关参数汇总
Use Fiddler to simulate weak network test (2g/3g)
Go from introduction to practice - error mechanism (note)
regular expression
Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
∫(0→1) ln(1+x) / (x ² + 1) dx
[leetcode] dynamic programming solution split integer i[silver fox]
[LeetCode]动态规划解拆分整数I[Silver Fox]
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
[LeetCode]30. 串联所有单词的子串
[LeetCode]30. Concatenate substrings of all words
年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数
Read write separation master-slave replication of MySQL
qt 大文件生成md5校验码