How to find the common values from two different tables having a common column - sql

I have two tables table1 and table2. Both tables have a common column named city.
How do I find all values under city which are in both the tables ?

You can do an inner join on the city column, to find values that exist in both tables.
select
-- Output the city from either table (since it will be the same)
t1.city
from
-- Join table1 and table2 together, on a matching city column
table1 t1 join table2 t2 on (t1.city=t2.city)
group by
-- Only return a single row per city
t1.city

SELECT tbone.desired_column1
tbone.desired_column2,
--other columns from table one
tbtwo.desired_column1,
tbtwo.desired_column2
--other columns from table two
-- Bellow we're stating what this table could be identified as (tbone and tbtwo), so that you don't have to keep typing table name above and bellow. Can be anything, such as A or B or HORSECORRECTINGBATTERY
FROM table1 tbone,
table2 tbtwo
WHERE tbone.city = tbtwo.city
If you don't want to specify which columns to take, just go with
SELECT * FROM ...

Related

Using While SQL Server

I have two tables and both contains columns Names and ID_Number.
table1 contains columns Names, ID_Number, Price_date
table2 contains columns Names, ID_Number, historical_date, comments
I am trying to do a loop such that it will start from the first value in ID_Number column in table1 and see if it matches with any value in ID_number column in table2.
If there is a match, then compare the 'Names' for the two tables for that particular ID_number. If the names does not matched, then in the comments column, enter the Name from table1 and enter the Price_date from table1 to historical_date in table2.
Don't use loops in SQL, as long as you can avoid them. SQL is a set-based language, that is not optimized for iterative processes.
From your explanation, it seems like you want an update statement with a join. This should do what you want:
update t2
set t2.comments = t1.names, t2.historical_date = t1.price_date
from table2 t2
inner join table1 t1
on t1.id_number = t2.id_number
and t1.names <> t2.names

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).)

SQL statement selecting rows from a table dependent on information from another table

I have two different tables in a database, differing in number of columns. Now, I want to select a number of rows from the first table dependent on some variable (for example that the first column should have the value 1). However, I would also like to use information from my other table to select rows from my first table.
In my specific case, both table1 and table2contains the columns Group and Person. Table1 specify each person once, and declares what group he or she belongs to. However, people can also be part of secondary groups, which are listed in table2. That is, in table2, a person can be listed again with a new group number.
I would like to write an SQL statement where I select persons (that is, rows) from table1 (since I have more information about the persons in this table) that are members of a certain group, x. However, since a person can belong to several groups, I need to look through table2 as well, somehow.
How can I write this SQL statement?
select t1.person_id,t1.group_id
from table1
union all
select t2.person_id,t2.group_id
from table2
this will give you one table
person group
person1 group1
person1 group2
person2 group3
no matter what tables they belong to.
This architecture seems silly however if the same data is in both tables.
If i have understood your question correctly, this query below will get you a person's details where the person is a member of a group 'X' and that relationship between person and that particular group 'X'is coming from a record maintained in either table1 or table2.
SELECT t1.*
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.Person = t2.Person
WHERE t1.Person = 'Y'
AND (t1.Group = 'X' OR t2.Group = 'X')
You will need some sort of identifier in both tables - like a candidate key.
When you do your select you need to join the tables, example:
SELECT column_name(s)
FROM Table1 table_name1
INNER JOIN Table2 table_name2
ON table_name1.column_name=table_name2.column_name
WHERE table_name1.person = table_name2.person
You can use JOIN clause to combine rows from two or more tables, based on a related column between them.
Let's look at a selection in this example:
SELECT
       table1.user_name,
       table2.group_name,
       table1.address
FROM table1
INNER JOIN table2
ON table1.UserID = table2.ID;

Help with Joins

my first table has about 18K records
so when i
select * from table2 i get about 18k
i'm trying to do a join on it as follows, but i'm getting like 26K back.. what am i doing wrong? i though it's supposed to return all of the "right" aka table2 records plus show me whatever value matches from the first in a separate column...
Select t1.fID , t2.*
FROM table1 t1 right join table2 t2 on t1.fName = t2.f
here is an exmaple of my tables:
table 1:
fID, fName
table 2: id, f, address, etc
i need to get all records from table 2, with an fID column, whenever f=fName
table1 has many rows with a value of fname that matches the same in table2.
Example, say 5k rows table2 have no matching rows in table1, you have a average of 2 rows in table 1 for each of the remaining 13k table2 rows
Because you have also asked for a column for table1, this will happen. You'll note multiple t1.fId values for a given t2.fname. Or NULLs
If t1.fName and t2.f aren't unique identifiers for their tables, you will find that rows from table1 are being joined with multiple rows from table2.
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).See Right Join
So it looks like you do not have your matching criteria set correctly or you have no matches.
This is possible when some fName values are repeated in Table2 and/or Table 1.
Run these Queries and See:
SELECT fName, COUNT(1) FROM Table2 GROUP BY fName HAVING COUNT(1) > 1
SELECT fName, COUNT(1) FROM Table1 GROUP BY fName HAVING COUNT(1) > 1