I'm new to SQL and I am seeing a select that states SELECT something.columnName FROM Table.
I'm just wondering what the "something" is used for?
If you select from multiple tables in a single query then you can specify the table and column name you want to select. Example:
select table1.col1, table1,col2, table2.col5
from table1
join table2 on t1.id = t2.id
You can also specify alias names for tables or columns to make things shorter or if you join the same tables twice. Example:
select t1.col1 as A, t1.col2 as B, t2.col5 as C
from veryLongTableName1 t1
join veryLongTableName2 t2 on t1.id = t2.id
That is either a table name or an alias for a table name.
When joining tables, you might need to specify which table a field should be fetched from. Example:
select TableA.Name, TableB.Name
from TableA
inner join TableB on TableB.id = TableA.id
Example with aliases:
select a.Name, b.Name
from TableA as a
inner join TableB as b on b.id = a.id
It is a alias to show form wich table the column comes. You need this if you join to tables wich have a colum with the same name.
Related
Table1: column is( ID)
Table2: column is (NAME)
QUERY:
SELECT ID FROM TABLE1
SELECT NAME FROM TABLE2
NOW, I want output side by side , is there any possibility
Try this....
if this is without any condition
select id, name from table1, table2
I woder whether you mean CROSS JOIN
select id, name
from table1
cross join table2
You need to do an JOIN between tables on a value that exists in both tables. An INNER JOIN only takes rows that has a match in both tables.
Example:
SELECT tb1.id, tb2.name FROM table1 AS tb1 INNER JOIN table2 AS tb2 ON tb1.id = tb2.table1_id;
I have two tables let's say tables A and B. Each table has two columns ID and Name but the data in column Name of table A and B is different, but ID are the same, so I want to update column Name in table A with values from column Name in table B. How to achieve this? If any could be helpful
update tbl1 set tbl1.Name = tbl2.Name
from Table1 as tbl1
inner join Table2 as tbl2 on tbl1.Id = tbl2.Id
this is what your are looking for
Try with UPDATE using JOIN
UPDATE TableA
SET TableA.Name= TableB.Name
FROM TableA INNER JOIN TableB
ON TableA.ID= TableB.ID
Here you can do as join
UPDATE table_a
SET table_a.Name = table_b.Name
FROM table_a
INNER JOIN table_b ON table_a.id = table_b.id
I have two tables, t1 and t2, with identical columns(id, desc) and data. But one of the columns, desc, might have different data for the same primary key, id.
I want to select all those rows from these two tables such that t1.desc != t2.desc
select a.id, b.desc
FROM (SELECT * FROM t1 AS a
UNION ALL
SELECT * FROM t2 AS b)
WHERE a.desc != b.desc
For example, if t1 has (1,'aaa') and (2,'bbb') and t2 has(1,'aaa') and (2,'bbb1') then the new table should have (2,'bbb') and (2,'bbb1')
However, this does not seem to work. Please let me know where I am going wrong and what is the right way to do it right.
Union is not going to compare the data.You need Join here
SELECT *
FROM t1 AS a
inner join t2 AS b
on a.id =b.id
and a.desc != b.desc
UNION ALL dumps all rows of the second part of the query after the rows produced by the first part of the query. You cannot compare a's fields to b's, because they belong to different rows.
What you are probably trying to do is locating records of t1 with ids matching these of t2, but different description. This can be achieved by a JOIN:
SELECT a.id, b.desc
FROM t1 AS a
JOIN t2 AS b ON a.id = b.id
WHERE a.desc != b.desc
This way records of t1 with IDs matching records of t2 would end up on the same row of joined data, allowing you to do the comparison of descriptions for inequality.
I want both the rows to be selected is the descriptions are not equal
You can use UNION ALL between two sets of rows obtained through join, with tables switching places, like this:
SELECT a.id, b.desc -- t1 is a, t2 is b
FROM t1 AS a
JOIN t2 AS b ON a.id = b.id
WHERE a.desc != b.desc
UNION ALL
SELECT a.id, b.desc -- t1 is b, t2 is a
FROM t2 AS a
JOIN t1 AS b ON a.id = b.id
WHERE a.desc != b.desc
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types.
So, if it has same number of columns and same datatype, then use Union otherwise join only Can be used.
SELECT *
FROM t1 AS a
inner join t2 AS b
on a.id =b.id
and a.desc != b.desc
SELECT *
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
Both tables have a date_createField, so when I use select *, date_createField is returned twice. I could solve this by changing my select to:
SELECT T1.date_createField, T2.date_createField
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
But is it possible to not specify the specifik fields (keep select *), and force the table name in front of the property?
I'm having this problem because I'm joining 2 tables with a lot of columns and some columns have the same name. I would like to use select * and still have a distinction between columns present in both tables. Is this possible?
This is not possible you need to specify the column names or accept all columns. If this is a query you will execute often make a view which can be reused.
May this will help you
SELECT T1.*, T2.* FROM Table1 T1 LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
I have a table (Table1) which has a composite primary key(Column1 + Column2). I am using it as a foreign key in another table (Table2).
Now I want to a SELECT statement to select all records from Table1 and Table2.
But its returning me 0 rows, because table2 is Empty. I want all records from table1 and if it does not exist in table2, value of Columns in Table2 should be null.
I know, I only need to Join it. But I am not getting it right.
Thanks
SELECT * FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id = T2.FK
FK is your foreign key on the second table.
A Left Join will return all rows from table1 even if they don't exist in table2.
You need an outer join
SELECT *
FROM table1
LEFT OUTER JOIN table2
ON table1.column1 = table2.column1
AND table1.column2 = table2.column2
Left means preserve all rows from the left (first) table in the query.
You need a LEFT JOIN
SELECT Table1.*, Table2.*
FROM Table1
LEFT JOIN Table2 ON Table1.Column1 = Table2.Column2
Try that out.
Use LEFT JOIN for join you tables. See SQL SERVER JOINS to understand the concept.