-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFetch-second-last-record.sql
More file actions
44 lines (39 loc) · 1.74 KB
/
Fetch-second-last-record.sql
File metadata and controls
44 lines (39 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
--Write a SQL query to fetch the second last record from a employee table.
--Tables Structure:
create table employee1
( emp_ID int primary key
, emp_NAME varchar(50) not null
, DEPT_NAME varchar(50)
, SALARY int);
insert into employee1 values(101, 'Mohan', 'Admin', 4000);
insert into employee1 values(102, 'Rajkumar', 'HR', 3000);
insert into employee1 values(103, 'Akbar', 'IT', 4000);
insert into employee1 values(104, 'Dorvin', 'Finance', 6500);
insert into employee1 values(105, 'Rohit', 'HR', 3000);
insert into employee1 values(106, 'Rajesh', 'Finance', 5000);
insert into employee1 values(107, 'Preet', 'HR', 7000);
insert into employee1 values(108, 'Maryam', 'Admin', 4000);
insert into employee1 values(109, 'Sanjay', 'IT', 6500);
insert into employee1 values(110, 'Vasudha', 'IT', 7000);
insert into employee1 values(111, 'Melinda', 'IT', 8000);
insert into employee1 values(112, 'Komal', 'IT', 10000);
insert into employee1 values(113, 'Gautham', 'Admin', 2000);
insert into employee1 values(114, 'Manisha', 'HR', 3000);
insert into employee1 values(115, 'Chandni', 'IT', 4500);
insert into employee1 values(116, 'Satya', 'Finance', 6500);
insert into employee1 values(117, 'Adarsh', 'HR', 3500);
insert into employee1 values(118, 'Tejaswi', 'Finance', 5500);
insert into employee1 values(119, 'Cory', 'HR', 8000);
insert into employee1 values(120, 'Monica', 'Admin', 5000);
insert into employee1 values(121, 'Rosalin', 'IT', 6000);
insert into employee1 values(122, 'Ibrahim', 'IT', 8000);
insert into employee1 values(123, 'Vikram', 'IT', 8000);
insert into employee1 values(124, 'Dheeraj', 'IT', 11000);
--query
select * from(
select EMP_ID, EMP_NAME, DEPT_NAME, SALARY,
row_number() over(order by EMP_ID desc) as emp_rank
from employee1
)
where emp_rank =2
;