Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
225 views

SQL Question Bank - Mysql

Uploaded by

dkmishra2701
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

SQL Question Bank - Mysql

Uploaded by

dkmishra2701
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Question: 1

Convert the following ER diagrams into Relational Models. Write DDL statement for
all and insert data into it. Loan_ty
Loan_Id pe
Cust_Id Cust_Na

M
1 LOAN
CUSTOME Borr
RR
Amount
Address

Has
Relatio

Dep_No

DEPENDE Dep_Name
NT

Question: 2
Convert the following ER diagrams into Relational Models

Painter_Cd Painter_Name Painting_id Painting_Desc

M
1
PAINTER paints PAINTING
M
PaintingTheme
Address Contact_No
display
Gallery_No Gallery_address
1

Gallery_Name GALLERY

Sumitra Jakhete [pict] Page 1


Question: 3
Book = { Book_No ,Book_Name, Author_name , Cost, Category}
Member = { M_Id , M_Name ,Mship_type, Fees_paid,Max_Books_Allowed,
Penalty_Amount }
Issue ={Lib_Issue_Id , Book_No , M_Id, Issue_Date, Return_date}
DELIMITER //
 List top 5 books which are issued by Annual members
CREATE PROCEDURE  List the names of members who has issued the books whose cost is more than
proc1()
300 rupees and whose author is “Scott Urman”
BEGIN
DECLARE salary INT;  Write a query to display number of booked in each category of books issued by
DECLARE high_salary all member types.
INT DEFAULT 0;

DECLARE done INT


DEFAULT 0;
DECLARE cur CURSOR
FOR SELECTQuestion:
salary FROM4
Emp;
1) Create a table ‘emp’ with the following columns by assuming suitable data type
DECLARE CONTINUE
HANDLER FOR NOT and size with correct syntax in SQL.
FOUND SET done = 1;
Emp–id, Ename, City, State, Salary, Age, Hire_ date.
OPEN cur;
Give an expression in SQL to solve each of the following queries :
read_loop: LOOP i) Find the names of all employees whose name starts with ‘Sa’.
FETCH cur INTO salary; ii) List all the employees name and salary whose age is less than 40 years.
IF done = 1 THEN
LEAVE read_loop; iii) Select the employees whose salary is between Rs. 20000 and Rs. 30000.
END IF; iv) Write a procedure to display highest salary of an employee without using
IF salary > high_salary aggregate functions.
THEN
SET high_salary =
salary;
END IF;
END LOOP;

CLOSE cur;
END//
Question: 5
DELIMITER ;
Consider the relational database :

dept (dept-no, dname, LOC)


emp (emp-no, ename, designation,sal) 1 --> MANY means add foreign key in the emp table
project (proj-no, proj-name, status)

dept and emp are related as 1 to many.


project and emp are related as 1 to many.
Write relational or sq 1 expressions for the following :

i) List all employees of ‘INVENTORY’ department of ‘PUNE’ location.

Sumitra Jakhete [pict] Page 2


ii) Give the names of employees who are working on ‘Blood Bank’ project.
iii) Give the name of managers from ‘MARKETING’ department.
iv) Give all the employees working under status ‘INCOMPLETE’ projects.
v)Write a Function that take Employee Number and return all the information related to
the employee working on the project.
vi) Write a Procedure block that updates the salaries of the employees as per the
following rules.
DELIMITER //
 If the designation is CLERK and deptno is 10 then increase the salary by 20%
CREATE PROCEDURE proc1()  If the designation is MANAGER and deptno is 20 then increase the salary by 5%
BEGIN  For all the other cases increase the salary by 10%
DECLARE salary DECIMAL(10, 2);
DECLARE designation VARCHAR(20);
DECLARE done INT DEFAULT 0;
DECLARE deptno INT;

DECLARE c1 CURSOR FOR


Question : 6 SELECT sal,
designation, dept_no FROM emp;
DECLARE CONTINUE HANDLER FOR
NOT FOUND SET done = 1;
Write SQL Create table statements to create the following schema. Include all appropriate
OPEN c1; primary and foreign key declarations. Choose appropriate types for each attribute.

