first() — SQL Technical Interview Questions & Answers
first()
first()
- MIN(marks) lowest value, skips NULL. SELECT name, marks FROM students WHERE marks = (SELECT MIN(marks) FROM students); can return several students who tied.
- MIN on strings is alphabetical (collation). MIN on dates is earliest.
- Trap: MIN as ‘first inserted row’ — it is not, unless you MIN(id).
- On the example next to this theory: first(): create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
Technical Interview Tip: MIN(marks) lowest score. Subquery to get those students.
Code Examples for first()
first() — sample query
-- first()
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;first(): create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
How to Explain first() to an Interviewer
Lowest marks?
SELECT MIN(marks) FROM students