oracle sql column alias - sql

I have 3 tables A, B, c and I want to join those tables. These tables have common columns like, id_no, order_no
and i want to write a query that returns all columns from all 3 tables with column name extension like tabA., tabB., tabC....I don't want to manually specify all column names. In that way i can differentiate the common columns among tables.
select tabA.id_no, tabA.order_no, tabA....., tabB.id_no, tabB.order_no,tabB..., tabC.id_no, tabC.order_no,tabC..
from A tabA, B tabB, C tabC
where tabA.id_no = tabB.id_no
and tabB.id_no = tabC.id_no
could u pls let me know how to achieve this in oracle sql.

Oracle SQL Developer can do that.
Write your * query, put your mouse over the '*'
SQL Developer offers to explode that to the fully qualified column list, click the blue text.
Ta-da.
Don't forget your WHERE clause or ANSI join in the FROM, or your DBA will explain to you what a Cartesian product is.
If your table has foreign keys, SQLDev can generate that as well.

You can do the following:
SELECT tabA.*, tabB.*, tabC.*
FROM a tabA INNER JOIN b tabB
ON tabA.id_no = tabB.id_no
INNER JOIN c tabC
ON tabB.id_no = tabC.id_no;
EDIT
If you want only to get a list of the columns associated with the three tables, and to see which column names are common among the three, then you can try something like the following:
SELECT column_name, COUNT(*), LISTAGG(table_name, ',') WITHIN GROUP ( ORDER BY table_name
FROM all_tab_columns
WHERE owner = '<table_owner>'
AND table_name IN ('A','B','C')
GROUP BY column_name;
N.B. LISTAGG() assumes you're using Oracle 11g or greater; prior to that you can use the undocumented function WM_CONCAT().
Hope this helps.

Related

SQL Inner Join w/ Unique Vals

Questions similar to this one about using DISTINCT values in an INNER JOIN have been asked a few times, but I don't see my (simple) use case.
Problem Description:
I have two tables Table A and Table B. They can be joined via a variable ID. Each ID may appear on multiple rows in both Table A and Table B.
I would like to INNER JOIN Table A and Table B on the distinct values of ID which appear in Table B and select all rows of Table A with a Table A.ID which appears matching some condition in Table B.
What I want:
I want to make sure I get only one copy of each row of Table A with a Table A.ID matching a Table B.ID which satisfies [some condition].
What I would like to do:
SELECT * FROM TABLE A
INNER JOIN (
SELECT DISTINCT ID FROM TABLE B WHERE [some condition]
) ON TABLE A.ID=TABLE B.ID
Additionally:
As a further (really dumb) constraint, I can't say anything about the SQL standard in use, since I'm executing the SQL query through Stata's odbc load command on a database I have no information about beyond the variable names and the fact that "it does accept SQL queries," ( <- this is the extent of the information I have).
If you want all rows in a that match an id in b, then use exists:
select a.*
from a
where exists (select 1 from b where b.id = a.id);
Trying to use join just complicates matters, because it both filters and generates duplicates.

how to use a column alias when selecting * from a table inner joined with another table

I am using MS Access 2010. I have inner joined two tables (2 columns from one table, and all columns from the other) and would like to use an alias on one of the columns from the table that I select all the columns. Here as an example of the current SQL
SELECT
Cars.brand, Cars.owner, *
FROM
Cars INNER JOIN Inspections ON Cars.vin = Inspections.vin
Right now when I run this, the vin column is returned as 'Inspections.vin'. I would like it to just read 'vin'. Is this possible?
Just add table name, currently you select all columns from both tables:
SELECT
Cars.brand, Cars.owner, Inspections.*
FROM
Cars INNER JOIN Inspections ON Cars.vin = Inspections.vin
You can only assign an alias to columns that are explicitly included in your select statement. Otherwise, all those within the all part of your query will return its original column name
SELECT
Cars.brand, Cars.owner, [vin] = Cars.vin, *
FROM
Cars INNER JOIN Inspections ON Cars.vin = Inspections.vin
I think this is what you're looking for since you want all the columns from Inspections. You can't use * if you want aliases; instead you will need to specify every column you want from Inspections. In my example it's as if Inspections has only two variables; just add more Inpections.whatever until you have all the columns.
SELECT Car.brand, Cars.owner, Inspections.vin as vin, Inspections.othervar
FROM Cars
INNER JOIN Inspections
ON Cars.vin = Inspections.vin

SQL merge tables with matching columns

How can I merge 2 tables with the same column names into 1 table? Something like this:
The 2nd table should fill in the 1st table.
This is as close as I got
SELECT * FROM
Animals
LEFT JOIN Best
ON Animals.species=Best.species;
http://sqlfiddle.com/#!5/d0a98/3
But it seems to concatenate the 2nd table on there.
Is LEFT JOIN really the correct way to do this?
You should list the columns in the SELECT. Then you would readily see that all you need is COALESCE():
SELECT a.price, a.species, COALESCE(b.name, a.name) as name
FROM Animals a LEFT JOIN
Best b
ON a.species = b.species;

Natural join on in SQL

TableA TableB
Column1 Column2 Column3 Column4
1 2 1 3
I have two table TableA(Column1,Column2) and TableB(Column3,Column4).I want to join two table using column1 ,column4(LIKE NATURAL JOIN). Is in SQL any things to join two table and return a new table with deleting repeated columns?
I want select this:
column1 column2 column4
1 2 3
DBMSes that support NATURAL JOIN require the column names of the join keys to match, and if you do SELECT * you will get only the unique column names. It doesn't make sense to try to specify column names, because the whole thing works by the names already being the same.
You MUST have same-named columns between the two tables, as it will use every same-named column between them to perform the join. Your tables TableA and TableB are unsuitable for a natural join as they don't share any column names.
So you are relegated to doing a regular join:
SELECT
A.*, -- you can at least get all the columns from one table
B.Column4 -- but you have to specify the rest one at a time
FROM
TableA A
INNER JOIN TableB B
ON A.Column1 = B.Column3
;
You just have to bite the bullet and write the query. You may want to not have to write the column names, but that's just not possible.
Some notes: When you say "return a new table", I think I know what you mean, but technically it is a rowset since to be a table it would have to be stored in the database with a name.
It may be possible to alias the column in a view or inline derived table, but you haven't told us what specific DBMS you're using so we can answer for its exact capabilities. It might look something like this:
SELECT
*
FROM
TableA A
NATURAL JOIN (
SELECT Column1 = Column3, Column4
FROM TableB B
) B
;
But notice that you still have to list all the other columns in TableB in order to do this. And I'm not even sure it works.
Joining two tables and querying on some or all columns doesn't return you a new table but record set. To get what you wanted try this. Below query adheres to SQL standard and thus should work on all SQL compliant databases.
SELECT ta.column1, ta.column2, tb.column4 from TableA ta INNER JOIN TableB tb ON (ta.column1 = tb.column4)
If you want to use Natural Join, you need to have same columns.
'Distinct' statement prevents repeating the similar rows too
SELECT Distinct
TableA.Column1,
TableA.Column2,
TableB.Column4
FROM
TableA INNER JOIN TableB ON TableA.Column1 = TableB.Column3

SQL inner join query returns two identical columns

Let's say I have the following SQL query:
SELECT *
FROM employee
INNER JOIN department ON employee.EmpID = department.EmpID
I wanted to ask, why I am getting two EmpID columns, and how can I get only one of those, preferably the first.
I'm using SQL server
SELECT employee.EmpID, employee.name, ...
FROM employee
INNER JOIN department ON employee.EmpID=department.EmpID
Be precise and specify which columns you need instead of using the astrisk to select all columns.
You get all columns from these two tables, that's why you have two EmpID columns. The only JOIN type that removes common column is NATURAL JOIN, which is not implemented by SQL Server. Your query would look then like this:
SELECT *
FROM employee
NATURAL JOIN department
This generates join predicates by comparing all columns with the same name in both tables. The resulting table contains only one column for each pair of equally named columns.
You're getting all columns from all tables involved in your query since you're asking for it: SELECT *
If you want only specific column - you need to specify which ones you want:
SELECT e.EmpID, e.Name as 'Employee Name', d.Name AS 'Department Name'
FROM employee e
INNER JOIN department d ON e.EmpID = d.EmpID
Don't use *. Specify the columns you want in the field list.
SELECT E.EmpID, E.EmpName -- etc
FROM employee as E
INNER JOIN department as D
ON E.EmpID=D.EmpID
As stated by others, don't use *
See this SO question for reasons why:
Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc
Essentially, the answer to your question is that the output from a SQL SELECT query is not a relation, and therefore if you do not take care you may end up with duplicate attribute names (columns) and rows.
Standard SQL has some constructs to mitigate SQL's non-relational problems e.g. NATURAL JOIN would ensure the result has only one EmpID attribute. Sadly, SQL Server does not support this syntax but you can vote for it here.
Therefore, you are forced to write out in long-hand the columns you want, using the table name to qualify which attribute you prefer e.g. employee.EmpID.