What is the method for SQL to search for the top three
Most people do not understand the knowledge points of this article "what is the method of SQL search in the top three", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the method of SQL search in the top three".
The specific data and SQL are like this.
WITH Employee AS (
SELECT 1 Id, 'joe' Name,70000 SALARY,1 DepartmentId FROM DUAL UNION ALL
SELECT 2 Id, 'Henry' Name,80000 SALARY,2 DepartmentId FROM DUAL UNION ALL
SELECT 3 Id, 'Sam' Name,60000 SALARY,2 DepartmentId FROM DUAL UNION ALL
SELECT 4 Id, 'Max' Name,90000 SALARY,1 DepartmentId FROM DUAL UNION ALL
SELECT 5 Id, 'Janet' Name,69000 SALARY,1 DepartmentId FROM DUAL UNION ALL
SELECT 6 Id, 'Randy' Name,85000 SALARY,1 DepartmentId FROM DUAL
),
Department AS (
SELECT 1 Id, 'IT' Name FROM DUAL UNION ALL
SELECT 2 Id, 'Sales' Name FROM DUAL
)
SELECT T1.namename T2.*
FROM
Department T1
, Employee T2
WHERE T1.ID = T2.DepartmentId
AND 3 >
(
SELECT COUNT (distinct T3.SALARY) RN
FROM Employee T3
WHERE T2.DepartmentId = T3.DepartmentId
AND T2.SALARY < T3.SALARY
)
ORDER BY T2.DEPARTMENTID,T2.SALARY
The above is about the content of this article on "what is the method of searching the top three in SQL". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.