Professional Documents
Culture Documents
SQL Coding Test 01
SQL Coding Test 01
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.
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
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:
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.
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;