Find the third highest and third lowest ‘url_id’



CREATE TABLE mytb (url_id int, url_addr varchar(100));

INSERT INTO mytb VALUES (1, 'www.google.com');
INSERT INTO mytb VALUES (2, 'www.microsoft.com');
INSERT INTO mytb VALUES (3, 'www.apple.com');
INSERT INTO mytb VALUES (4, 'www.google.com');
INSERT INTO mytb VALUES (5, 'www.cnn.com');
INSERT INTO mytb VALUES (6, 'www.apple.com');



Third hightest url_id
SELECT max(url_id) FROM myta WHERE url_id < (
SELECT max(url_id) FROM myta WHERE url_id < (
          SELECT max(url_id) FROM myta
)
)

Third lowest url_id
SELECT min(url_id) FROM myta WHERE url_id > (
SELECT min(url_id) FROM myta WHERE url_id > (
          SELECT min(url_id) FROM myta
)
)


Post a Comment