0% found this document useful (0 votes)
99 views3 pages

SQL Queries for Student and Loan Data

The document contains 15 sample questions on SQL based on different relational schemas. The questions test various SQL skills like writing queries to retrieve, update, insert and delete data from multiple tables by performing joins, aggregations and other operations.

Uploaded by

optimuz prime
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
0% found this document useful (0 votes)
99 views3 pages

SQL Queries for Student and Loan Data

The document contains 15 sample questions on SQL based on different relational schemas. The questions test various SQL skills like writing queries to retrieve, update, insert and delete data from multiple tables by performing joins, aggregations and other operations.

Uploaded by

optimuz prime
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

Sample Questions on SQL

The relational schema for student course registration are as follows

Parents-S(F-NID, M-NID, Sid, F-name, M-name, street, city, income)


Student (Sid, name, street, city. Mobile, email, CGPA, age, tot-cred)
Takes (course-id, Sid, semester, year, grade)
Course (course-id, title, credit-hour)
Parents-T(F-NID, M-NID, Tid, F-name, M-name, street, city, income)
Teacher (Tid, name, designation, street, city, Mobile, email, salary, date-of-birth)
Teach (course-id, Tid, semester, year, remuneration)

Q. 1: Write SQL expression to find Sid, F-NID, M-NID, mobile, email and age of all
students whose tot-cred is greater than or equal to 130.

Q. 2: Write SQL expression to find F-NID, Sid, course-id and title for all students whose
parents live in Dhaka.

Q. 3: Write SQL expression to find Sid, name, street, city and average grade of each
student.

Q. 4: Find city and street wise average, maximum and minimum income of parents (of
students) living in Dhaka or Rajshahi and average income higher than 500000.

Q. 5: Write SQL expression to find Sid, name, course-id and title of all students who have
taken any course (course-id) taken by Abid in Fall 2018.

Q. 6: Write SQL expression to find Sid, name, course-id and title of all students who have
taken all courses taken by Abid in Fall 2018.

Q. 7: Increase salary of all teachers who taught 3 credit courses by 10%.

Q. 8. Some students have become teachers. Write SQL statement to insert id, name, street,
city, mobile and email into teacher table. Other attributes will be null.

Given the relational schema as follows:

branch(branch name, branch city, assets)


customer (customer name, customer street, customer city)
loan (loan number, branch name, amount)
borrower (customer name, loan number)
account (account number, branch name, balance )
depositor (customer name, account number)
Q. 9. Write SQL for the following:

a. Insert all the loans of branch name = “NSU” to the account relation as loan number will
be account number and amount will be balance.
b. Update the database as follows: those borrower have loans more than 50000, decrease
their loan by 10%. For other borrowers, make their loan zero.
c. Delete all loans of customers who lives in ‘Gazipur’.
d. Find the list of customer name, branch name and branch city of all customers who lives
in ‘Dhaka’.
e. Find the list of customer name, branch name and branch city of all customers who have
accounts in all branch city.
f. There are some customers who have no account and also some customers who have no
loan. Find these customer name, customer street and customer city for the above using
joins.

Given the relational schema as follows:

employee (person name, street, city)


works (person name, company name, salary)
company (company name, city)

Q. 10. Write SQL for the following:


a. Find person name, street, [Link], company name, [Link] for all employees
salary greater than 10000.
b. Find person name, street and city of all employees who live in the same city as ‘Mr. Akib’
lives.
c. Find all person name of all employees who live in the same city as the company.
d. Find the list of customer name, branch name and branch city of all customers who lives in
‘Dhaka’ as per Q5.

Q11: Given relational schema as follows: 6


employee (person-name, street, city)
works (person-name, company-name, salary)
company (company-name, city)

Create a role empl with the following privileges:


Assign insert, select and delete privileges on employee relation and works relation to the
role empl. Assign select privilege on company relation to empl. There are two users e1
and e2. Now grant the above privileges to e1 and e2 using the role.

Q12: Using the relational schema of Q3, there are some employees who do not work to 6
any company. Create a view named employee-no-company with only those employees not
working to any company.

Q13: Given the following relational schema: 5


branch(branch name, branch city, assets)
customer (customer name, customer street, customer city)
borrower (customer name, loan number)
loan (loan number, branch name, amount)

Find each customer city and the total amount of loan of that customer city. Output:
customer city, loan-amount.

Q 14: Given the following relational schema: 5


branch(branch name, branch city, assets)
customer (customer name, customer street, customer city)
depositor (customer name, account number)
account (account number, branch name, balance )

a. Delete all accounts of customers who lives in ‘Sylhet’.


b. Gove 10% profit to all accounts of customers living in Bhola and 5% profit to
customers living in Dhaka and 6% to others.

