Medium SQL Challenge: Using GROUP BY and HAVING

365 Days of Daily Coding: Day 16

Since today is a weekend, I had extra time to complete more challenges today unlike weekdays.

Challenge 1:

Given a table ‘World’ with ‘name’, ‘continent’, ‘area’, ‘population’ and ‘gdp’, I had to find out the countries that had population larger than 25 million or area bigger than 3 million square km.

Table: World

namecontinentareapopulationgdp
AfghanistanAsia6522302550010020343000
AlbaniaEurope28748283147112960000
AlgeriaAfrica238174137100000188681000
AndorraEurope468781153712000
AngolaAfrica124670020609294100990000
Sample of the table ‘World’

Expected Output:

namepopulation area
Afghanistan2550010020343000
Algeria37100000188681000
Sample of the output

My solution:

SELECT name, population, area 
FROM world 
WHERE population > 25000000 OR area > 3000000;

It was an easy challenge easily executable by ‘SELECT’ clause with ‘WHERE’ specifying the logical conditions.

Challenge 2:

Given a table ‘courses’ with the column ‘student’ and ‘class’, I had to find out those classes with equal to or more than 5 students.

Table: courses

studentclass
AMath
BEnglish
CMath
DBiology
EMath
FComputer
GMath
HMath
IMath
Sample of the table ‘courses’

Expected Output:

class
Math
Sample of the output

My solution:

SELECT class 
FROM (SELECT DISTINCT class, student FROM courses) 
GROUP BY class HAVING COUNT(student) >= 5 
Runtime = 430 ms

This was also another easy challenge easily completed using ‘SELECT’ clause with ‘GROUP BY’ function to group all the students in a class and lastly using ‘COUNT’ to count the number of students in the group.

Other Submitted Solution from Discussions:

SELECT class FROM courses
GROUP BY class HAVING COUNT(DISTINCT student) >= 5 
Runtime = 454 ms

This is very similar to my solution above for Challenge 2. The only difference is rather than using subquery to identify the distinct students, this query directly counts the distinct number of students. What’s also seen here is that the runtime of this query is longer than the runtime of my solution which indicates subqueries optimises performance.

Challenge 3:

Given a table ‘salary’ with columns ‘id’, ‘name’, ‘sex’ and ‘salary’, I had to update the sexes in the table; change ‘f’ to ‘m’ and ‘m’ to ‘f’ without using the ‘SELECT’ statement.

Table: salary

idnamesexsalary
1Am2500
2Bf1500
3Cm5500
4Df500
Sample of the table ‘salary’

Expected Output:

idnamesexsalary
1Af2500
2Bm1500
3Cf5500
4Dm500
Sample of the output

My solution:

Update salary
Set sex = CASE sex
    WHEN 'm' Then 'f'
    WHEN 'f' Then 'm'
    ELSE sex
    END
WHERE sex in ('f','m');
Runtime: 519ms

Initially my solution didn’t work so I had to google and I found out that my ‘CASE’ syntax was not correct.

Other Submitted Solution from Discussions:

UPDATE salary SET sex = DECODE(sex,'m','f','m');
Runtime: 426 ms

The ‘DECODE’ function used here has the functionality of IF-THEN-ELSE statement.

Leave a comment