Sql query for extracting info from one table based on other - sql

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

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.

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)

Merging two different table's data by 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)

Help forming a SQL statement

I have two tables: attractions and cities. Attractions contains a column called city, which is a reference to the id in the cities table. I want to form a MySQL statement that will show me which cities have the most attractions.
I know I can do:
SELECT COUNT(*) as `number`
FROM `attractions`
WHERE `city` = XX
...to get a count of how many attractions are in one city, but is there one statement that can count all of the attractions in every city, and return the results in descending order by number of attractions?
SELECT City.Name, COUNT(Attractions.City) as AttractionCount
FROM City
LEFT OUTER JOIN Attractions ON City.City = Attractions.City
GROUP BY City.Name
ORDER BY COUNT(Attractions.City) DESC