Merging two different table's data by SQL - sql

I have a two different tables, Country_m and State_m
the State Table has fields like StateId, Name, CountryId, etc, and Country Table has fileds like CountryId, Name, Currency, etc.
I want to get a datagrid of State Table in which it should print State Name and respective Country Name... how to execute this query?

SELECT S.NAME as STATE_NAME,C.NAME COUNTRY_NAME
FROM STATE_M S JOIN COUNTRY_M C
ON S.COUNTRYID=C.COUNTRYID;

select t1.Name state_name, t2.Name Country_name from State_m t1,Country_m t2 where t1.CountryId=t2.CountryId;
use thsi

try this
select s.name as STATENAME,c.name AS COUNTRYNAME from state s
inner join country c
on s.countryid=c.countryid

It's a join. It might be a good idea to learn about them first:
http://en.wikipedia.org/wiki/Join_(SQL)

Related

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

Select SQL with multiple tables in Access 2010

Two database tables:
Continent table with ContinentID and ContinentName columns
City table with CityID, CityName and ContinentName columns
Situation:
I want to combine the corresponding city to its continent. Like Europe (continent) has Denmark (country).
However, what's wrong with my SQL statement?
select
CountryID, CountryName
from
Country
where
Country.ContientID = Contient.ContientID;
You actually need to join the tables
select CountryID, CountryName
from Country
inner join Contient on Country.ContientID=Contient.ContientID
You were probably trying the old, legacy implicit join syntax which would work like this
select CountryID, CountryName
from Country, Contient
where Country.ContientID=Contient.ContientID
but you should not use that any more.

Creating table using select statement from multiple tables

I have this university task to create table using SELECT statement from multiple tables, but it's not as simple... here's basic info:
I'm using 2 tables -
CITY(city_ID, name);
PERSON(person_ID, name, surname, city_ID);
--city_ID is FK indicating in which city person was born.
Now my task is to create new table
STATISTICS(city_ID, city_name, number_of_births);
--number_of_births is basically a count of people born in each city
Problem is that I have to use only SELECT statement to do so.
I've tried something like this: (I'm well aware that this cannot possibly work but as to give you a better idea where I'm stuck)
CREATE TABLE Statistics AS
(SELECT city.city_ID, city.name as "city_name", number_of_births AS
(SELECT COUNT(*) FROM person WHERE person.city_id = city.city_id)
FROM city, person);
For SQL Server you can do SELECT * INTO. Something like this:
SELECT
*
INTO Statistics
FROM (
SELECT
city.city_ID,
city.name as "city_name",
(SELECT COUNT(*) FROM person WHERE person.city_id = city.city_id) as 'number_of_births'
FROM city
inner join person on city.city_id = person.city_id
) t1
(Posted on behalf of the question author).
Ok, this got really messy. Dave Zych's answer was correct when rewritten in Oracle dialect.
CREATE TABLE Statistics AS SELECT * FROM (
SELECT DISTINCT
city.city_ID,
city.name AS "City_name",
(SELECT COUNT(*) FROM person WHERE person.city_ID = city.city_ID) AS "number_of_births"
FROM city INNER JOIN person ON city.city_ID = person.city_ID);

Sql query for extracting info from one table based on other

I have two tables
Country with countryid and countryname
City with cityid and cityname
I want to extract city names based on the countryid I select.
I'm very new to sql database and please help me with direct query if you can.
Thank you very much.
According to the table structure which you showed, this query is not possible. You need to add a 'country_id' to the cities table so that you know in which country is each city. Once this has been done, your query would be
select cities.cityname, countries.countryname
from cities inner join countries on countries.country_id = cities.country_id
order by countries.countryname, cities.cityname
First you should have countryid in city table as a country can have many cities. Then you can write.
select A.cityname from city A
where
A.countryid in (select B.countryid from country B)
Please check this link if you want to find out more.
SQL: Select from one table matching criteria in another?
You have to add a countryid in City table and write the following query
select * from city c left outer join country co on c.countryid=co.countryid;
You need to have the countryid in the city table.
SELECT cityid, cityname FROM city WHERE countryid = $CounrtyID

Modifying a SELECT query

I have a query like this:
SELECT initials, name
FROM employee e, projects p
WHERE e.country = p.country
Until now, both tables used an abbreviation for the country columns. Like "SWE" for Sweden and "ITA" for Italy.
In the future, the employee table will use names for the country columns. Like "Sweden" and "Italy".
Is it somehow possible to change my query so it can match abbreviations with names? Like "SWE" = "Sweden" and "ITA" = "Italy".
Thanks.
It would be better to have an own country table and the other tables referencing to that.
country table
-------------
id
name
abbreviation
I'd say the best solution is creating a third table where you match the current abbreviation with the full country name. You can then join both tables on that.
CountryTable (countryAbbreviation, countryName)
The select would then be something like this:
SELECT initials, name
FROM employee e JOIN countryTable c ON c.countryName = c.country
JOIN projects p ON p.country = c.countryAbbreviation
Although I fafor the solution by juergen, another solution will be altering the two tables to the new format.
UPDATE employee
SET country = "SWEDEN"
WHERE country = "SWE"
Do this for all the countries you have.
Always in country table, if country name starts with first three letters of the country in the employee table then you can use substring operator
SELECT initials, name
FROM employee e, projects p
WHERE upper(substring(e.country,1,3)) = upper(p.country)