CREATE TABLE student (id int, name varchar(30), age int, gender char(6));
INSERT INTO student VALUES
(1 ,'Ranga', 27, 'Male'),
(2 ,'Reddy', 26, 'Male'),
(3 ,'Vasu', 50, 'Female'),
(4 ,'Ranga', 27, 'Male'),
(5 ,'Raja', 10, 'Male'),
(6 ,'Pavi', 52, 'Female'),
(7 ,'Vinod', 27, 'Male'),
(8 ,'Vasu', 50, 'Female'),
(9 ,'Ranga', 27, 'Male');
Query:
-----------------------------------
SELECT name as Name,
(SELECT name FROM student s1
WHERE s1.id < s.id
ORDER BY id DESC LIMIT 1) as Previous_Name,
(SELECT name FROM student s2
WHERE s2.id > s.id
ORDER BY id ASC LIMIT 1) as Next_Name
FROM student s
WHERE id = 7;
Output:
------------------------------------------------------
Name Previous_Name Next_Name
Vinod Pavi Vasu
Sunday, May 17, 2015
Home »
Interview Questions
,
MySQL
,
SQL
» Write a SQL Program to Get the Next and Previous values based on Current value?
0 comments:
Post a Comment