Given the two tables below, write a select statement that will select all the country names that don't have exactly one default language.
Table A
id country_code country_name
1 CAN Canada
2 USA United States of America
3 MEX Mexico
4 IND India
5 ESP Spain
Table B
id country_code language_code is_default
1 USA en t
2 CAN fr t
3 CAN en t
4 USA es f
5 USA zh f
6 IND en f
7 MEX es t
8 IND hi f
9 CHL es f
Join the tables and GROUP BY country_name. use a HAVING clause to filter the resulting rows to just those that have multiple language codes. You don't have to include the COUNT() as a column in order to use it in the HAVING clause.
select A.country_name
from TableA as A
inner join TableB as B
on A.country_code = B.country_code
where b.is_default = 't'
group by A.country_name
having count(language_code) > 1
You're going to have to use an inner join on the "Country Code" to grab the country name. Also, you're going to have to do a count of iterations of the country name to determine the number of language_code rows present. Enjoy your Group By! :D
Related
I have two tables
Cities:
id name
------------
1 Helsinki
2 Tukholma
3 Oslo
4 Turku
Flights
id where_id to_id
---------------------
1 1 2
2 1 3
3 2 3
4 2 4
I want to get this result
Helsinki Tukholma
Helsinki Oslo
Tukholma Oslo
Tukholma Turku
How do I compose the query? Result has two name columns and I can't get around it?
You can join twice:
select c1.name where_city, c2.name to_city
from flights f
inner join cities c1 on c1.id = f.where_id
inner join cities c2 on c2.id = f.to_id
You need two joins:
select f.*, cw.name, ct.name
from flights f join
cities cw
on f.where_id = cw.id join
cities ct
on f.to_id = ct.id;
I found this solution. Pretty clear. No JOINs
SELECT A.name, B.name FROM cities A, cities B, flights F WHERE F.where_id=A.id AND L.to_id=B.id;
Performance table:
PerformanceId SingerId MovieId NumberofSongs
1 1 1 2
2 3 1 4
3 2 2 6
4 4 5 3
5 5 5 3
6 2 6 2
7 4 6 5
8 6 4 6
9 6 3 3
10 4 3 4
Singer table:
SingerId SingerName City DOB Gender
1 A Hyderabad 14-Apr-65 M
2 B Chennai 25-May-84 M
3 C Bangalore 14-Sep-78 F
4 D Hyderabad 17-Jan-70 M
5 E Hyderabad 18-Mar-87 F
6 F Bangalore 23-Aug-75 F
Movie table:
MovieId MovieName ReleaseDate
1 AAA 12-Jan-15
2 BBB 19-Sep-12
3 CCC 23-Jul-10
4 DDD 06-Oct-01
5 EEE 08-Nov-05
6 FFF 18-Apr-99
7 GGG 07-Aug-12
I would need to list the MovieId, MovieName in which both Male and Female singers are performed
(list the movieid, moviename in which male and femail singer ie both singer are performed in one movie)
Hi guys please help me out with this query i tried i am not getting exact query
Here's my query:
select * from movies a inner join performance b
on a.movie_id=b.movie_id where b.singer_id in
(select singer_id from singer where gender = 'F') c inner join
(SELECT SINGER_ID FROM SINGER WHERE gender = 'M') d
on c.singer_id=d.singer_id;
First create a view "MOVIE_ANALYSIS" where you'll have a list of all movies and if they've mixed singers or not:
CREATE VIEW MOVIE_ANALYSIS AS SELECT
M.MOVIEID ,
M.MOVIENAME,
(COUNT(DISTINCT S.GENDER) > 1) AS MIXED
FROM MOVIE M
INNER JOIN PERFORMANCE P ON P.MOVIEID = M.MOVIEID
INNER JOIN SINGER S ON S.SINGERID = P.SINGERID
GROUP BY M.MOVIEID ;
Then you'll get your final result with this query:
SELECT * FROM MOVIE_ANALYSIS WHERE MIXED IS TRUE ;
The reason I used an intermediate view is that in several SQL engines, you cannot use the computed "mixed" attribute directly in the same query with a group by. It'll keep insisting that mixed column is missing.
Validated and test on H2. I did not test on Oracle, as I've no access to it.
What you want here is the INTERSECT statement (using Oracle):
select movieId
from performance a
where exists (select 1
from singer b
where A.singerID = b.singerID
and b.gender = 'F')
intersect
select movieId
from performance a
where exists (select 1
from singer b
where A.singerID = b.singerID
and b.gender = 'M');
Then you just join the movie IDs you get back to the Movie table.
INTERSECT will only bring back IDs that are common to both queries (or however many you use).
I am using sql-server. I have two tables (simple snap shot below).
table hlds table bench
name country wgt name country wgt
abc us 30 abc us 40
mno uk 50 ppp fr 45
xyz us 20 xyz us 15
what I would like to do is calculate the differnces in the wgt columns and insert the results into another table, lets call it merge_tbl. The other thing I would like to do is in merge_tbl have a bit column where it is 1 if the company exists in the table hlds.
So I would like the result to look like below,
merge_tbl
name country wgt inHld
abc us -10 1
mno uk 50 1
xzy us 5 1
ppp fr -45 0
How do I go about doing this?
I think you need a FULL OUTER JOIN to get records from both tables. Then, you can use a INSERT INTO SELECT statement to do the insert:
INSERT INTO merge_tbl
SELECT COALESCE(h.name, b.name) AS name,
COALESCE(h.country, b.country) AS country,
COALESCE(h.wgt, 0) - COALESCE(b.wgt, 0) AS wgt,
CASE WHEN h.name IS NOT NULL THEN 1
ELSE 0
END AS inHld
FROM hlds AS h
FULL OUTER JOIN bench AS b ON h.name = b.name AND h.country = b.country
The ON clause of the JOIN operation depends on your actual requirements. I have made the assumption that records from hlds, bench tables match if both name and country fields are equal.
Demo here
Sorry for repost but this will help you understand the scnario better-
For each member, there can be two types of addresses (mail and legal- based on two diff indicators). My goal is extract both the adress and show them in one column for each member id.
TABLE1
Address_key(PK) Country City PostCode
1 UK London 1111
2 US New York 2222
3 Spain Madrid 3333
4 France Paris 4444
5 Swiss Munich 5555
Table 2
Member Key(PK) Memebr ID
1 1
2 2
3 3
4 2
Table3
Address Key Member Key Mail Ind Legal Ind
1 1 Y N
2 1 N Y
3 2 Y Y
4 4 N Y
5 4 Y N
My goal is to get the mail address(based on mail ind) and legal adress(based on legal ind) for each member id.
SO my output should be -
Member Key Member ID Country City Postcode Legal Country Legal City Legal Postcode
1 1 UK London 1111 US New York 2222
2 2 Spain Madrid 3333 Spain Madrid 3333
4 2 Swiss Munich 5555 France Paris 4444
Can anyone help how to achieve this ? I am using oracle 10m toad 9.0
Which is better to use: Inner query or simple join ?
Something like the below? With your column naming i'm a little confused with Table2 Member Key(PK) , Memebr ID and what their relationship is to Table 3 Member ID. My best guess is below:
Select [Member Key], t2.[Member Id], t1.*
FROM TABLE2 t2
INNER JOIN TABLE3 t3 on t3.[Member Key] = t2.[Member Id]
INNER JOIN TABLE1 t1 on t1.[Address Key] = t3.[Address Key]
Since you have two types of addresses, you could join twice to TABLE3, once for the Mailing address and next for the legal address.
select T2.Member_Key, T2.Member_id
,coalesce(T1A.Country,''), coalesce(T1A.City,''), coalesce(T1A.PostCode,'')
,coalesce(T1B.Country,''), coalesce(T1B.City,''), coalesce(T1B.PostCode,'')
from Table2 T2
left join Table3 T3A on T2.Member_Key=T3A.Member_Key and T3A.Mail_Ind='Y'
left join Table1 T1A on T3A.Address_key = T1A.Address_Key
left join Table3 T3B on T2.Member_Key=T3B.Member_Key and T3B.Legal_Ind='Y'
left join Table1 T1B on T3B.Address_key = T1B.Address_Key
Another way would be to join once and use a CASE expression, thus:
select T2.Member_Key, T2.Member_id
,max(coalesce(CASE T3.Mail_Ind='Y' then T1.Country Else '' End,''))
, ... etc.
,max(coalesce(CASE T3.Legal_Ind='Y' then T1.Country Else '' End,''))
, ... etc.
from Table2 T2
left join Table3 T3 on T2.Member_Key=T3.Member_Key
left join Table1 T1A on T3.Address_key = T1.Address_Key
group by Member_Key, Member_id
I am trying to do a query but I donĀ“t know how to do it.
These are the tables:
Table Hospital Table Doctor Table Work
Hid Country ic Hid ic
1 England 1 1 1
2 Spain 2 1 2
3 France 3 1 3
4 England 4 2 4
5 China 5 4 5
Result that I want:
Country Average of Doctors Working on that Hospitals of that Country
England 2 (the doctor with ic 1, 2, 3, and 4/number of hid)
Spain 1
France 0
China 0
I tried:
SELECT DISTINCT H.country, AVG(D.ic)
FROM Hospital H, Doctor D
WHERE H.hid IN
( SELECT W.hid
FROM Work W
WHERE W.ic IN
( SELECT COUNT(D.ic)
FROM D Doctor ....
)
)
GROUP BY(H.country);
Try this
select H.Country, count(W.ic) / count(distinct H.hid) as [Average]
from Hospital as H
left outer join Work as W on W.Hid = H.Hid
group by H.Country
SQL FIDDLE
First get the number of doctors for each hospital, then get the average over that:
select country, avg(docs) as avg_doctors_working
from (
select country, h.hid, count(*) as docs,
from hospital h
left join work w on w.hid = h.hid
group by 1, 2) x
group by 1;