Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

SQL Coding Test Data Analyst

Question 1
You are given two tables: Students and Grades. Image attached.
Joanna gives Soha a task to generate a report containing three columns: Name, Grade and Mark. Joanna
doesn't want the NAMES of those students who received a grade lower than 8. The report must be in
descending order by grade -- i.e. higher grades are entered first. If there is more than one student with
the same grade (1 - 10) assigned to them, order those particular students by their name alphabetically.
Finally, if the grade is lower than 8, use "NULL" as their name and list them by their grades in descending
order. If there is more than one student with the same grade 1 7 assigned to them, order those
particular students by their marks in ascending order.

Write a query to help Soha.

Print "NULL" as the name if the grade is less than 8.

Answer 01:
SELECT IF(GRADE < 8, NULL, NAME), GRADE, MARKS FROM STUDENTS AS S JOIN GRADES AS G
WHERE S.MARKS BETWEEN G.MIN_MARK AND G.MAX_MARK
ORDER BY G.GRADE DESC, S.NAME ASC;

Question 2

SQL Coding - Data Analyst 1


Let’s say you have two SQL tables: authors and books. The author’s dataset & Book's dataset has 1M
rows;

Create an SQL query that shows the BOTTOM 5 authors who sold the most books in total!

Both the tables have above 1M rows, however the image shows only the first 6.

Answer 02:
SELECT authors.author_name, SUM (books.sold_copies) AS sold_sum
FROM authors
JOIN books
ON books.book_name = authors.book_name
GROUP BY authors.author_name
ORDER BY sold_sum ASC
LIMIT 5;

Question 3:

You are given a table, Functions.


Two pairs X1, Y1 and X2, Y2 are said to be symmetric pairs if X1 Y2 and X2 Y1.

SQL Coding - Data Analyst 2


Write a query to output all such symmetric pairs in ascending order by the value of X.
List the rows such that X1 Y1.

Answer 03:
SELECT f1.X, f1.Y FROM Functions AS f1
WHERE f1.X = f1.Y AND (SELECT COUNT(*) FROM Functions WHERE X = f1.X AND Y = f1.Y) > 1
UNION
SELECT f1.X, f1.Y from Functions AS f1
WHERE EXISTS(SELECT X, Y FROM Functions WHERE f1.X = Y AND f1.Y = X AND f1.X < X)
ORDER BY X;

Question 4

You have two SQL tables! The first one is called employees and it contains the employee names, the
unique employee ids and the department names of a company. Sample 1 attached.

The second one is named salaries. It holds the same employee names and the same employee ids – and
the salaries for each employee in Dollars. Sample 2 attached.

The company has 546 employees, so both tables have 546 rows.

SQL Coding - Data Analyst 3


Print every department where the average salary per employee is lower than AED 1835!
Exchange Rate $1 AED 3.67.

Answer 04:
Select *
From (Select department_name, employee_id, employee_name, round (salary*3.67, 0) as 'Salary in AED'
from sample1, sample2) t where ['Salary in AED'] <1835;

SQL Coding - Data Analyst 4

You might also like