SQL Question Bank - Mysql
SQL Question Bank - Mysql
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
M
1
PAINTER paints PAINTING
M
PaintingTheme
Address Contact_No
display
Gallery_No Gallery_address
1
Gallery_Name GALLERY
CLOSE cur;
END//
Question: 5
DELIMITER ;
Consider the relational database :
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;
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.
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)
Question: 10
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)
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).
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
Question: 15
Question: 16
Consider the following Relations.
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.
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.
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
Question: 18
Question: 19
Question: 19