Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "select 1 from" do?

I read some articles but really didn't understand what does select 1 from do? Someone says "you should use select 1 instead of select *". Here is an example table:

cust_id     cust_name       cust_address  1000000001  Village Toys    Mapl 1000000002  Kids Place      South 1000000003  Fun4All         Sunny 1000000004  Fun4All         Riverside 1000000005  The Toy Store   53rd 

What will the result be when I write select 1 from customer_table what does this statement do?

like image 900
Mehmet Avatar asked Aug 12 '11 12:08

Mehmet


People also ask

What does select 1 from dual do?

In your case, SELECT 1 FROM DUAL; will simply returns 1 . You need it because the INSERT ALL syntax demands a SELECT clause but you are not querying the input values from a table.

What does select * from do?

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.

What is the meaning of select * from table?

Hi, Select * from any table will fetch and display all the column in that table, while Select 1 from any table will display one row with 1 without any column name.

What does 1 mean in SQL?

WHERE 1 is a synonym for "true" or "everything." It's a shortcut so they don't have to remove the where clause from the generated SQL.


1 Answers

select 1 from table 

will return a column of 1's for every row in the table. You could use it with a where statement to check whether you have an entry for a given key, as in:

if exists(select 1 from table where some_column = 'some_value') 

What your friend was probably saying is instead of making bulk selects with select * from table, you should specify the columns that you need precisely, for two reasons:

1) performance & you might retrieve more data than you actually need.

2) the query's user may rely on the order of columns. If your table gets updated, the client will receive columns in a different order than expected.

like image 90
Vladimir Avatar answered Oct 12 '22 21:10

Vladimir