read_loop: LOOP
a. remotecentre(centreId, college, town, state)
FETCH c1 INTO salary, designation,
deptno; b. person(ID, name, email)
IF done = 1 THEN c. programme(progId, title, fromdate, todate)
LEAVE read_loop;d. coordinator(ID, progId, centreId)
END IF; e. participant(ID, progId, centreId)
IF designation = 'CLERK' AND deptno =
10 THEN
SET salary = salary + (salary * 0.2);
ELSEIF designation = 'MANAGER' AND
deptno = 10 THEN
SET salary = salary + (salary * 0.05);
END IF;

-- Additional logic or actions can be


added here as needed

END LOOP;

CLOSE c1;
END //
Question: 7 SELECT PNO, PNAME, COUNT(EMPNO) AS
DELIMITER ; Employees_Working
A database consists of following tables. FROM PROJECT
PROJECT(PNO, PNAME, CHIEF) JOIN ASSIGNED ON PROJECT.PNO = ASSIGNED.PNO
EMPLOYEE(EMPNO, EMPNAME) GROUP BY PNO, PNAME;
ASSIGNED(PNO,EMPNO)
A.Get count of employees working on project.
B. Get details of employee working on project pr002.
C. Get details of employee working on project DBMS.
D. Write a trigger to delete all corresponding records from assigned table if employee id
deleted.
E. Write a trigger to keep back up of assign table records if project is deleted.

Sumitra Jakhete [pict] Page 3


Question: 8
Employee = (emp_no, emp_name, hiredate, comm., netsal ,dept_no, , designation)

1. Display all the employee details in department 30.


2. List the names, numbers and departments of all clerks.
3. Find the employees whose commission is greater than their salaries.
4. Find the employees whose commission is greater than 60% of their salaries.
5. List the name job and salary of all the employees in department 20 who earn more than
2000/-.
6. Find all the clerks in department 30 whose salary is greater than 1500/-.
7. Find all employees whose designation is either manager or president.
8. Find all managers who are not in department 30.
9. Find all the details of all the managers and clerks in department 10.
10. Find the details of the managers in department 10 and all clerks in department 20.

Question: 9
Employees (Employee_id,first_name , last_name , email, ph_no , hire_date, job_id,
Salary, department_id )
Works(Employee_id,manager_id)
Departments (Department_id,dept_name , location_id)
Jobs (Job_id, job_title ,min_salary , max_salary)
Locations (Location_id , street, city, state , country)
Job_history(Employee_id , hire_date, leaving_date, salary, job_id, department_id)

1] Display all the employees in descending order of their salary.


2] Display employee_id, full name and salary of all employees who have joined in year 2006
according to their seniority.
3] List name of all departments in location 20,30 and 50
4] Display the full name of all employees whose first_name or last_name contains ‘a’
5] Find the department with the most employees.
6]Display the department id and the count of the total no. of employees in respective departments
in descending order by department id if count is > 5
7] Find those departments whose employees earn a higher salary, on average, than the average
salary at department id 30.
8] List previous details of all employees who changed their department.
9] Display the manager_id and salary of the lowest paid employee for that manager. Exclude
anyone whose manager is not known. Exclude any group where the minimum salary is less than
$5000. Sort the output in descending order of salary.
10] Write a procedure that accepts deptno value from a user, selects the maximum salary
and minimum salary paid in the department, from the EMP table

Question: 10

Sumitra Jakhete [pict] Page 4


Create following tables in oracle
Emp(eno, ename, sal, contact_no, addr, dno)
project(pno, pname)
dept(dno, dname, loc)
assigned_to(eno, pno)

Write the SQL queries:


