Finding the “First” Entry

Here's a problem based on more than a syntax error: It's a misunderstanding of what a relational database can do.

The question, like others in this chapter, is based on mail sent via the Internet. Given a table with a limited number of codes, each possibly having multiple identification entries, the submitter asked, How do you find the first entry for each code?

There are a couple of odd things about this question. It implies a built-in order, and it assigns a special status to the first entry for each code. Both of these concepts are foreign to relational databases and hence meaningless in SQL.

If you find yourself needing to construct such a query, you should probably start by reexamining your database design. Maybe ...

Get Practical SQL Handbook, The: Using SQL Variants, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.