autoincrement column insertion

Shaifali jain 420 Reputation points
2023-09-20T07:45:04.49+00:00
I am trying to insert multiple data at once in the id column of the table below but found out that the data in the id column is not inserted in ascending form . please correct me why  

```sql


CREATE TABLE sub_group (
    id INT IDENTITY(1,1) PRIMARY KEY,
    sub_group_name VARCHAR(255) NOT NULL UNIQUE
);

INSERT INTO sub_group (sub_group_name) VALUES
    ('Sundry Creditors'),
    ('Sundry Debtors'),
    ('Provision'),
    ('Cash in hand'),
    ('Duties & Taxes'),
    ('Deposit(asset)'),
    ('Bank Accounts'),
    ('Loans & Advance (Assets)'),
    ('Bank OD & OCC Account'),
    ('Secured Loans'),
    ('Unsecured Loans'),
    ('Stock in hand'),
    ('Reserves & Surplus');

	select * from sub_group


this is the result i got
id sub_group_name

7 Bank Accounts

9 Bank OD & OCC Account

4 Cash in hand

6 Deposit(asset)

5 Duties & Taxes

8 Loans & Advance (Assets)

3 Provision

13 Reserves & Surplus

10 Secured Loans

12 Stock in hand

1 Sundry Creditors

2 Sundry Debtors

11 Unsecured Loans


SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,788 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sreeju Nair 12,346 Reputation points
    2023-10-18T17:03:30.2733333+00:00

    If you look into the ID created, and compare your insert query order, it is created in the correct order. In your select query, the data is retrieved in the order of sub group name. Try execute the query to select the data in the order of id.

    select * from sub_group order by id
    
    

    Hope this helps.

    0 comments No comments

  2. Dan Guzman 9,236 Reputation points
    2023-10-18T17:07:07.68+00:00

    Note a table is an unordered set of rows. If result set ordering is required, you need an ORDER BY clauses.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.