当前位置:网站首页>Database hyperphone (III)
Database hyperphone (III)
2022-07-27 17:34:00 【Moon in bubble】
One 、 Use TOP Limit the number of rows in the result set
- TOP n [ percent ] [WITH TIES ]
- TOP n: Get the first... Of the query results n Row data .
- TOP n percent: Get the first... Of the query results n% That's ok .
- WITH TIES: Including parallel results .
- TOP The predicate is written in SELECT Behind the word
(1) Check the names of the three oldest students 、 Age and department ,
Including juxtaposition .
SELECT TOP 3 WITH TIES Sn, age, dept`
FROM S
ORDER BY age DESC
notes : If you use WITH TIES Options , Must be used ORDER BY Clause , Otherwise, there will be grammatical errors
(2) Inquire about VB The names of the top three students with the highest test scores 、 Department and
VB Test results , Including juxtaposition .
SELECT TOP 3 WITH TIES Sn, dept, Score
FROM S JOIN SC on S.Sno = SC.Sno
JOIN C ON C.Cno = SC.Cno
WHERE Cn = 'VB'
ORDER BY Score DESC
(3) Query the two courses with the least number of students ( It does not include courses without candidates ),
List the course number and the number of people selected .
select top 2 with ties Cno count(Cno) The number of elective courses
from SC
group by Cno
order by count(Sno)
(4) Inquire about VB The top one with the highest score in the exam 3 Names of students , Department and VB Examination results include juxtaposition
select top 3 with ties Sn,dept scorce
from S,SC,C
where S.Sno = SC.Sno and SC.Cno=C.Cno and CN = 'VB'
order by scorce desc
(5) The number of courses selected by the computer department exceeds 2 Among the students of the school , The top with the highest average score in the exam 2 name ( Including juxtaposition ) Student's student number , Number of courses selected and average grade
select top 2 with ties S.Sno,count(Cno) The number of elective courses AVG(Scorce)
from S,SC
where S.Sno = SC.Sno and dept =' Department of Computer Science '
group by S. Sno
having count(Cno)
order by AVG(Scorce)
CASE expression
- A multi branch expression , One of several possible results can be returned according to the value of the condition list Can be used wherever expressions are allowed .
- Not a complete T-SQL sentence , You can't do it alone .
CASE Test expression
WHEN Simple expressions 1 THEN The result expression 1
WHEN Simple expressions 2 THEN The result expression 2
…
WHEN Simple expressions n THEN The result expression n
[ ELSE The result expression n+1 ]
END
Evaluate the test expression , Then match the value of the test expression with each WHEN Clause .
If it is equal to the value of the test expression , Returns the first matching WHEN The value of the result expression corresponding to clause .
If it does not match the values of all test expressions :
If you have any ELSE Clause , Then return to ELSE Clause
If there is no clause , Then return to NULL
(1) The query selected VB Student id of the course 、 full name 、 Department and grades ,
And deal with the Department as follows :
“ Department of Computer Science ”: Show “CS”;
“ Information management department ”: Show “IM”;
“ Department of communication engineering ”: Show “COM”.
SELECT S.Sno Student number ,Sn full name ,
CASE dept
WHEN ' Department of Computer Science ' THEN 'CS'
WHEN ' Information management department ' THEN 'IM'
WHEN ' Department of communication engineering ' THEN 'COM'
END AS Department ,Score achievement
FROM S join SC ON S.Sno=SC.Sno
JOIN C ON C.Cno = SC.Cno
WHERE Cn = 'VB'
CASE
WHEN Boolean expression 1 THEN The result expression 1
WHEN Boolean expression 2 THEN The result expression 2
…
WHEN Boolean expression n THEN The result expression n
[ ELSE The result expression n+1 ]
END
- Calculate each... In writing order from top to bottom WHEN Boolean expression of clause .
- Returns the first value of TRUE The result expression value corresponding to the Boolean expression of .
- If not, the value is TRUE The Boolean expression of :
- If you have any ELSE Clause , Then return to ELSE Clause
- If there is no clause , Then return to NULL.
(2) Count the number of courses selected by each student in the computer department , Including students who don't choose courses .
List the student number 、 The number of courses selected and the situation of course selection , The treatment of course selection is :
If the number of courses selected exceeds 4 door , Then for “ many ”;
If the number of courses selected is 2~4 Within the scope of , Then for “ commonly ”;
If the number of courses selected is less than 2 door , Then for “ Less ”;
If the student doesn't choose a course , be “ Unselected ”.
Sort the query results in descending order according to the number of courses selected .
SELECT S.Sno, COUNT(SC.Cno) The number of elective courses ,CASE
WHEN COUNT(SC.Cno) > 4 THEN ' many '
WHEN COUNT(SC.Cno) BETWEEN 2 AND 4 THEN ' commonly '
WHEN COUNT(SC.Cno) BETWEEN 1 AND 2 THEN ' Less '
WHEN COUNT(SC.Cno) = 0 THEN ' Unselected '
END AS Course selection
FROM S LEFT JOIN SC ON S.Sno = SC.Sno
WHERE dept = ' Department of Computer Science '
GROUP BY S.Sno
ORDER BY COUNT(SC.Cno) DESC
边栏推荐
- Windows and network foundation-15-local security policy
- 奇瑞欧萌达也太像长安UNI-T了,但长得像,产品力就像吗?
- Built in object (bottom)
- An analysis of CPU explosion of a smart logistics WCS system in.Net
- 立创EDA——原理图的布局与检查(三)
- Kubernetes Chapter 8: deploy NFS system with kubernetes to complete database persistence (kubernetes work practice class)
- SAP UI5 FileUploader 使用的隐藏 iframe 和 form 元素的设计明细
- AppStore 内购
- JSP El expression, JSTL tag
- Purchase in Appstore
猜你喜欢

SAP UI5 FileUploader 的隐藏 iframe 设计明细

Gods at dusk, "cat trembles" bid farewell to the big V Era

下棋机器人折断7岁男孩手指,网友:违反了机器人第一定律

Shell编程规范与变量

Xcode releases test package testflight

High cost, difficult to implement, slow to take effect, what about open source security?

阿里巴巴鹰眼系统简介

Can deep learning overturn video codec? The first prize winner of the National Technological Invention Award nags you in the little red book

SAP UI5 FileUploader 的本地文件上传技术实现分享

国产新冠口服药为什么是治艾滋病的药
随机推荐
Mobile page layout
With the arrival of large displacement hard core products, can the tank brand break through the ceiling of its own brand?
Uncle's nephew and his students
大排量硬核产品来袭,坦克品牌能否冲破自主品牌天花板?
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
helm安装kubevela完整Makefile脚本内容
Project exercise: the function of checking and modifying tables
Two table joint query 1
Kubernetes第八篇:使用kubernetes部署NFS系统完成数据库持久化(Kubernetes工作实践类)
动作捕捉系统用于柔性机械臂的末端定位控制
MySQL: 函数
URL 返回nil 以及urlhash处理
Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
奇瑞欧萌达也太像长安UNI-T了,但长得像,产品力就像吗?
立创EDA——PCB的布局(四)
CUE语言基础入门:CUE是一门为配置而生的语言
交换机和路由器技术-02-以太网交换机工作原理
高精度定时器
Windows与网络基础-15-本地安全策略
Purchase in Appstore