DA Preliminary Round Answers
DA Preliminary Round Answers
Question 1:
Table Name: Transactions
Account_numbe transaction_tim Transaction_i balanc
r e d e
123 1/1/2019 8:00 101 1000
123 1/2/2019 8:00 102 2000
123 1/3/2019 8:00 103 3000
789 1/4/2019 8:00 104 1000
789 1/5/2019 8:00 105 500
123 1/6/2019 8:00 106 4000
Using the above (Transactions) dataset, come up with a SQL query to get the most recent/latest balance,
transaction for each account number.
ANS:
SELECT
*
FROM
TRANSACTIONS
WHERE
TRANSACTION_ID IN (
SELECT
MAX (TRANSACTION_ID)
FROM
TRANSACTIONS
GROUP BY
ACCOUNT_NUMBER
)
Question 2:
Table Name : Student Table Name : Student_info
Studentid Subject Marks Studentid DOB Location
1 English 80 1 1/1/1993 Chennai
1 Maths 70 3 1/1/1993 Bangalore
1 Science 75
1 English 85
Expected output
2 English 35
Studentid
3 English 100
2
3 Maths 39
Using the above 2 tables, Write a SQL to get the list of students, for whom student_info table doesn’t
have data?
Note: without using “not in” Clause
ANS:
SELECT
studentid
FROM
student A
WHERE
NOT EXISTS (
SELECT
student_id
FROM
student_info B
WHERE
A.studentid = B.student_id
)
OR
ANS:
SELECT product_code,
quantity,
Inventory_date,
SUM(quantity) OVER(PARTITION BY product_code ORDER BY product_code, inventory_date ) AS Sum
FROM
product
ORDER BY
product_code,
inventory_date