100% found this document useful (1 vote)
104 views3 pages

DA Preliminary Round Answers

The document contains 3 SQL questions and answers. Question 1 asks to write a query to get the most recent transaction and balance for each account number from a Transactions table. Question 2 asks to write a query to get a list of students that are not present in the student_info table using two tables, Student and Student_info. Question 3 asks to write a query to get the cumulative sum of quantity by inventory date from a Product Inventory table.

Uploaded by

Govarthanan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
104 views3 pages

DA Preliminary Round Answers

The document contains 3 SQL questions and answers. Question 1 asks to write a query to get the most recent transaction and balance for each account number from a Transactions table. Question 2 asks to write a query to get a list of students that are not present in the student_info table using two tables, Student and Student_info. Question 3 asks to write a query to get the cumulative sum of quantity by inventory date from a Product Inventory table.

Uploaded by

Govarthanan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Questions:

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.

Expected Output Account_numbe


r Transaction_id balance
123 106 4000
789 105 500

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

SELECT STUDENTID FROM STUDENT


EXCEPT
SELECT STUDENT_ID FEOM STUDENT_INFO
Question 3
Product Inventory Table
Using the above
Inventory Cumulative
inventory dataset,
Product Code Quantity Date Sum (output)
Write a SQL query to get
P1 10 1 Aug 2019 10
the cumulative sum
P1 50 2 Aug 2019 60
(mentioned above)
ordered P1 20 3 Aug 2019 80 by inventory date.
P1 10 4 Aug 2019 90
P2 200 1 Aug 2019 200
P2 350 2 Aug 2019 550
P2 400 3 Aug 2019 950
P3 600 1 Aug 2019 600
P3 250 2 Aug 2019 850

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

You might also like