1. Gather details of employees working on project 353 and 354.
2. Obtains the details of employees working on the database project.
3. Find the employee nos of employees who work on at least one project that employee
107rks on.
4. Find the employee no of employees who work on all of the projects that employee 107
works on.
5. Find the project with minimum no of employees.
6. Create view to store pno, pname and no of employees working on the project.
7. Write a procedure to display details of the employees working on particular project. Use
cursor.
8. Write a function to count no of employees working on particular project without using
aggregate function.

Question: 11
Consider the following table
parts (pno, pname, qty_in_hands, price, level)

1. Write trigger on table for update command. Old record must be stored in one log file. Log
file having fields(pno,date,old_price,new_price)
2. Write procedure that update price of pno by 80% of its old price if qty_in_hand is more
than 200 otherwise by 70%.

Question: 12
instructor (ID ,name , dept_name, salary )
student (ID, name, dept_name , tot_cred )
takes (ID ,course_id , sec_id, semester,year , grade )
course(course_id,title , dept_name , credits )
classroom (building,room_number,capacity)
advisor(s_id,i_id)
Prereq(course_id,prereq_id)
Department(dept_name,building,budget)
Section(course_id,sec_id,semester,year,building,room_number,time_slot_id)
Teaches(id,course_id,sec_id,semester,year,grade)
Time_slot(time_slot_id,day,start_time,end_time)

Sumitra Jakhete [pict] Page 5


1. Find the number of instructors who have never taught any course.
2. Find the total capacity of every building in the university
3. Find the maximum number of teachers for any single course section
4. Find all departments that have at least one instructor, and list the names of the
departments along with the number of instructors; order the result in descending order
of number of instructors.
5. As in the previous question, but this time you shouold include departments even if they
do not have any instructor, with the count as 0
6. For each student, compute the total credits they have successfully completed, i.e. total
credits of courses they have taken, for which they have a non-null grade other than 'F'. Do
NOT use the tot_creds attribute of student.
7. Find the number of students who have been taught (at any time) by an instructor named
'Srinivasan'. Make sure you count a student only once even if the student has taken more
than one course from Srinivasan.

8. Find the name of all instructors who get the highest salary in their department.
9. Find all students who have taken all courses taken by instructor 'Srinivasan'. (This is the
division operation of relational algebra.) You can implement it by counting the number of
courses taught by Srinivasan, and for each student (i.e. group by student), find the
number of courses taken by that student, which were taught by Srinivasan. Make sure to
count each course ID only once.
10. Find the total money spent by each department for salaries of instructors of that
department.

Question 13
You need to create a movie database.

Create three tables, one for SELECT m.title AS movie_title, COUNT(ar.AID) AS number_of_roles
FROM movies m
JOIN actor_role ar ON m.MID = ar.MID
actors(AID, name), JOIN actors a ON ar.AID = a.AID
movies(MID, title) and WHERE a.name = 'Charlie Chaplin'
GROUP BY m.MID, m.title;
actor_role(MID, AID, rolename).

Sumitra Jakhete [pict] Page 6


Use appropriate data types for each of the attributes, and add appropriate primary/foreign
key constraints.

1. Insert data to the above tables (approx 3 to 6 rows in each table), including data for actor
"Charlie Chaplin", and for yourself (using your roll number as ID).
2. Write a query to list all movies in which actor "Charlie Chaplin" has acted, along with the
number of roles he had in that movie.
3. Write a query to list all actors who have not acted in any movie
4. List names of actors, along with titles of movies they have acted in. If they have not acted
in any movie, show the movie title as null. (Do not use SQL outerjoin syntax here, write
it from scratch.)

Question: 14
Consider the relational database

Supplier (sid, sname, address)


Parts (pid, pname, color)
Catalog (sid, pid, cost)
Write SQL queries for the following:
i) Find names of suppliers who supply some red parts.
ii) Find names of all parts whose cost is more than Rs. 25
iii) Find name of all parts whose color is green.
iv) Find name of supplier and parts with its color and cost.
2 )SELECT pname FROM Parts WHERE pid IN (SELECT pid FROM Catalog where cost > 25);

4)SELECT sname, pname, color, cost


