views:

1502

answers:

6

I was asked this question and could not come up with the answer.

Is there a way to find the entry at the first row and first column in a table?

(In matrix notation, that would be the [1,1]th location)

+6  A: 

In a couple of words: a relational table is not a matrix.

In relational world, 1'st row makes sense only when you define some order on your dataset. There is no such thing as "implicitly first record".

Assuming you order your rows on the values of the first column, it's this:

SELECT  column1
FROM    table
ORDER BY
        column1
LIMIT 1

Also note that different columns in the table belong to different domains.

The point of having the table is to define some relation between these domains, that's why the whole thing is called "relational database", and the tables themselves are sometimes called "relations".

The column order also has no special meaning in a relational table.

It's assumed you always refer to the column by name.

If replacing the order of a column affects the result of your query, it is considered a poor design.

There are things like SELECT * and ORDER BY 1, but they generally should be avoided.

Quassnoi
A: 
Select top 1 Column1
From MyTable
Order by Column1
codeulike
+1  A: 

Conceptually, SQL tables are unordered. You need to specify columns to order by in order for the idea of the "first row" to be meaningful.

This is easy if your DBMS supports LIMIT.

SELECT * FROM table ORDER BY column LIMIT 1

Otherwise you can use TOP, but if your order by column has duplicate values for the top item, you might get more rows than you ask for (depending on your DBMS, in MS-SQL, it won't do this unless you specify WITH TIES).

SELECT TOP 1 * FROM Table ORDER BY column
Tmdean
+1  A: 

A relational table doesn't have a defined row order, so there is no first row. You can certainly select the first result from a query using whatever LIMIT equivalent your database offers.

Some databases that use ISAM-type tables will appear to return a consistent row order when you run queries without an ordered cursor, but deleting and inserting can easily change that.

Andrew Duffy
A: 

If you are trying to find the first record entered into the table, then you need to have set up a DateEntered field at the time of table creation, then you can find the first record by seraching for the earliest date. Ordering on the identity field if you have one is a way to get the earlier records but you can't really guarantee that transaction which created ID 1 really got inserted to the table earlier than the transaction that generated ID 2. You only know that transaction started first, not finished first. Using a date field is the only way to truly be sure. Over the years, I've learned to design for these things, you can't query information later that you didn't store in the first place.

HLGEM
+1  A: 

There were a lot of good feedback related to the "first row" part of thge question. But as far as the "first column" goes, you can try one of the following options:

  1. You can use database data dictionary to get the first column of the table. In Oracle, for example the query would be:

    select column_name  
    from   user_tab_columns 
    where  table_name='YOUR_TABLE_NAME' 
    and    column_id=1
    
  2. If you use JDBC or some other database access API, most of these APIS would allow you to do "select *" and then you could get first column by accessing zero (or first) element of you result set.

IK