Getting the 2nd highest salary after dpt ID input - plsqldeveloper

I am trying to write this PL SQL query to get the second highest salary out of the table after input of the dpt ID.
I put this together but the query does not run after the input of the dpt ID.
CREATE TABLE OUTPUT_LOG
(my_column VARCHAR(250));
DECLARE
v_dpt_id
BEGIN
select dpt_id
into v_dpt_id
from employees
where dpt_id = inticap('&prompt_user');
select salary from
(select rownum n,a.* from
( select distinct salary from employees order by salary desc) a)
where n = 2;
insert into output_log (my_column)
VALUES (||first_name||' '||last_name||' '||salary||);
end;
SELECT my_column
FROM OUTPUT_LOG;

try this
you can simply change query as like
select * from employees where salary in
(
select salary from
(select rownum n,a.* from
( select distinct salary from employees where dpt_id = initcap('&prompt_user') order by salary desc) a
)
where n = 2
)
this query give all employees which have secode highest salary on their department.
i hope this may help.

Related

In SQL, How to output "NULL" instead of " There are no results to be displayed" when there's no value to be exported

Using MySQL v8.0 right now.
The question is:
Write an SQL query to report the id and the salary of the second highest salary from the
Employee table. If there is no second highest salary, the query should
report null.
My dummy data is:
Create table If Not Exists Employee (id int, salary int);
insert into Employee (id, salary) values
(1, 100);
My ideal output is like this:
+------+--------+
| id | salary |
+------+--------+
| NULL | NULL |
+------+--------+
I used DENSE_RANK as a more straightforward way for me to solve this question:
WITH sub AS (SELECT id,
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS num
FROM Employee )
SELECT id, salary
FROM sub
WHERE num = 2
But I have a problem exporting NULL when there's no second highest salary. I tried IFNULL, but it didn't work. I guess it's because the output is not actually null but just empty.
Thank you in advance.
WITH sub AS (
SELECT id,
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS num
FROM Employee
)
SELECT id, salary
FROM sub
WHERE num = 2
UNION ALL
SELECT NULL, NULL
WHERE 0 = ( SELECT COUNT(*)
FROM sub
WHERE num = 2 );
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=31f5afb0e7e5dce9c2c128ccc49a6f42
Just making your query a subquery and left joining from a single-row producing subquery seems to me the simplest approach:
select id, salary
from (select null) at_least_one_row
left join (
select id, salary
from (
select id, salary, dense_rank() over (order by salary desc) as num
from Employee
) ranked_employees
where num = 2
) second_highest_salary on true
(I usually prefer a subquery to a cte that's only used once; I find that obfuscatory.)

How to retreive 5 highest values from a table

I have table with 10 rows with employee id and salary.
I want get a table with 5 rows in order of the highest salary.
create table employee(
id int not null primary key,
employeeName VarChar(20),
salary int
);
Thank you.
You can use RANK(), or DENSE_RANK, or ROW_NUMBER().
For example:
select *
from (
select *,
rank() over(order by salary desc) as rk
from employee
) x
where rk <= 5
order by rk, employeename
See running example (that show 6 rows since two are tied in fifth place) at DBFidle.

correlated subquery with exists and top clause using cte

with x as
( select max(sal),deptno from emp
group by deptno
)
select * from emp
where exists
(select top 1 1 x on x.max_sal=e.sal and x.deptno=e.deptno)
It's going to fetch records of max sal in each department
I don't have any idea why we are using top 1 1 and what does it mean?
What is the use of exists operator?

Mysql retriving the nth record

Suppose I have a table named EMPLOYEE containing the following attributes
(EMPLOYEE_ID, LAST_NAME, FIRST_NAME, MIDDLE_NAME, JOB_ID, MANAGER_ID, Salary)
Can I
Display the Nth highest salary drawing employee details
Please help
ORDER BY and LIMIT where 10 is n + 1:
SELECT
*
FROM
employees
ORDER BY
Salary DESC
LIMIT
10, 1
(If you want the first record, use LIMIT 0, 1. For the tenth, use LIMIT 9, 1 etc.)
try this
put n > 1 to get corresponding results
n=3 must give you second highest salary
SELECT * --This is the outer query part
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
Test Table
CREATE TABLE Test
(ID INT IDENTITY(1,1),
Salary INT)
INSERT INTO Test
VALUES (100), (200), (300), (400), (500)
SELECT * FROM Test
Query
SELECT TOP 1 Salary
FROM
(SELECT TOP 3 Salary FROM Test ORDER BY Salary DESC)q
ORDER BY Salary ASC
In your Sub-query SELECT TOP Nth the rest remains the same and it will get you the desired results

MySQL: Select top n max values?

I am really confused about the query that needing to return top N rows having biggest values on particular column.
For example, if the rows N-1, N, N + 1 have same values. Must I return just top N or top N + 1 rows.
If you do:
select *
from t
order by value desc
limit N
You will get the top N rows.
If you do:
select *
from t join
(select min(value) as cutoff
from (select value
from t
order by value
limit N
) tlim
) tlim
on t.value >= tlim;
Or you could phrase this a bit more simply as:
select *
from t join
(select value
from t
order by value
limit N
) tlim
on t.value = tlim.value;
The following is conceptually what you want to do, but it might not work in MySQL:
select *
from t
where t.value >= ANY (select value from t order by value limit N)
Use the following SQL query.
SELECT salary FROM salesperson
ORDER BY salary DESC
LIMIT 2,1
You should use self join for this.
first find the top (n) possible values for a perticular column
join it with same table based on the primary key
For E.g. on below sample table
CREATE TABLE `employee` (
`ID` INT(11) AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR(50) NOT NULL,
`SALARY` INT(11) NOT NULL ,
JOINING_DATE TIMESTAMP
) ENGINE=MYISAM
INSERT INTO employee (NAME,salary,joining_date) VALUES('JAMES',50000,'2010-02-02'),
('GARGI',60000,'2010-02-02'),('DAN',30000,'2010-02-02'),('JOHN',10000,'2010-02-02'),('MICHEL',70000,'2010-02-02'),
('STIEVE',50000,'2010-02-02'),('CALRK',20000,'2010-02-02'),('BINNY',50000,'2010-02-02'),('SMITH',40000,'2010-02-02'),
('ROBIN',60000,'2010-02-02'),('CRIS',80000,'2010-02-02');
With the above table-data set up Query to find employees having top 3 salaries would be :
SELECT e1.* FROM
(SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT 3 ) S1
JOIN employee e1
ON e1.salary = s1.salary
ORDER BY e1.salary DESC
TIP:-
If you need top 4 then just change LIMIT 3 to LIMIT 4