Select SQL with multiple tables in Access 2010 - sql

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.

Related

SQL connecting 3 seperate tables using procedures

I'm struggling with SQL a bit, so far I have 3 tables:
CUSTOMER
id,
name,
surname,
gender,
birthdate
GENDER
id,
gender
ADRESS
id,
country,
city
I need to create a procedure on connecting these 3 tables into a big one, so it would have (customer id, name, gender, country)
I know that I have to use joins but I am so unfamiliar with SQL.
You should just use a JOIN we use a JOIN on 2 or more tables in order to fetch some common attributes related values of one table that exists in other as together. So, here the sql then would look like. Also you dont require the gender seperate table as you have gender already present in your customer table
Select
c.id, c.name, c.gender, a.country
From CUSTOMER c
Join
Address a
On c.id =a.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)

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)

SQL Query with a subquery

I need to retrieve the player number, name, street and town for all players that live in the same street and town as the players with number 6 or 7;
I am not sure how to write the SQL Query, i think i need to have a subquery in the WHERE clause but can't figure out how to do it.
This is what i came up with, but i have no way of testing it at this point
SELECT playerNo, name, street, town
FROM Players
WHERE street IN ( SELECT street, playerNo
FROM Players
WHERE playerNo IN (6,7));
AND town IN (SELECT town, playerNo
FROM Players
WHERE playerNo IN (6,7));
Should be compatible with Oracle 10g
Thanks to everyone who replied!
You didn't state your DBMS so this is the ANSI SQL solution (which works in Oracle, PostgreSQL, DB2, Teradata, MySQL and probably a lot of others as well):
SELECT playerNo, name, street, town
FROM Players
WHERE (street, town) IN (SELECT street, town
FROM Players
WHERE playerNo IN (6,7));
A side note regarding the IN operator:
your the expression town IN (SELECT town, playerNO ... is invalid because the subselect must the exact same number of columns as the left hand side of the IN operator. In your case you would have to write town IN (SELECT town FROM ...)
Something like this should do the trick:
select t.playerNumber, t.name, t.street, t.town
from tablename t
inner join (select street, town from tablename where playerNumber in (6,7)) aux on aux.street = t.street and aux.town = t.town
SELECT p1.playerNo, p1.name, p1.street, p1.town
FROM Players AS p1
INNER JOIN Players AS p2 ON p2.street = p1.street AND p2.town = p1.town
WHERE p2.playerNo IN (6,7)
It's often best to avoid subqueries because the database optimizer can't 'see inside the parens'.