Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query - Select * from view or Select col1, col2, ... colN from view [duplicate]

Tags:

sql

sql-server

We are using SQL Server 2005, but this question can be for any RDBMS.

Which of the following is more efficient, when selecting all columns from a view?

Select * from view 

or

Select col1, col2, ..., colN from view
like image 408
Napco Avatar asked Sep 24 '08 17:09

Napco


People also ask

Can we use SELECT * In view?

If you change the structure of any tables in the underlying view, select * may break any applications that rely on the columns being in a specific order etc. It's generally accepted that doing select * anywhere, not just in view definitions, is bad practice.

How do I query duplicate records in SQL?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.

What is SELECT * from in SQL?

An asterisk (" * ") can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause, which indicates the table(s) to retrieve data from.

How do I SELECT without duplicate rows in SQL?

If you want the query to return only unique rows, use the keyword DISTINCT after SELECT . DISTINCT can be used to fetch unique rows from one or more columns.


1 Answers

NEVER, EVER USE "SELECT *"!!!!

This is the cardinal rule of query design!

There are multiple reasons for this. One of which is, that if your table only has three fields on it and you use all three fields in the code that calls the query, there's a great possibility that you will be adding more fields to that table as the application grows, and if your select * query was only meant to return those 3 fields for the calling code, then you're pulling much more data from the database than you need.

Another reason is performance. In query design, don't think about reusability as much as this mantra:

TAKE ALL YOU CAN EAT, BUT EAT ALL YOU TAKE.

like image 179
devlord Avatar answered Oct 04 '22 02:10

devlord