Q15: Given relational schema as follows: 5


employee (person-name, street, city)
works (person-name, company-name, salary)
company (company-name, city)

Find person-name and company-name of all employees who live in the same city where
‘Abid’ lives.

Common questions

Powered by AI

To identify customers who have neither accounts nor loans, we utilize LEFT JOIN and check for NULLs. The query is: SELECT customer-name, customer-street, customer-city FROM customer LEFT JOIN depositor ON customer.customer-name = depositor.customer-name LEFT JOIN borrower ON customer.customer-name = borrower.customer-name WHERE depositor.customer-name IS NULL AND borrower.customer-name IS NULL. This checks for the absence of customer records in both depositor and borrower tables by looking for NULLs, thus effectively identifying the required customers .

To retrieve details of customers living in Dhaka owning accounts across all branches, we use a multi-join operation. The SQL is: SELECT DISTINCT customer-name, branch-name, branch-city FROM customer JOIN depositor ON customer.customer-name = depositor.customer-name JOIN account ON depositor.account-number = account.account-number WHERE customer-city = 'Dhaka' AND NOT EXISTS (SELECT 1 FROM branch WHERE branch.branch-name NOT IN (SELECT branch-name FROM account WHERE account.account-number = depositor.account-number)). This query identifies customers by verifying ownership of accounts in every branch's city .

To create a view isolating employees not working in any company, use: CREATE VIEW employee-no-company AS SELECT person-name, street, city FROM employee WHERE person-name NOT IN (SELECT person-name FROM works). This SQL command establishes a view filtering out employees present in the works relation, hence focusing only on those unemployed based on the existing schema .

To increase the salaries of teachers who taught 3-credit courses by 10%, an SQL UPDATE statement can be executed using a subquery to filter teachers based on the 'Teach' relation. The SQL statement would be: UPDATE Teacher SET salary = salary * 1.10 WHERE Tid IN (SELECT Tid FROM Teach JOIN Course ON Teach.course-id = Course.course-id WHERE credit-hour = 3). This query finds teachers who have taught any 3-credit courses and increases their salary by 10% .

The SQL expression to find the average, maximum, and minimum income of parents in Dhaka or Rajshahi with incomes above 500,000 involves using the GROUP BY and HAVING clauses. The query is: SELECT city, street, AVG(income), MAX(income), MIN(income) FROM Parents-S WHERE city IN ('Dhaka', 'Rajshahi') GROUP BY city, street HAVING AVG(income) > 500000. This groups the parent data by city and street, aggregating income values, and applying a condition on the average income .

To adjust account profits based on customer location, a conditional SQL UPDATE statement can be used: UPDATE account SET balance = CASE WHEN customer-city = 'Bhola' THEN balance * 1.10 WHEN customer-city = 'Dhaka' THEN balance * 1.05 ELSE balance * 1.06 END FROM account, customer, depositor WHERE account.account-number = depositor.account-number AND depositor.customer-name = customer.customer-name. This query selectively increases account balances based on the customer's city .

Linking employees to companies by salary and city involves a join query with conditions. Use: SELECT employee.person-name, employee.street, employee.city, company.name, company.city FROM employee INNER JOIN works ON employee.person-name = works.person-name INNER JOIN company ON works.company-name = company.name WHERE works.salary > 10000. This SQL command leverages inner joins connecting employee, works, and company relations by shared keys, filtered by salary .

Creating a role 'empl' with given privileges involves SQL statements: CREATE ROLE empl; GRANT INSERT, SELECT, DELETE ON employee TO empl; GRANT INSERT, SELECT, DELETE ON works TO empl; GRANT SELECT ON company TO empl. Assigning this role to users 'e1' and 'e2' is done using: GRANT empl TO e1, e2. This sequence creates a role 'empl' with specific privileges and assigns this role to specified users, leveraging SQL's role-based access control functionalities .

Managing loan updates for different borrowers involves a conditional SQL UPDATE command: UPDATE loan SET amount = CASE WHEN amount > 50000 THEN amount * 0.90 ELSE 0 END FROM borrower WHERE loan.loan-number = borrower.loan-number. This query decreases the amount by 10% for loans above 50,000 and sets others to zero, efficiently handling varying borrower conditions .

To convert loans from a specific branch such as 'NSU' to account entries, for data integrity, we need to ensure that loan numbers map correctly to account numbers and amounts to balances in the 'account' relation. The SQL statement is: INSERT INTO account (account number, branch name, balance) SELECT loan number, branch name, amount FROM loan WHERE branch-name = 'NSU'. This transfers the necessary loan details into the account relation effectively, maintaining the original data structure integrity .

You might also like