Ralgebr
Ralgebr
Ralgebr
2. Construct a view for the above relations which has the information about suppliers and the parts they
supply. The view contains the S#, SNAME, P# , PNAME renamed as SNO, NAME, PNO, PNAME.
(i) Find the names of all employees who work for ‘First Bank Corporation’.
(ii) Find the names and company names of all employees sorted in ascending order ofcompany
name and descending order of employee names of that company.
(iii) Change the city of First Bank Corporation to ‘New Delhi’
(i) Retrieve the names of all students majoring in ‘CS’ (Computer Science).
(ii) Retrieve the names of all courses taught by Professor King in 1998
(iii) Delete the record for the student whose name is ‘Smith’ and whose student
number is 17.
(iv) Insert a new course <’Knowledge Engineering’, ‘CS4390’, 3, ‘CS’>
(i) List the names of passengers who are travelling from the start to the destination
station of the train.
(ii) List the names of passengers who have a return journey ticket.
(iii) Insert a new Shatabti train from Delhi to Bangalore.
(iv) Cancel the ticket of Tintin.
Ans: (i) Select name, regno from PHYSICIAN where city = ‘Delhi’;
(ii) Select pname, city from PATIENT,VISIT where PATIENT.pname=VISIT.pname
anddate_of_visit = ’31-Aug-04’;
(iii) select name, count(*) from PHYSICIAN, VISIT
wherePHYSICIAN.regno = VISIT.regno group by
Physician .regno;
Ans: (i) Select Fname, Lname from STAFF, BRANCH where STAFF.bno =
BRANCH.bno and street = ‘163 main street’
(ii) Select Fname, Lname from STAFF where salary >( select max (salary) from
Staff where bno=’B3’
8. (i) Consider employee (e_no, e_name, e_salary, d_code), dept (d_code, d_name) and
dependent (depndt_name, e_no, relation). Show the names of employees in purchase and accounts
departments with at least one dependent.
(ii) Consider student (std_id, std_name, date_of_birth, phone, dept_name). Put the data
for a student with student id200, name arun, birth date 1st February, 1985, phonenumber (01110
32818 and dept name English in the student table.
(iii) A constraint named less_than_20 was defined on the field date_of_birth of tablestudent.
Delete this constraint.
(iv) Consider the table student and list names of students in the departments other than
(v) Consider employee table of (i) and list names of department(s) for which averagemaths and
computer.salary for department is more than 10,000.
(vi) Create role named role_table that allows a user to create tables. Using role_tableallow users
kripa and reena to create tables.
(vii) Create a view emp_dep containing e_name and number of dependents from the
tables employee and dependent of (i)
Ans: Create table street (name character(30) primary key, location character(30), city
character(30));
Create table house( number integer primary key, street_name character(30) references
street(name));
Create table lives ( name character(30) primary key, house number integer references
house(number));
11. For the relations given in Q11 answer the following queries in SQL
(i) Get the names of persons who live in the street named ‘Mahatma Gandhi’.
(ii) Get the house numbers street wise.
(iii) Get the numbers of houses which are not occupied
Ans: (i) Select lives.name from Street, House, Lives where Street.name =
House.street_name and House. Number = Lives. House_number and street_name =
‘Mahatma Gandhi’;
(ii) Select number from House
order by street_name;
(iii) Select number from house
minus
Select house_number from lives;
12. Consider the relations
EMP(ENO,ENAME,AGE,BASIC_SALARY)
WORK_IN(ENO,DNO)
DEPT(DNO,DNAME,CITY)
Express the following queries in SQL
(i) Find names of employees who work in a deptt. in Delhi.
(ii) Get the deptt. number in which more than one employee is working.
(iii) Find name of employee who earns highest salary in ‘HR’ department.
Joins
Example1: Suppose we want to fetch records from two tables emp, dept where deptno of employee is equal to
dept no of dept.
Example2: Suppose there are two tables category and product.Both table contains category_id. We want to
fetch the records where both tables contain same category_id .
Example3: Suppose there are two table Employee and Bonus. We want to fetch records where both tables
contain same empno.
Example4: Suppose there are two tables Employee and Salary_Grade.Salary_Grade contains lowsal,Hisal.We
want to fetch all records where sal of employees less than lowsal.
Example5: There are two table emp and dept. We want to fetch records of emp where deptno between 10 and
20.
SELECT
E.EMPNO,ED.SAL,ED.F_NAME,ED.CITY,ED.PIN,ED.DOB
FROM EMP E,EMP_DETAILS ED WHERE E.SAL>=4000;
Example7: From the last examle fetch the records from Emp table whose city in between
DELHI,BANGALORE,PUNE.
SELECT * FROM EMP WHERE EMP_DETAILS.CITY
IN("DELHI","BANGALORE","PUNE");
Example8: In Emp table there are two columns empno and mgr. Fetch the records where empno and mgr are
same.
SELECT * FROM EMP E1, EMP E2 WHERE E1.EMPNO=E2.MGR
Example9: In Emp_SAL table there are two columns gross_pay and net_pay. Fetch the records where
gross_pay and net_pay are same.
Example10: InEmployee_details table retrieve the record whose city are same.