Query of two table with different structure - sql

I have a SQL table that shows Cities and County Table that shows counties, but the structure of tables is different so I cannot use Union. I need to show all recorded from both table that has a name for example 'Orange',
These are my 2 queries that I want in combine.
select * from [geo].[tblCounty] co
where co.CountyName like 'ORANGE%'
select * from [geo].[tblCity] c
where c.CityName like 'ORANGE%'
Table County:
City Table:
I need all county or citys that has Orange on the CountyName or CityName. basically 3 row of record with these data.

just use the same type columns instead of *
select co.CountyName from [geo].[tblCounty] co
where co.CountyName like 'ORANGE%'
union
select c.CityName from [geo].[tblCity] c
where c.CityName like 'ORANGE%'

Related

Create a additional row from join sql?

I have 2 tables: Person and House with 1-n relation.
I want to the result return as picture below:
Row always have a Person column with a null House column.
Thanks.
you can use unionall to join the result set with person table something like
select p.name,h.name as housename from person p join house h on p.id=h.personid
union all (select name,null from person)
order by name,housename

Get Unique records from two hive tables

I have two hive table's. And both tables have one column column. There are some few rows which value is same in both tables
Now my requirement is join the two tables and exclude the common records from second table based on common column.
For example,
Table a:
Name. City. Country
Devid. Hyd. Ind
Steve. London. UK
John. Bangalore. Ind
Table B
Name. City. Country
Xxxx. Xxxxx. Ind
Yyyyy. Yyyy. US
Zzzz. Zzzzz. UK
Now my required output is
Name. City. Country
Devid. Hyd. Ind
Steve. London. UK
John. Bangalore. Ind
Yyyyy. Yyyy. US
I tried following logic
Select a.* From A a union
Select t.* From (
Select c.* From table B b right join table A c on
b.country = c.coubtry
Where b.id is null) t;
This query not completing, keep on running. Any workaround needed?
Please help me out guys.
Try the below code:
SELECT * FROM A
UNION
SELECT * FROM B
WHERE Country NOT IN
(SELECT DISTINCT Country FROM A)

SQL JOIN to get the compare two values/row from table "A" and the same row from table "B"

I have two tables with the following rows
Table A (transaction)
Order Seller Customer
1 300 500
Table B (Persons)
PersonID FullName
300 Peter White
500 Scott Bold
I want a result like this
Order Seller Customer FullName (Seller) FullName (customer)
1 300 500 Peter White Scott Bold
I've tried multiple things however which makes more sense is a join a table twice, however I'm getting:
Ambiguous column name
This is SQL Server 2019.
Basically I'm looking to retrieve info from the same table instead of creating additional tables. Is that possible? If yes, how do you do? Thank you in advance.
As #jarlh wrote in comment:
select t.order, t.seller, t.customer, sel.fullname, cust.fullname
from transaction t
join persons sel -- sel is an alias to persons table
on sel.personid = t.seller
join persons cust
on cust.personid = t.customer;
Query with join will return the result as long as both seller and customer exist in persons table -- here it should as source table names transactions :).
I have another form of query it still join table B twice.
This is archaic syntax which I don't recommend but for beginner know the concept of JOIN:
select t.*,B.FullName as FullName (customer) from
(
select A.Order,A.Seller,A.Customer,B.FullName as FullName(Seller)
from A,B where A.Seller=B.PersionID
) t, B where t.Customer=B.PersionID
The proper way of JOIN:
select t.*,B.FullName as FullName (customer) from
(
select A.Order,A.Seller,A.Customer,B.FullName as FullName(Seller)
from A JOIN B ON A.Seller=B.PersionID
) t JOIN B ON t.Customer=B.PersionID
Hoping this can help you.

Select rows from table A where string exists in a similar column in table B or table C

I have 3 different SQL tables that I am working with. Here is what the table look like:
Master Accounts
---------------
CustomerNumber PK
CompanyName
More Columns...
Bill Tos
---------------
MasterCustomerNumber FK
CompanyName
Ship Tos
---------------
MasterCustomerNumber FK
CompanyName
I want to write a MS SQL query that returns all the columns in the Master Accounts table where a company name contains the string 'ama' in any of the three tables.
Here is my current SQL:
SELECT DISTINCT
MA.*
FROM MasterAccounts MA
LEFT JOIN BillTos BT
ON MA.CustNo = BT.MasterCustNo
LEFT JOIN ShipTos ST
ON MA.CustNo = ST.MasterCustNo
WHERE MA.CompanyName LIKE '%ama%' OR BT.CompanyName LIKE '%ama%' OR ST.CompanyName LIKE '%ama%'
My goal is to get all master accounts where a billto or a shipto companyname contains 'ama'.
I read yours comments and maybe this go faster:
select MA.* from
MasterAccounts MA
join
(select CustNo nmb from MasterAccounts where CompanyName LIKE '%ama%'
union
select MasterCustNo nmb from BillTos where CompanyName LIKE '%ama%'
union
select MasterCustNo nmb from ShipTos where CompanyName LIKE '%ama%' ) num on (num.nmb = MA.CustNo)
if you need more companies maybe you use company name in lower case...
*if is small error in query i'm sorry - i read this fast....

Join two tables and select a row using foreign key SQL query

I have two tables Cities and Country. I am using the query
SELECT *
FROM citiesTable
WHERE cityName LIKE 'F%'
LIMIT 30
Each cityName has their respective country joined with foreign key in country table.
I want to select each city with and their respective country like Fayzabad , Afghanistan. Which query should I use? Please mention must since I am new to SQL
Better use left join to get all city table data
select city.*, ctry.countryName
from citytable city
left join country ctry on city.countryid = ctry.countryid
Use join query
SELECT *
FROM citiesTable, country
WHERE country.country_id = citiesTable.country_id
AND cityName LIKE 'F%' LIMIT 30
Try this query:
SELECT *
FROM CitiesTable a, Country b
WHERE a.country_id = b.id
AND a.cityName LIKE 'F%'
LIMIT 30