Home » SQL & PL/SQL » SQL & PL/SQL » select * from one table but not all tables in a query (Oracle 11g)
select * from one table but not all tables in a query [message #599989] Wed, 30 October 2013 13:56 Go to next message
seahaze
Messages: 2
Registered: October 2013
Location: Boston, MA
Junior Member
Hi all,

First post...and a very basic question.

How does one select * from one table without selecting * from other tables that are included in a query? For example, if in the query below I want to view all fields in some_table, but not the fields from other_table, how do it?

select *
from some_table st,
other_table ot
where st.id = ot.id

Thanks in advance!

Re: select * from one table but not all tables in a query [message #599990 is a reply to message #599989] Wed, 30 October 2013 13:59 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
seahaze wrote on Wed, 30 October 2013 11:56
Hi all,

First post...and a very basic question.

How does one select * from one table without selecting * from other tables that are included in a query? For example, if in the query below I want to view all fields in some_table, but not the fields from other_table, how do it?

select *
from some_table st,
other_table ot
where st.id = ot.id

Thanks in advance!



explicitly enumerate the columns that are to be projected.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: select * from one table but not all tables in a query [message #599991 is a reply to message #599989] Wed, 30 October 2013 14:00 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
seahaze wrote on Thu, 31 October 2013 00:26


if in the query below I want to view all fields in some_table, but not the fields from other_table, how do it?

select *
from some_table st,
other_table ot
where st.id = ot.id


You are very close to your answer by using aliases for tables. Just mention table_alias.* in the select statement. Try and post it back here.

PS - Thanks that you posted a query as a test case in your first post, however, using code tags would have been more appreciated. In future, please follow the link that Blackswan posted.

[Updated on: Wed, 30 October 2013 14:02]

Report message to a moderator

Re: select * from one table but not all tables in a query [message #599992 is a reply to message #599991] Wed, 30 October 2013 14:03 Go to previous messageGo to next message
seahaze
Messages: 2
Registered: October 2013
Location: Boston, MA
Junior Member
Brilliant! That worked. Thank you, Lalit!

Re: select * from one table but not all tables in a query [message #599993 is a reply to message #599992] Wed, 30 October 2013 14:08 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It is a Bad Practice to have production SQL that does SELECT *
Re: select * from one table but not all tables in a query [message #599994 is a reply to message #599991] Wed, 30 October 2013 14:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
PS - Thanks that you posted a query as a test case


You should read test case to know what it is.
And your condescending tone is still an offend.

Re: select * from one table but not all tables in a query [message #600076 is a reply to message #599993] Thu, 31 October 2013 11:31 Go to previous messageGo to next message
rc3d
Messages: 213
Registered: September 2013
Location: Baden-Württemberg
Senior Member
BlackSwan wrote on Wed, 30 October 2013 14:08
It is a Bad Practice to have production SQL that does SELECT *


if you add or modify columns your application may be broken.

select * from USER_TAB_COLUMNS


select * uses the order defined by COLUMN_ID

[Updated on: Thu, 31 October 2013 11:32]

Report message to a moderator

Re: select * from one table but not all tables in a query [message #600080 is a reply to message #600076] Thu, 31 October 2013 11:44 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Your code might need changes when adding/removing columns regardless if you use * or list column names. Personally, * versus list is six versus halh-a-dozen. I could be wrong, but I don't know any compelling reason not to use *.

SY.
Re: select * from one table but not all tables in a query [message #600084 is a reply to message #600080] Thu, 31 October 2013 12:25 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
I don't see any compelling reason not to use * either.

Of course one has to use it correctly.

Consider these two approaches:

-- Vesion 1
DECLARE
  v_a  mytab.a%TYPE;
  v_b  mytab.b%TYPE;
  v_c  mytab.c%TYPE;
BEGIN 
 SELECT a, b, c 
   INTO v_a, v_b, v_c
   FROM mytab;
END;
/

-- Vesion 2
DECLARE
  l_var mytab%ROWTYPE;
BEGIN 
 SELECT * 
   INTO l_var
   FROM mytab;
END;
/


Both of them are "safe" in regards to that they don't break when you change the table.

If you would use "select *" in the first case it would break when the table is changed, and if you use "SELECT a, b, c" in the second case it would break when the table is changed. So whether or not to use "select *" depends on what you do with the result of the select.

[Updated on: Thu, 31 October 2013 12:25]

Report message to a moderator

Re: select * from one table but not all tables in a query [message #600085 is a reply to message #600084] Thu, 31 October 2013 12:30 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>if you use "SELECT a, b, c" in the second case it would break when the table is changed.
or not break if the change was adding a new column to the table
Re: select * from one table but not all tables in a query [message #600086 is a reply to message #600085] Thu, 31 October 2013 12:35 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
>or not break if the change was adding a new column to the table

OK. Not "break at compile time", only break in the sense that a column in the rowtype is not filled as might expected further down in the code.
Re: select * from one table but not all tables in a query [message #600087 is a reply to message #600086] Thu, 31 October 2013 12:38 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
ThomasG wrote on Thu, 31 October 2013 10:35
>or not break if the change was adding a new column to the table

OK. Not "break at compile time", only break in the sense that a column in the rowtype is not filled as might expected further down in the code.


down stream code never expects the new column so, old/existing code continues to work as previous.
new column implies new requirement & new code to utilize the new column; but does not break existing functionality.
Re: select * from one table but not all tables in a query [message #600091 is a reply to message #600087] Thu, 31 October 2013 12:58 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
BlackSwan wrote on Thu, 31 October 2013 18:38
down stream code never expects the new column


It might at some point. For example I have written interface procedures that move data from system A to system B ten years ago that still work without a single change, and will continue to work as long as new columns are added in the target interface table before they are added in the source interface table.

If I had "enumerated" the columns instead of using "select *" I would have had to change that procedures 3-4 times a year to add new columns.

[Updated on: Thu, 31 October 2013 12:59]

Report message to a moderator

Re: select * from one table but not all tables in a query [message #600099 is a reply to message #600087] Thu, 31 October 2013 14:50 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
BlackSwan wrote on Thu, 31 October 2013 13:38

down stream code never expects the new column so, old/existing code continues to work as previous.
new column implies new requirement & new code to utilize the new column; but does not break existing functionality.


Exactly! So if I use * this code has a chance to break at compile time (if, for example, code uses SELECT INTO) if I missed to update it after adding a column. And if I missed the same code but was using column list I will only find out about it when customer calls me with "app doesn't work". And yes, I can describe many scenarios where using * creates an issue. As I said, six or half-a-dozen.

SY.
Re: select * from one table but not all tables in a query [message #600101 is a reply to message #599993] Thu, 31 October 2013 15:16 Go to previous message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
BlackSwan wrote on Wed, 30 October 2013 19:08
It is a Bad Practice to have production SQL that does SELECT *
Yes. I would say that programmers should project only the columns that they need. I have seen an application (written in Java, of course) where the programmers projected every column, sent the whole row back to the middle tier, and then used only one or two columns: a massive and unnecessary network hit for wide tables.
Previous Topic: LONG data type obsolete - but Oracle use them in meta data (directory)? WTF?
Next Topic: CREATE New Table from existing tables
Goto Forum:
  


Current Time: Wed Apr 24 19:08:02 CDT 2024