Replacing the values in a column in select query - sql

I have a table where one of the columns is ids as foreign key to another table. how can I replace the ids with data from the other table inside select query?

You use a join:
select t1.column_one, t2.display_value
from table_one t1
join table_two t2 on t1.fk_column_to_table_two = t2.primary_key_column;

Related

Select all columns from table which is a result of joining two tables

I'm wondering if it's possible to select all columns belonging to one table from result table which is a join of two tables:
CREATE TABLE TABLE_AB
AS (SELECT TABLE_A.*,TABLE_B.* FROM TABLE_A NATURAL JOIN TABLE_B);
And I'd like to SELECT TABLE_A FROM TABLE_AB;
Is this possible in Oracle?
Based on your comment, you could create TABLE_AB like this:
CREATE TABLE TABLE_AB
AS (SELECT TABLE_A.* FROM TABLE_A NATURAL JOIN TABLE_B);
Now it is a copy of TABLE_A but containing only the rows you want to delete. You can reinstate those rows later using:
insert into table_a select * from table_ab;

Creating a table out of two tables in SQL

I'm trying to create a table based off of two tables. For example, I have a column in one table called Customer_ID and a column in another table called Debit_Card_Number. How can I make it so I can get the Customer_ID column from one table and the Debit_card_number from the other table and make a table? Thanks
Assuming Two Table Names as TableOne and TableTwo and CustomerID as a common Attribute.
CREATE TABLE NEW_TABLE_NAME AS (
SELECT
TableOne.Customer_ID,
TableTwo.Debit_Card_Number
FROM
TableOne,
TableTwo
Where
tableOne.CustomerID = tableTwo.CustomerID
)
Look into using a join. Use Left Join to give you the id, even if there isn't a matching card number for that id. Your value to match on will probably be the id, assuming that value is in the table with the card number
create table joined_table as(
select t1.customer_id, t2.debit_card_number
from t1
inner join t2
on t1.matchValue = t2.matchValue
)

What will be the column name after joining two tables in SQL with different join columns

Let's say I write the query
SELECT * FROM table1 JOIN table2 ON table1.ID = table2.student.Number
There are the same values in column "ID" of table1 and in column "Number" of table2.
How will the column with the joined values be named in the result table?
"ID" or "Number"?
Both of them. JOIN does not remove common columns - indeed, if you're doing a join on two tables which share a column name, the result will have two columns with that name. (You can disambiguate them using the table name, e.g. SELECT table1.id FROM table1 JOIN table2 ON (table1.x = table2.y).)

select a column in foreign key

Suppose I have two tables in SQL where
table1 has columns - id, name, sex.
table2 has columns - eid, group, hours.
column 'eid' is the foreign key in table2 and column 'id' is the primary key on table1.
suppose I conduct a search using 'select * from table2 where hour=x' in table 2 and get some results. How can I list the names of the people (from table1) associated with the ids from the search?
GOt it!
SELECT T1.NAME FROM TABLE2 T2
INNER JOIN TABLE1 T1 ON (T1.ID=T2.EID)
WHERE T2.HOUR=X

copying fields from multiple tables to create new table

How to create a table by copying only the columns from multiple tables??
eg. t1 with fields (A1,A2,A3)
t2 with fiedls (D1,D2,D3)
t3 with fields (Z1,Z2,Z3)
Now I've to create a new table using only the fields not the values from the above three tables i.e.
new_tbl (A1,A2,A3,D1,D2,D3,Z1,Z2,Z3)
How can I do it??
try this:
create table new_tbl as
select *
from t1
join t1 on 1=2
join t3 on 1=2
the condition 1=2 is always false.. so it will not return any data but the column header..