FROM Supplier
JOIN Catalog ON Supplier.sid = Catalog.sid
JOIN Parts ON Catalog.pid = Parts.pid;

Question: 15

Consider the relational database

Employee (person-name, street, city)


works (person-name, company-name, salary)
Company (company-name, city)
Manages (person-name, manager-name)

Sumitra Jakhete [pict] Page 7


Consider the above relational database.
Write SQL queries for the following:
1. Find the names, street address, and cities of residence of all employees who work for First
Bank Corporation and earn more than $10,000 per annum.
2. Find the names of all employees in this database who live in the same city as the company
for which they work.
3. Find the names of all employees who live in the same city and on the same street as do their
managers.
4. Write a Trigger on update of employee company_name

Question: 16
Consider the following Relations.

It defines the schema of the database application for a library.

Book (Book_ISBN [pk], Title, Publisher_Name [fk])

BOOK_AUTHORS (Book_ISBN [pk, fk], Author_Name [pk])

PUBLISHER (Name [pk], Address, Phone)

BOOK_COPIES (Book_ISBN [pk, fk], Branch_ID [pk, fk], Num_Copies)

BOOK_LOANS (Book_ISBN [pk,fk], Branch_ID [pk, fk], Card_Num

[pk, fk], Date_Out, Date_Due)

LIBRARY_BANCH (Branch_ID[pk], Branch_Name, Address)

BORROWER (Card_Num [pk], Name, Address, Phone)

Write SQL query for the following:

1) List the ISBN and title of all books written by “John Smith”.

2) List the ISBN and title of all books written by “John Smith” as the onlyauthor.

3) List the Card number and name of all borrowers who checked out twoor more books on
06/16/2014.

4) List the branch ID and name of all library branches that have at least one copy of all the books.

5) Write a Trigger on Insert to convert the name into capital letters.

Sumitra Jakhete [pict] Page 8


6) Write a procedure which will calculate books borrow date exceed. Display that Books and
name.

Question: 17
Consider the following Relations.

It defines the schema of the database application for a bank. It manages the branches and
customers of the bank.

Customers take loans (borrow money) or open accounts (deposit money) at one or more branches.

Branch (B_No, B_name, B_city, asset),


Customer (C_No,C_Name, C_city street)
Loan(Loan_no, B_name, amount),
Account (Acc_No, B_name, Balance)
Borrower (C_No, Loan_No),
Depositor (C_No, Acc_No)

Write SQL query for the following:

1) Find the names and address of customers who have a loan.

2) Find loan data, ordered by decreasing amounts, then increasing loan numbers.

3) Find the pairs of names of different customers who live at the same address but have accounts
at different branches.

Write a Function that calculate total account balance and loan amount for a particular branch

Write a trigger which keeps track of updated balance.

Question: 18

Consider following database:


Student (Roll_no, Name, Address)
Subject (Sub_code, Sub_name)
Marks (Roll_no, Sub_code, marks)
Write following queries in SQL:
Sumitra Jakhete [pict] Page 9
i) Find average marks of each student, along with the name of student.
ii) Find how many students have failed in the subject “DBMS”.
iii) Write a Trigger that check the rollno must be start with ‘TE’.

Question: 19

Consider following database:


Student (Roll_no, Name, Address)
Subject (Sub_code, Sub_name)
Marks (Roll_no, Sub_code, marks)
Write following queries in MySQL:
i) Find average marks of each student, along with the name of student.
ii) Find how many students have failed in the subject “DBMS”.
iii) Find the students who get marks greater than 75 and and also find student who get
less than 40.
iv) Find the student whose addresses are ‘PUNE’.

Question: 19

Consider following database:


Teacher (Id, Name, Address, Dept, Salary)
i) Insert appropriate data in MySQL:
ii) Find out teachers whose address is ‘pune’ and salary=25000
iii) Count the no.of teachers whose salary greater than 75000
iv) Find the average salary package of IT Dept teachers.

Sumitra Jakhete [pict] Page 10

You might also like