Sorting and Limiting Results Flashcards
7 cards from real SQL practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Sorting and Limiting Results flashcards as text
What does ORDER BY RANDOM() do in PostgreSQL?
Answer: Returns rows in random order
ORDER BY RANDOM() shuffles rows into a random sequence.
Which gets a single random row efficiently in MySQL?
Answer: ORDER BY RAND() LIMIT 1
MySQL uses RAND() with LIMIT 1 to return one random row.
When sorting strings of numbers stored as text, what happens?
Answer: They sort lexicographically (e.g., '10' before '2')
Text columns sort character-by-character, so '10' comes before '2'.
What does ORDER BY 1 DESC do?
Answer: Sorts by the first SELECT column descending
The number 1 references the first column in the SELECT list, sorted descending.
Which is generally faster than LIMIT/OFFSET for deep pagination?
Answer: Keyset (seek) pagination using WHERE on a sorted key
Keyset pagination filters by the last seen key, avoiding the cost of skipping rows.
Does adding LIMIT change the total count of matching rows in the table?
Answer: No, it only limits rows returned
LIMIT only restricts the result set returned, not the underlying data.
To find the second-highest salary, which approach works?
Answer: ORDER BY salary DESC LIMIT 1 OFFSET 1
Sorting descending and skipping the top row returns the second-highest value.