last() — SQL Technical Interview Questions & Answers
last()
last()
- MAX(marks) highest value. Topper query: ORDER BY marks DESC LIMIT 1 or WHERE marks = (SELECT MAX(marks) FROM students). The subquery form keeps ties.
- LIMIT 1 hides ties. Say that in the interview.
- Trap: MAX without mentioning ties.
- On the example next to this theory: last(): create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
Technical Interview Tip: MAX(marks). Ties: WHERE marks = (SELECT MAX(marks) FROM students).
Code Examples for last()
last() — sample query
-- last()
CREATE TABLE demo (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL,
qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
(1, 'alpha', 2),
(2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;last(): create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
How to Explain last() to an Interviewer
All toppers, not one?
WHERE marks = (SELECT MAX(marks) FROM students)