[go: up one dir, main page]

Skip to content

Commit

Permalink
Added Aggregate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ybg345 committed Jun 8, 2019
1 parent da6330d commit c497c2d
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Problem Statement:
Show the total population of the world.
*/

-- Solution:

SELECT SUM(population) FROM world;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Problem Statement:
List all the continents - just once each.
*/

-- Solution:

SELECT DISTINCT(continent) FROM world;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Problem Statement:
Give the total GDP of Africa.
*/

-- Solution:

SELECT SUM(gdp) FROM world
WHERE continent = 'Africa';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Problem Statement:
How many countries have an area of at least 1000000.
*/

-- Solution:

SELECT COUNT(name) FROM world WHERE area >= 1000000;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Problem Statement:
What is the total population of ('Estonia', 'Latvia', 'Lithuania').
*/

-- Solution:

SELECT SUM(population) FROM world
WHERE name IN ('Estonia', 'Latvia', 'Lithuania');
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Problem Statement:
For each continent show the continent and number of countries.
*/

-- Solution:

SELECT continent, COUNT(name) FROM world
GROUP BY continent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* Problem Statement:
For each continent show the continent and number of countries with populations of at least 10 million.
*/

-- Solution:

SELECT continent, COUNT(name)
FROM world
WHERE population >= 10000000
GROUP BY continent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Problem Statement:
List the continents that have a total population of at least 100 million.
*/

-- Solution:

SELECT continent FROM world
GROUP BY continent
HAVING SUM(population) >= 100000000;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Problem Statement:
Show the total number of prizes awarded.
*/

-- Solution:

SELECT COUNT(winner) FROM nobel;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Problem Statement:
List each subject - just once.
*/

-- Solution:

SELECT DISTINCT(subject) FROM nobel;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Problem Statement:
Show the total number of prizes awarded for Physics.
*/

-- Solution:

SELECT COUNT(winner) FROM nobel WHERE subject = 'Physics';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Problem Statement:
For each subject show the subject and the number of prizes.
*/

-- Solution:

SELECT subject, COUNT(winner)
FROM nobel
GROUP BY subject;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Problem Statement:
For each subject show the first year that the prize was awarded.
*/

-- Solution:

SELECT subject, MIN(yr) FROM nobel
GROUP BY subject;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Problem Statement:
For each subject show the number of prizes awarded in the year 2000.
*/

-- Solution:

SELECT subject, COUNT(winner) FROM nobel
WHERE yr = 2000
GROUP BY subject;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Problem Statement:
Show the number of different winners for each subject.
*/

-- Solution:

SELECT subject, COUNT(DISTINCT(winner)) FROM nobel
GROUP BY subject;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Problem Statement:
For each subject show how many years have had prizes awarded.
*/

-- Solution:

SELECT subject, COUNT(DISTINCT yr) FROM nobel
GROUP BY subject;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* Problem Statement:
Show the years in which three prizes were given for Physics.
*/

-- Solution:

SELECT yr FROM nobel
WHERE subject = 'Physics'
GROUP BY yr
HAVING COUNT(winner) = 3;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Problem Statement:
Show winners who have won more than once.
*/

-- Solution:

SELECT winner FROM nobel
GROUP BY winner
HAVING COUNT(winner) > 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Problem Statement:
Show winners who have won more than one subject.
*/

-- Solution:

SELECT winner FROM nobel
GROUP BY winner
HAVING COUNT(DISTINCT subject) > 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Problem Statement:
Show winners who have won more than one subject.
*/

-- Solution:

SELECT yr, subject FROM nobel
GROUP BY yr, subject
HAVING COUNT(subject) = 3 AND yr >= 2000;

0 comments on commit c497c2d

Please sign in to comment.