当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Passive income: return to the original and safe two ways to earn
Can deep learning overturn video codec? The first prize winner of the National Technological Invention Award nags you in the little red book
MySQL - linked table query
MySQL: Functions
大排量硬核产品来袭,坦克品牌能否冲破自主品牌天花板?
大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
Behind every piece of information you collect, you can't live without TA
Swift QQ authorized login pit set
Day07 operation
Hegong sky team vision training Day7 - vision, Jetson naon and d435i
New attributes of ES6 array
Built in object (bottom)
This large model sparse training method with high accuracy and low resource consumption has been found by Alibaba cloud scientists! Has been included in IJCAI
.net core with microservices - what is a microservice
在CRA创建的项目中使用@并让其识别@路径并给出路径提示
What are VO, do, dto and Po
Global string object (function type) +math object
MySQL : 函数
Mobile page layout
第7天总结&作业









