Easy SQL Challenge : Using FLOOR()

365 Days of Daily Coding: Day 21

I have finished all the free leetcode challenges except for 1 hard challenge which I am saving it for much later to try when I can solve it without looking for any solution in the forums.

I did a hacker rank challenge today, an easier one. I got to learn about a new function called FLOOR() that is available in Oracle.

Challenge: Given a table called ‘CITY’, I had to find the average population of all cities and the average needed to be rounded down.

FieldType
IDNUMBER
NAMEVARCHAR2(17)
COUNTRYCODEVARCHAR2(3)
POPULATIONNUMBER
Table ‘CITY’

My solution:

SELECT  FLOOR(AVG(Population)) FROM City;

The FLOOR() function returns the largest integer value not greater than a number specified as an argument. Source It is used when there is a need to round down numbers.

FLOOR(4.9)
Output --> 4

FLOOR(-4.9)
Output --> -5

The FLOOR() function basically rounds down the number to the nearest integer that is lesser than the number specified in the arguement.

I was working on a development feature for one of my stories where I had to insert a value for a specific column in an existing table.

I used the ‘UPDATE’ clause with the ‘WHERE’ clause to insert the value into a specified record and a column.

UPDATE table
SET column1 = value1
column2 = value2
WHERE conditions;

Leave a comment