I am reading a tutorial about TOP keyword in T-SQL. But I don't really understand the examples given in the tutorial.
Example 1:
INSERT TOP n INTO table_name1 (Col1, Col2)
SELECT Col3, Col4 FROM table_name2;
The tutorial mentions that the TOP here doesn't really achieve anything since the database doesn't have a concept of order, which I kind of get.
And is it correct to assume that the n
rows that are chosen from table_name2 are completely random records?
Example 2:
The tutorial gives another example that improves the first statement.
INSERT INTO table_name1 (Col1, Col2)
SELECT TOP n Col3, Col4
FROM table_name2
Order By Col1;
Here is the confusing part. Putting the TOP keyword inside the SELECT statement is supposed to give us a better control over what rows get chosen and inserted through the ORDER BY clause. So I would assume the ORDER BY clause is part of the SELECT statement in the second example. But Col1 isn't part of table_name2.
This confused the hell out of me. Thanks for any insight.