Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an "exclusive" query in SQL?

Tags:

sql

database

join

I am going over a past paper for a database course I am taking and I am stuck on an SQL question

Here is the schema provided

  • Country(name, capital, area), name is the key

  • People(country, population, children, adult) where country refers to the name in Country, population is the total population, and children and adult is the percentage of the children and adult population.

  • Language(country,language,percentage) – for each language spoken in the country, it lists the percentage of the population that speaks the language.

Here is the question:

Write the following query in SQL: Find languages that are only spoken in countries whose total population exceeds 10^7.

This is what I have so far:

SELECT l.language
FROM people p, language l
WHERE l.country = p.country AND
    p.population > 10^7

The bit I am confused about is how to check that there are no other countries which a language is spoken in but the population is less than 10^7.

Any advice? Thanks

like image 890
sam Avatar asked Apr 10 '12 20:04

sam


People also ask

What is eXclusive in SQL?

The lock is a mechanism to ensure data consistency. SQL Server locks the objects when the transaction is started. The concurrency is a situation where is two users want to access the same information at the same time in the database.

Why do we use (+) in SQL query?

The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key.

Is between inclusive or eXclusive SQL?

The BETWEEN operator is inclusive: begin and end values are included.


2 Answers

Get all languages. From that set remove all languages spoken in countries with population <= 10^7. The reminder should be languages only spoken on countries with population > 10^7.

select language from languages
where language not in (
    select language from languages l
    join people p on l.country = p.country
    where p.population <= 10^7)

This works on the basis that your design has the restriction that every language must be spoken in at least one country ;)

like image 156
Mosty Mostacho Avatar answered Sep 25 '22 19:09

Mosty Mostacho


WITH T 
     AS
     (
      SELECT l.language, 
             p.country, p.population
        FROM people p, language l
       WHERE l.country = p.country
     )
SELECT language
  FROM T
EXCEPT
SELECT language
  FROM T
 WHERE population <= 10000000;

Alternatively:

SELECT language
  FROM language AS l1
 WHERE 10000000 < ( SELECT MIN(p.population)
                      FROM people p, language l
                     WHERE l.country = p.country
                           AND l.language = l1.language );

Similarly:

SELECT language
  FROM language AS l1
 WHERE 10000000 < ALL ( SELECT p.population
                          FROM people p, language l
                         WHERE l.country = p.country
                               AND l.language = l1.language );
like image 28
onedaywhen Avatar answered Sep 25 '22 19:09

onedaywhen