FactSet Research Systems, Inc Interview Question
Software Engineer / DevelopersCountry: United States
How do you do the computations without using sql aggregate function and only using sql queries?
Huh?
If you want to find the average salary of employees under a specific manager, get the list of salaries (using the simple sql query), and find the average yourself by going through the returned results and computing the average yourself.
Might not be efficient, but that is what we get for putting idiotic constraints.
I have implemented the sum function using recursive with clause in Oracle SQL, similarly it can be modified for average (sum/count)
WITH s (iteration, num_sum)
AS (SELECT row_id iteration, xn.num
FROM ( SELECT NUM,
ROW_NUMBER () OVER (ORDER BY num) row_id
FROM xxtest_num) xn
WHERE xn.row_id = 1 -- first row in the table
UNION ALL
SELECT s1.iteration + 1, s1.num_sum + xn1.num
FROM s s1,
( SELECT NUM,
ROW_NUMBER () OVER (ORDER BY num) row_id
FROM xxtest_num) xn1
WHERE s1.iteration <= 10 -- stopping condition count of rows
AND s1.iteration + 1 = xn1.row_id)
SELECT num_sum
FROM ( SELECT *
FROM s s1
ORDER BY num_sum DESC)
WHERE ROWNUM = 1;
I can only come up with a solution in PL/SQL to do it. Were you able to do it in SQL?
- kr.neerav March 29, 2014