Link to home
Start Free TrialLog in
Avatar of vbnetcoder
vbnetcoder

asked on

what does this do

I see bit of code:

SELECT TOP 1 *

What is the difference between that VS simply doing this:

SELECT TOP 1 *
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

I don't see a difference between your two examples.  Can you please clarify your question?
Avatar of Jim Horn
SELECT means execute a statement that will return a set of records.

TOP 1 returns only one row, either based on the ORDER BY statement provided, or if there is none the first row in the table or view.

* means all columns in the table or view.
Avatar of vbnetcoder
vbnetcoder

ASKER

I'm sorry I meant:

SELECT TOP 1 *

VS

 SELECT TOP 1
The difference is the columns being returned.  "*" returns all columns in the table.  Not having an asterisk will only return the columns specified after "SELECT TOP 1"

For example:
SELECT TOP 1 * FROM myTable

Open in new window


will return all columns from the "TOP 1" record in the table

SELECT TOP 1 FirstName, LastName FROM myTable

Open in new window


will return only the FirstName and LastName colums from the "TOP 1" record in the table
As far as I know if you are using SQL Server the 2nd statement won't work as you aren't specifying any fields.
Limits the rows returned in a query result set to a specified number of rows or percentage of rows in SQL Server 2017. When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows; otherwise, it returns the first N number of rows in an undefined order. ...

Best Practices
In a SELECT statement, always use an ORDER BY clause with the TOP clause. This is the only way to predictably indicate which rows are affected by TOP.
TOP (Transact-SQL)
For backward compatibility, using the TOP expression without parentheses in SELECT statements is supported, but we do not recommend it.
...

*
Specifies that all columns from all tables and views in the FROM clause should be returned. The columns are returned by table or view, as specified in the FROM clause, and in the order in which they exist in the table or view.
SELECT Clause (Transact-SQL)

You will see "select *" used a lot in example SQL, and it is popular with us when making answers because it is a convenient and brief shortcut. However "select *" is NOT something you should use in production quality code. Also take note of the 2 points above regarding the use of TOP
1. use parentheses
2. always use with an ORDER BY
e.g.

SELECT TOP(1) col1, col2, col3 FROM ... ORDER BY col2, col3
Just a note: The difference has nothing to do with TOP at all. The TOP clause ends after specifying what number of top records as absolute number or percentage. Then what follows is just the same as with a SELECT without TOP, the field list.

Bye, Olaf.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ty
welcome !!