<p>I've heard that <code>SELECT *</code> is generally bad practice to use when writing SQL commands because it is more efficient to <code>SELECT</code> columns you specifically need.</p>

<p>If I need to <code>SELECT</code> every column in a table, should I use </p>

<pre><code>SELECT * FROM TABLE
</code></pre>

<p>or </p>

<pre><code>SELECT column1, colum2, column3, etc. FROM TABLE
</code></pre>

<p>Does the efficiency really matter in this case? I'd think <code>SELECT *</code> would be more optimal internally if you really need all of the data, but I'm saying this with no real understanding of database.</p>

<p>I'm curious to know what the best practice is in this case.</p>

<p><strong>UPDATE:</strong> I probably should specify that the only situation where I would really <em>want</em> to do a <code>SELECT *</code> is when I'm selecting data from one table where I know all columns will always need to be retrieved, even when new columns are added. </p>

<p>Given the responses I've seen however, this still seems like a bad idea and <code>SELECT *</code> should never be used for a lot more technical reasons that I ever though about.</p>

[{'answer_id': 65518, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 2, 'selected': False, 'text': '<p>definitely defining the columns, because SQL Server will not have to do a lookup on the columns to pull them.  If you define the columns, then SQL can skip that step.</p>\n'}, {'answer_id': 65529, 'author': 'BrewinBombers', 'author_id': 5989, 'author_profile': 'https://Stackoverflow.com/users/5989', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s always better to specify the columns you need, if you think about it one time, SQL doesn\'t have to think "wtf is *" every time you query. On top of that, someone later may add columns to the table that you actually do not need in your query and you\'ll be better off in that case by specifying all of your columns.</p>\n'}, {'answer_id': 65532, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 9, 'selected': True, 'text': '<p>One reason that selecting specific columns is better is that it raises the probability that SQL Server can access the data from indexes rather than querying the table data. </p>\n\n<p>Here\'s a post I wrote about it:  <a href="http://weblogs.asp.net/jgalloway/archive/2007/07/18/the-real-reason-select-queries-are-bad-index-coverage.aspx" rel="noreferrer"><strong>The real reason select queries are bad index coverage</strong></a> </p>\n\n<p>It\'s also less fragile to change, since any code that consumes the data will be getting the same data structure regardless of changes you make to the table schema in the future.</p>\n'}, {'answer_id': 65540, 'author': 'Yann Ramin', 'author_id': 9167, 'author_profile': 'https://Stackoverflow.com/users/9167', 'pm_score': 2, 'selected': False, 'text': '<p>Performance wise, SELECT with specific columns can be faster (no need to read in all the data). If your query really does use ALL the columns, SELECT with explicit parameters is still preferred. Any speed difference will be basically unnoticeable and near constant-time. One day your schema will change, and this is good insurance to prevent problems due to this. </p>\n'}, {'answer_id': 65550, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 2, 'selected': False, 'text': "<p>Select is equally efficient (in terms of velocity) if you use * or columns.</p>\n\n<p>The difference is about memory, not velocity. When you select several columns SQL Server must allocate memory space to serve you the query, including all data for all the columns that you've requested, even if you're only using one of them.</p>\n\n<p>What does matter in terms of performance is the excecution plan which in turn depends heavily on your WHERE clause and the number of JOIN, OUTER JOIN, etc ...</p>\n\n<p>For your question just use SELECT *. If you need all the columns there's no performance difference.</p>\n"}, {'answer_id': 65554, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 1, 'selected': False, 'text': "<p>It depends on the version of your DB server, but modern versions of SQL can cache the plan either way. I'd say go with whatever is most maintainable with your data access code.</p>\n"}, {'answer_id': 65558, 'author': 'cazlab', 'author_id': 6178, 'author_profile': 'https://Stackoverflow.com/users/6178', 'pm_score': 0, 'selected': False, 'text': '<p>Absolutely define the columns you want to SELECT every time.  There is no reason not to and the performance improvement is well worth it.</p>\n\n<p>They should never have given the option to "SELECT *"</p>\n'}, {'answer_id': 65563, 'author': 'DamienG', 'author_id': 5720, 'author_profile': 'https://Stackoverflow.com/users/5720', 'pm_score': 0, 'selected': False, 'text': "<p>If you need every column then just use SELECT * but remember that the order could potentially change so when you are consuming the results access them by name and not by index.</p>\n\n<p>I would ignore comments about how * needs to go get the list - chances are parsing and validating named columns is equal to the processing time if not more. Don't prematurely optimize ;-)</p>\n"}, {'answer_id': 65565, 'author': 'dpollock', 'author_id': 7884, 'author_profile': 'https://Stackoverflow.com/users/7884', 'pm_score': 1, 'selected': False, 'text': "<p>One reason it's better practice to spell out exactly which columns you want is because of possible future changes in the table structure.</p>\n\n<p>If you are reading in data manually using an index based approach to populate a data structure with the results of your query, then in the future when you add/remove a column you will have headaches trying to figure out what went wrong. </p>\n\n<p>As to what is faster, I'll defer to others for their expertise.</p>\n"}, {'answer_id': 65570, 'author': 'Erik', 'author_id': 6733, 'author_profile': 'https://Stackoverflow.com/users/6733', 'pm_score': 0, 'selected': False, 'text': "<p>In terms of execution efficiency I am not aware of any significant difference. But for programmers efficiency I would write the names of the fields because</p>\n\n<ul>\n<li>You know the order if you need to index by number, or if your driver behaves funny on blob-values, and you need a definite order</li>\n<li>You only read the fields you need, if you should ever add more fields</li>\n<li>You get an sql-error if you misspell or rename a field, not an empty value from a recordset/row</li>\n<li>You can better read what's going on.</li>\n</ul>\n"}, {'answer_id': 65601, 'author': 'Alexandre Brasil', 'author_id': 8841, 'author_profile': 'https://Stackoverflow.com/users/8841', 'pm_score': 2, 'selected': False, 'text': '<p>The problem with "select *" is the possibility of bringing data you don\'t really need. During the actual database query, the selected columns don\'t really add to the computation. What\'s really "heavy" is the data transport back to your client, and any column that you don\'t really need is just wasting network bandwidth and adding to the time you\'re waiting for you query to return.</p>\n\n<p>Even if you do use all the columns brought from a "select *...", that\'s just for now. If in the future you change the table/view layout and add more columns, you\'ll start bring those in your selects even if you don\'t need them.</p>\n\n<p>Another point in which a "select *" statement is bad is on view creation. If you create a view using "select *" and later add columns to your table, the view definition and the data returned won\'t match, and you\'ll need to recompile your views in order for them to work again.</p>\n\n<p>I know that writing a "select *" is tempting, \'cause I really don\'t like to manually specify all the fields on my queries, but when your system start to evolve, you\'ll see that it\'s worth to spend this extra time/effort in specifying the fields rather than spending much more time and effort removing bugs on your views or optimizing your app.</p>\n'}, {'answer_id': 65604, 'author': 'Paweł Hajdan', 'author_id': 9403, 'author_profile': 'https://Stackoverflow.com/users/9403', 'pm_score': 2, 'selected': False, 'text': "<p>While explicitly listing columns is good for performance, don't get crazy.</p>\n\n<p>So if you use all the data, try SELECT * for simplicity (imagine having many columns and doing a JOIN... query may get awful). Then - measure. Compare with query with column names listed explicitly.</p>\n\n<p>Don't speculate about performance, <strong>measure it!</strong></p>\n\n<p>Explicit listing helps most when you have some column containing big data (like body of a post or article), and don't need it in given query. Then by not returning it in your answer DB server can save time, bandwidth, and disk throughput. Your query result will also be smaller, which is good for any query cache.</p>\n"}, {'answer_id': 65622, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>hey, be practical. use select * when prototyping and select specific columns when implementing and deploying. from an execution plan perspective, both are relatively identical on modern systems. however, selecting specific columns limits the amount of data that has to be retrieved from disk, stored in memory and sent over the network.</p>\n\n<p>ultimately the best plan is to select specific columns.</p>\n'}, {'answer_id': 65626, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<p>Specifying the column list is <em>usually</em> the best option because your application won't be affected if someone adds/inserts a column to the table.</p>\n"}, {'answer_id': 65639, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 0, 'selected': False, 'text': "<p>Also keep changes in mind. Today, Select * only selects the columns that you need, but tomorrow it may also select that varbinary(MAX) column that i've just added without telling you, and you are now also retreiving all 3.18 Gigabytes of Binary Data that wasn't in the table yesterday.</p>\n"}, {'answer_id': 65663, 'author': 'mikedopp', 'author_id': 7911, 'author_profile': 'https://Stackoverflow.com/users/7911', 'pm_score': 0, 'selected': False, 'text': '<p>Lets think about which is faster. If you can select just the data you need then it is faster. However in testing you can pull all the data to judge what data can be filtered out based on business needs. </p>\n'}, {'answer_id': 65695, 'author': 'Sam', 'author_id': 9406, 'author_profile': 'https://Stackoverflow.com/users/9406', 'pm_score': -1, 'selected': False, 'text': '<p>There can be a huge performance gain by limiting what columns are returned if the records are traversing the internet.</p>\n'}, {'answer_id': 65722, 'author': 'user9385', 'author_id': 9385, 'author_profile': 'https://Stackoverflow.com/users/9385', 'pm_score': 2, 'selected': False, 'text': "<p>It is NOT faster to use explicit field names versus *, if and only if, you need to get the data for all fields.  </p>\n\n<p>Your client software shouldn't depend on the order of the fields returned, so that's a nonsense too.</p>\n\n<p>And it's possible (though unlikely) that you need to get all fields using * because you don't yet know what fields exist (think very dynamic database structure).  </p>\n\n<p>Another disadvantage of using explicit field names is that if there are many of them and they're long then it makes reading the code and/or the query log more difficult.</p>\n\n<p>So the rule should be: if you need all the fields, use *, if you need only a subset, name them explicitly.</p>\n"}, {'answer_id': 65772, 'author': 'Notitze', 'author_id': 9411, 'author_profile': 'https://Stackoverflow.com/users/9411', 'pm_score': 0, 'selected': False, 'text': '<p>Well, it really depends on your metrics and purpose:</p>\n\n<ol>\n<li>If you have 250 columns and want to (indeed) select them all, use select * if you want to get home the same day :)</li>\n<li>If your coding needs flexibility and the table in need is small, again, select * helps you code faster and maintain it easier.</li>\n<li>If you want robust engineering and performance:\n\n<ul>\n<li>write your column names if they\'re just a few, or</li>\n<li>write a tool that lets you easily select/generate your column names</li>\n</ul></li>\n</ol>\n\n<p>As a rule of thumb, when I need to select all columns, I would use "select *" unless I have a very specific reason to do otherwise (plus, I think is faster on tables with many, many columns)</p>\n\n<p>And last, but not least, how do you want adding or deleting a column in the table to affect your code or its maintenance?</p>\n'}, {'answer_id': 65779, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>As with most problems, it depends on what you want to achieve. If you want to create a db grid that will allow all columns in any table, then "Select *" is the answer. However, if you will only need certain columns and adding or deleting columns from the query is done infrequently, then specify them individually. </p>\n\n<p>It also depends on the amount of data you want to transfer from the server. If one of the columns is a defined as memo, graphic, blob, etc. and you don\'t need that column, you\'d better not use "Select *" or you\'ll get a whole bunch of data you don\'t want and your performance could suffer.</p>\n'}, {'answer_id': 65825, 'author': 'Jeff Hubbard', 'author_id': 8844, 'author_profile': 'https://Stackoverflow.com/users/8844', 'pm_score': 0, 'selected': False, 'text': '<p>The main difference between the two is the amount of data passed back and forth. Any arguments about the time difference is fundamentally flawed in that "select *" and "select col1, ..., colN" result in the same amount of relative work performed by the DB engine. However, transmitting 15 columns per row vs. 5 columns per row is a 10-column difference.</p>\n'}, {'answer_id': 66219, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 0, 'selected': False, 'text': '<p>If you are concerned with speed make sure you use prepared statements.  Otherwise I am with ilitirit that changes is what you protect yourself against.</p>\n\n<p>/Allan</p>\n'}, {'answer_id': 66352, 'author': 'mxsscott', 'author_id': 9762, 'author_profile': 'https://Stackoverflow.com/users/9762', 'pm_score': 0, 'selected': False, 'text': "<p>I always recommend specifying the columns you need, just in case your schema changes and you don't need the extra column.</p>\n\n<p>In addition, qualify the column names with the table name. This is critical when the query contains joins. Without the table qualifications, it can be difficult to remember which column comes from which table, and adding a similarly named column to one of the other tables can break your query.</p>\n"}, {'answer_id': 66465, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Use specific field names, so if somebody changes the table on you, you don't get unexpected results. On the subject: ALWAYS specify field names when doing an insert so if you need to add a column later, you don't have to go back and fix your program and change the database at the same time in the production release.</p>\n"}, {'answer_id': 66734, 'author': 'Sam Cogan', 'author_id': 8768, 'author_profile': 'https://Stackoverflow.com/users/8768', 'pm_score': 0, 'selected': False, 'text': '<p>I find listing column names is particually important if other developers are likely to work with the code, or the database is likely to change, so that you are always getting consistent data.</p>\n'}, {'answer_id': 66823, 'author': 'Scott Lawrence', 'author_id': 3475, 'author_profile': 'https://Stackoverflow.com/users/3475', 'pm_score': 0, 'selected': False, 'text': "<p>Whether or not the efficiency matters depends a lot on the size of your production datasets (and their rate of growth).  If your datasets aren't going to be that large, and they aren't going to grow that quickly, there may not be much of a performance advantage to selecting individual columns.</p>\n\n<p>With larger datasets and faster rates of data growth, the performance advantage becomes more and more important.</p>\n\n<p>To see graphically whether or not there's any difference, I would suggest using the query analyzer to see the query execution plan for a SELECT * and the equivalent SELECT col1, col2, etc.  That should tell you which of the two queries is more efficient.  You could also generate some test data of varying volumes see what the timings are.</p>\n"}, {'answer_id': 67296, 'author': 'Dela', 'author_id': 10104, 'author_profile': 'https://Stackoverflow.com/users/10104', 'pm_score': 1, 'selected': False, 'text': '<p>To add on to what everyone else has said, if all of your columns that you are selecting are included in an index, your result set will be pulled from the index instead of looking up additional data from SQL.</p>\n'}, {'answer_id': 67380, 'author': 'IDisposable', 'author_id': 2076, 'author_profile': 'https://Stackoverflow.com/users/2076', 'pm_score': 6, 'selected': False, 'text': "<p>Given <strong>your</strong> specification that you <strong>are</strong> selecting all columns, there is little difference  <strong>at this time</strong>. Realize, however, that database schemas do change. If you use <strong><code>SELECT *</code></strong> you are going to get any new columns added to the table, even though in all likelihood, your code is not prepared to use or present that new data. This means that you are exposing your system to unexpected performance and functionality changes.</p>\n\n<p>You may be willing to dismiss this as a minor cost, but realize that columns that you don't need still must be:</p>\n\n<ol>\n<li>Read from database</li>\n<li>Sent across the network</li>\n<li>Marshalled into your process</li>\n<li>(for ADO-type technologies) Saved in a data-table in-memory</li>\n<li>Ignored and discarded / garbage-collected</li>\n</ol>\n\n<p>Item #1 has many hidden costs including eliminating some potential covering index, causing data-page loads (and server cache thrashing), incurring row / page / table locks that might be otherwise avoided.</p>\n\n<p>Balance this against the potential savings of specifying the columns versus an <strong><code>*</code></strong> and the only potential savings are:</p>\n\n<ol>\n<li>Programmer doesn't need to revisit the SQL to add columns</li>\n<li>The network-transport of the SQL is smaller / faster</li>\n<li>SQL Server query parse / validation time</li>\n<li>SQL Server query plan cache</li>\n</ol>\n\n<p>For item 1, the reality is that you're going to add / change code to use any new column you might add anyway, so it is a wash.</p>\n\n<p>For item 2, the difference is rarely enough to push you into a different packet-size or number of network packets. If you get to the point where SQL statement transmission time is the predominant issue, you probably need to reduce the rate of statements first.</p>\n\n<p>For item 3, there is NO savings as the expansion of the <strong><code>*</code></strong> has to happen anyway, which means consulting the table(s) schema anyway. Realistically, listing the columns will incur the same cost because they have to be validated against the schema. In other words this is a complete wash.</p>\n\n<p>For item 4, when you specify specific columns, your query plan cache could get larger but <strong>only</strong> if you are dealing with different sets of columns (which is not what you've specified). In this case, you <strong>do want</strong> different cache entries because you want different plans as needed.</p>\n\n<p>So, this all comes down, because of the way you specified the question, to the issue resiliency in the face of eventual schema modifications. If you're burning this schema into ROM (it happens), then an <strong><code>*</code></strong> is perfectly acceptable. </p>\n\n<p>However, my general guideline is that you should only select the columns you need, which means that <strong>sometimes</strong> it will look like you are asking for all of them, but DBAs and schema evolution mean that some new columns might appear that could greatly affect the query.</p>\n\n<p>My advice is that you should <strong>ALWAYS SELECT specific columns</strong>. Remember that you get good at what you do over and over, so just get in the habit of doing it right.</p>\n\n<p>If you are wondering why a schema might change without code changing, think in terms of audit logging, effective/expiration dates and other similar things that get added by DBAs for systemically for compliance issues. Another source of underhanded changes is denormalizations for performance elsewhere in the system or user-defined fields.</p>\n"}, {'answer_id': 72267, 'author': 'HLGEM', 'author_id': 9034, 'author_profile': 'https://Stackoverflow.com/users/9034', 'pm_score': 0, 'selected': False, 'text': "<p>It is particularly important for performance to not use select * when you have a join becaseu by definition at least two fields contain the same data. You do not want to waste network resources sending data you don't need fromthe database server to the application or web server. It may seem easier to use select * but it is a bad practice. Since it is easy to drag the column names into the query, just do that instead. </p>\n\n<p>Another issue that occurs when using select * is that there are idiots who choose to add new fields in the middle fo the table (always a bad practice), if you use select * as the basis for an insert then suddenly your column order may be wrong and you may try to insert the social security number into the honorarium (the amoutn of money a speaker may get paid to pick a non-random example) which could be a very bad thing for data integrity. Even if the select isn't an insert, it looks bad to the customer when the data is suddenly in the worng order on the report or web page.</p>\n\n<p>I think think of no circumstance when using select * is preferable to using a column list. You might think it is easier to maintain, but in truth it is not and will result in your application getting slower for no reason when fields you don't need are added to the tables. You will also have to face the problem of fixing things that would not have broken if you had used a column list, so the time you save not adding a column is used up doing this.</p>\n"}, {'answer_id': 97426, 'author': 'Cade Roux', 'author_id': 18255, 'author_profile': 'https://Stackoverflow.com/users/18255', 'pm_score': 0, 'selected': False, 'text': "<p>There are cases where SELECT * is good for maintenance purposes, but in general it should be avoided.</p>\n\n<p>These are special cases like views or stored procedures where you want changes in underlying tables to propagate without needing to go and change every view and stored proc which uses the table.  Even then, this can cause problems itself, like in the case where you have two views which are joined.  One underlying table changes and now the view is ambiguous because both tables have a column with the same name.  (Note this can happen any time you don't qualify all your columns with table prefixes).  Even with prefixes, if you have a construct like:</p>\n\n<p>SELECT A.<em>, B.</em> - you can have problems where the client now has difficulty selecting the right field.</p>\n\n<p>In general, I do not use SELECT * unless I am making a conscious design decision and counting on related risks to be low.</p>\n"}, {'answer_id': 97510, 'author': 'Morikal', 'author_id': 18272, 'author_profile': 'https://Stackoverflow.com/users/18272', 'pm_score': 0, 'selected': False, 'text': '<p>For querying the DB directly (such as at a sqlplus prompt or through a db administration tool), select * is generally fine--it saves you the trouble of writing out all the columns.</p>\n\n<p>On the other hand, in application code it is best to enumerate the columns. This has several benefits:</p>\n\n<ul>\n<li>The code is clearer</li>\n<li>You will know the order the results come back in (this may or may not be important to        you)</li>\n</ul>\n'}, {'answer_id': 115059, 'author': 'Herb Caudill', 'author_id': 239663, 'author_profile': 'https://Stackoverflow.com/users/239663', 'pm_score': 3, 'selected': False, 'text': "<p>Specifying column names is definitely faster - for the server. But if </p>\n\n<ol>\n<li><strong>performance is not a big issue</strong> (for example, this is a website content database with hundreds, maybe thousands - but not millions - of rows in each table); AND</li>\n<li>your job is to create <strong>many small, similar applications</strong> (e.g. public-facing content-managed websites) using a common framework, rather than creating a complex one-off application; AND</li>\n<li><strong>flexibility is important</strong> (lots of customization of the db schema for each site);</li>\n</ol>\n\n<p>then you're better off sticking with SELECT *. In our framework, heavy use of SELECT * allows us to introduce a new website managed content field to a table, giving it all of the benefits of the CMS (versioning, workflow/approvals, etc.), while only touching the code at a couple of points, instead of a couple dozen points. </p>\n\n<p>I know the DB gurus are going to hate me for this - go ahead, vote me down - but in my world, developer time is scarce and CPU cycles are abundant, so I adjust accordingly what I conserve and what I waste.</p>\n"}, {'answer_id': 275917, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>SELECT * is necessary if one wants to obtain metadata such as the number of columns.</p>\n'}, {'answer_id': 338980, 'author': 'HLGEM', 'author_id': 9034, 'author_profile': 'https://Stackoverflow.com/users/9034', 'pm_score': 0, 'selected': False, 'text': "<p>I see that several people seem to think that it takes much longer to specify the columns. Since you can drag the column list over from the object browser, it takes maybe an extra minute to specify columns (that's if you have a lot of columns and need to spend some time putting them on separate lines) in the query. Why do people think that is so time-consuming?</p>\n"}, {'answer_id': 1681941, 'author': 'klkitchens', 'author_id': 72852, 'author_profile': 'https://Stackoverflow.com/users/72852', 'pm_score': 1, 'selected': False, 'text': "<p>Gonna get slammed for this, but I do a select * because almost all my data is retrived from SQL Server Views that precombine needed values from multiple tables into a single easy to access View.</p>\n\n<p>I do then want all the columns from the view which won't change when new fields are added to underlying tables.  This has the added benefit of allowing me to change where data comes from.  FieldA in the View may at one time be calculated and then I may change it to be static.  Either way the View supplies FieldA to me.</p>\n\n<p>The beauty of this is that it allows my data layer to get datasets.  It then passes them to my BL which can then create objects from them.  My main app only knows and interacts with the objects.  I even allow my objects to self-create when passed a datarow.</p>\n\n<p>Of course, I'm the only developer, so that helps too :) </p>\n"}, {'answer_id': 2972030, 'author': 'kennytm', 'author_id': 224671, 'author_profile': 'https://Stackoverflow.com/users/224671', 'pm_score': 2, 'selected': False, 'text': '<p>The result is too huge. It is slow to generate and send the result from the SQL engine to the client.</p>\n\n<p>The client side, being a generic programming environment, is not and should not be designed to filter and process the results (e.g. the WHERE clause, ORDER clause), as the number of rows can be huge (e.g. tens of millions of rows).</p>\n'}, {'answer_id': 2972032, 'author': 'Giorgi', 'author_id': 239438, 'author_profile': 'https://Stackoverflow.com/users/239438', 'pm_score': 5, 'selected': False, 'text': "<p>You should only select the columns that you need. Even if you need all columns it's still better to list column names so that the sql server does not have to query system table for columns.</p>\n\n<p>Also, your application might break if someone adds columns to the table. Your program will get columns it didn't expect too and it might not know how to process them.</p>\n\n<p>Apart from this if the table has a binary column then the query will be much more slower and use more network resources.</p>\n"}, {'answer_id': 2972041, 'author': 'Matthew Abbott', 'author_id': 357693, 'author_profile': 'https://Stackoverflow.com/users/357693', 'pm_score': 2, 'selected': False, 'text': '<p>You should really be selecting only the fields you need, and only the required number, i.e.</p>\n\n<pre><code>SELECT Field1, Field2 FROM SomeTable WHERE --(constraints)\n</code></pre>\n\n<p>Outside of the database, dynamic queries run the risk of injection attacks and malformed data.  Typically you get round this using stored procedures or parameterised queries. Also (although not really that much of a problem) the server has to generate an execution plan each time a dynamic query is executed.</p>\n'}, {'answer_id': 2972058, 'author': 'bkaid', 'author_id': 265570, 'author_profile': 'https://Stackoverflow.com/users/265570', 'pm_score': 0, 'selected': False, 'text': "<p>The <code>SELECT *</code> <em>might</em> be ok if you actually needed all of the columns - but you should still list them all individually.  You certainly shouldn't be selecting all rows from a table - even if the app &amp; DB are on the same server or network.  Transferring all of the rows will take time, especially as the number of rows grows.  You should have at least a where clause filtering the results, and/or page the results to only select the subset of rows that need to be displayed. Several ORM tools exist depending on app language you are using to assist in querying and paging the subset of data you need.  For example, in .NET Linq to SQL, Entity Framework, and nHibernate all will help you with this.</p>\n"}, {'answer_id': 2972084, 'author': 'Don', 'author_id': 233581, 'author_profile': 'https://Stackoverflow.com/users/233581', 'pm_score': 2, 'selected': False, 'text': "<p>Naming each column you expect to get in your application also ensures your application won't break if someone alters the table, as long as your columns are still present (in any order).</p>\n"}, {'answer_id': 2972095, 'author': 'VladV', 'author_id': 23714, 'author_profile': 'https://Stackoverflow.com/users/23714', 'pm_score': 3, 'selected': False, 'text': "<p>SELECT * is a bad practice even if the query is not sent over a network. </p>\n\n<ol>\n<li>Selecting more data than you need makes the query less efficient - the server has to read and transfer extra data, so it takes time and creates unnecessary load on the system (not only the network, as others mentioned, but also disk, CPU etc.). Additionally, the server is unable to optimize the query as well as it might (for example, use covering index for the query).</li>\n<li>After some time your table structure might change, so SELECT * will return a different set of columns. So, your application might get a dataset of unexpected structure and break somewhere downstream. Explicitly stating the columns guarantees that you either get a dataset of known structure, or get a clear error on the database level (like 'column not found').</li>\n</ol>\n\n<p>Of course, all this doesn't matter much for a small and simple system.</p>\n"}, {'answer_id': 2972114, 'author': 'pkh', 'author_id': 334871, 'author_profile': 'https://Stackoverflow.com/users/334871', 'pm_score': 5, 'selected': False, 'text': "<p>There are four big reasons that <code>select *</code> is a bad thing:</p>\n\n<ol>\n<li><p>The most significant practical reason is that it forces the user to magically know the order in which columns will be returned. It's better to be explicit, which also protects you against the table changing, which segues nicely into...</p></li>\n<li><p>If a column name you're using changes, it's better to catch it early (at the point of the SQL call) rather than when you're trying to use the column that no longer exists (or has had its name changed, etc.)</p></li>\n<li><p>Listing the column names makes your code far more self-documented, and so probably more readable.</p></li>\n<li><p>If you're transferring over a network (or even if you aren't), columns you don't need are just waste.</p></li>\n</ol>\n"}, {'answer_id': 2972339, 'author': 'Faisal', 'author_id': 2652084, 'author_profile': 'https://Stackoverflow.com/users/2652084', 'pm_score': 1, 'selected': False, 'text': "<p>What everyone above said, plus:</p>\n\n<p>If you're striving for readable maintainable code, doing something like:</p>\n\n<p>SELECT foo, bar FROM widgets;</p>\n\n<p>is instantly readable and shows intent. If you make that call you know what you're getting back. If widgets only has foo and bar columns, then selecting * means you still have to think about what you're getting back, confirm the order is mapped correctly, etc. However, if widgets has more columns but you're only interested in foo and bar, then your code gets messy when you query for a wildcard and then only use some of what's returned.</p>\n"}, {'answer_id': 2974504, 'author': 'HLGEM', 'author_id': 9034, 'author_profile': 'https://Stackoverflow.com/users/9034', 'pm_score': 1, 'selected': False, 'text': "<p>And remember if you have an inner join by definition you do not need all the columns as the data in the join columns is repeated.</p>\n\n<p>It's not like listing columns in SQl server is hard or even time-consuming. You just drag them over from the object browser (you can get all in one go by dragging from the word columns). To put a permanent performance hit on your system (becasue this can reduce the use of indexes and becasue sending unneeded data over the network is costly) and make it more likely that you will have unexpected problems as the database changes (sometimes columns get added that you do not want the user to see for instance) just to save less than a minute of development time is short-sighted and unprofessional.</p>\n"}, {'answer_id': 2974791, 'author': 'Chris Wuestefeld', 'author_id': 10082, 'author_profile': 'https://Stackoverflow.com/users/10082', 'pm_score': 2, 'selected': False, 'text': '<p>Lots of good reasons answered here so far, here\'s another one that hasn\'t been mentioned.</p>\n\n<p>Explicitly naming the columns will help you with maintenance down the road. At some point you\'re going to be making changes or troubleshooting, and find yourself asking "where the heck is that column used". </p>\n\n<p>If you\'ve got the names listed explicitly, then finding every reference to that column -- through all your stored procedures, views, etc -- is simple. Just dump a CREATE script for your DB schema, and text search through it.</p>\n'}, {'answer_id': 29006404, 'author': 'Lahiru Cooray', 'author_id': 4640046, 'author_profile': 'https://Stackoverflow.com/users/4640046', 'pm_score': 2, 'selected': False, 'text': "<p>Performance wise I have seen comments that both are equal. but usability aspect there are some +'s and -'s</p>\n\n<p>When you use a (select *) in a query and if some one alter the table and add new fields which do not need for the previous query it is an unnecessary overhead. And what if the newly added field is a blob or an image field??? your query response time is going to be really slow then. </p>\n\n<p>In other hand if you use a (select col1,col2,..) and if the table get altered and added new fields and if those fields are needed in the result set, you always need to edit your select query after table alteration.</p>\n\n<p>But I suggest always to use select col1,col2,... in your queries and alter the query if the table get altered later...</p>\n"}, {'answer_id': 55983534, 'author': 'Premier Bowling Scorekeeper', 'author_id': 11251537, 'author_profile': 'https://Stackoverflow.com/users/11251537', 'pm_score': 1, 'selected': False, 'text': '<p>This is an old post, but still valid.  For reference, I have a very complicated query consisting of:</p>\n\n<ul>\n<li>12 tables </li>\n<li>6 Left joins </li>\n<li>9 inner joins </li>\n<li>108 total columns on all 12 tables </li>\n<li>I only need 54 columns </li>\n<li>A 4 column Order By clause</li>\n</ul>\n\n<p>When I execute the query using Select *, it takes an average of 2869ms.\nWhen I execute the query using Select , it takes an average of 1513ms.</p>\n\n<p>Total rows returned is 13,949.</p>\n\n<p>There is no doubt selecting column names means faster performance over Select *</p>\n'}, {'answer_id': 64120253, 'author': 'user14361342', 'author_id': 14361342, 'author_profile': 'https://Stackoverflow.com/users/14361342', 'pm_score': -1, 'selected': False, 'text': '<p>When we need all columns, I think select * is faster than all columns.</p>\n'}]
['https://Stackoverflow.com/questions/65512', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/392/']