Differences between select 1 and select * in Oracle

Source: Internet
Author: User

Create a myt table and insert data as follows:

Create table myt (name varchar2, create_time date)

Insert into myt values ('john', to_date (sysdate, 'dd-MON-YY '));

Insert into myt values ('Tom ', to_date (sysdate, 'dd-MON-YY '));

Insert into myt values ('lili', to_date (sysdate, 'dd-MON-YY '));

The SQL * plus statement is as follows:

SQL> select * from myt;
 
NAME CREATE_TIME
---------------------
John 2010-5-19
Tom 2010-5-19
Lili 2010-5-19
 
SQL> select 1 from myt;
 
1
----------
1
1
1

SQL> select 0 from myt;
 
0
----------
0
0
0
From the above results, we can see that select constant fromtable returns the corresponding constant value for all rows (see the following for specific applications ),

Select * from table returns all columns corresponding to all rows.

Select 1 is often used in the exists clause to check whether a qualified record exists.

For example, select * from T1 where exists (select 1 from T2 where T1.a = T2.a );
Small Data size of T1 and large data size of T2, T1 <T2, 1) high query efficiency.
"Select 1" here "1" is actually irrelevant, but it is okay to replace it with "*". www.bkjia.com only cares whether the data in the brackets can be searched out, whether such a record exists. If the where condition exists, it is true.
Example:

SQL> select 1/0 from dual;
 
Select 1/0 from dual
 
ORA-01476: divisor is 0

SQL> select * from myt where exists (select 1/0 from dual );
 
NAME CREATE_TIME
---------------------
John 2010-5-19
Tom 2010-5-19
Lili 2010-5-19

No error is returned. It indicates that the select statement is not involved in calculation.

Writing select * in the exists clause does not return all columns in the table, but checks whether the qualified records exist.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.