SQL PRC
SQL PRC
Example 1 :
create table
COURSE(cid,cname)
STUDENT(sid,sname,gender,dob,marks,cid)
-- Create the COURSE table
CREATE TABLE COURSE (
cid NUMBER(3) PRIMARY KEY,
cname VARCHAR2(20) NOT NULL);
Instagram mr_faisal_607
Telegram : GUBCA
9. Add 10 Marks in each student and rename column as gracing
select sid,sname,marks,marks + 10 as gracing from student;
10. Display name,marks and course id of student who marks> 600 and cid=1
select sname,marks,cid from student
where marks>600 and cid=1;
11. Display name,marks and course id of student who marks> 600 or cid=1
select sname,marks,cid from student
where marks>600 or cid=1;
12. Display students other than BCA course using NOT operator
select * from student
where not cid=1;
13. Display marks of students between 500 to 600
select sname,marks from student
where marks between 500 and 600;
14. Display records of student who as not assign course id
select * from student
where cid is null;
15. Display name of student starts with letter ‘A’.
select sname from student
where sname like 'A%';
16. List student who name has last letter ‘H’ of ‘A’
select sname from student
where sname like '%h' or sname like '%a';
17. List all student who’s name has any accurance of letter ‘A’
select sname from student
where sname like '%a%';
18. Student name has second letter ‘E’
select * from student
where sname like '_e%';
19. Display student whose cid is either 1 or 2 using in operator
select * from student
where cid in(1,2);
20. Update marks 450 of student whose cid=3
update student
set marks=455
where cid=3;
21. Add column of city in student table
alter table student
add city varchar2(20);
update student
set city='Ahemdabad'
where sid in(1,2,3);
22. Delete a column of city from student table
alter table student
drop column city;
23. Display total marks of student
select sum(marks) as total from student;
Instagram mr_faisal_607
Telegram : GUBCA
24. Display minimum marks of male student
select min(marks) as minimum from student
where gender='M';
25. Display maximum and minimum marks of student whose cid=2
select min(marks),max(marks) from student
where cid=2;
26. Display total students
select count(sid) as total from student;
27. Count number of BCA student
select count(sid) from student
where cid=1;
28. Display sid,sname,marks of student whose marks > average marks
select sid,sname,marks from student
where marks<(select avg(marks) from student);
29. Display sid,sname,marks and dob of student in descending order of marks
SELECT sid, sname, marks, dob
FROM student
ORDER BY marks DESC;
30. List student of BCA in ascending order of their name
SELECT sid, sname, marks, dob FROM student
WHERE course = 'BCA'
ORDER BY sname ASC;
31. List unique cid of student
SELECT DISTINCT cid FROM student;
32. Create view stu_detail which displays sid,sname,marks of male students
CREATE VIEW stu_detail AS SELECT sid, sname, marks FROM student
WHERE gender = 'Male';
33. Display sname in capital and lower case
select sname,upper(sname),lower(sname) from student;
34. Find number of character in student name
select sname,length(sname) as no_of_char from student;
35. Display first 3 character of sname
select sname,substr(sname,1,3) from student;
36. Display sid and sname in single column
select sid ||' ' || sname from student;
37. Add 2 years in student dob
select sname,dob,add_months(dob,24) from student;
38. Display last date of dob
select sname ,dob,last_day(dob) from student;
39. Display sid,sname,dob and year of student
select sid,sname,dob,to_char(dob,'yyyy') as year from student;
40. List student born in May month
select sid,sname,dob,to_char(dob,'mm') from student
where to_char(dob,'mm')=5;
41. Display all possible records of both the table(cross join)
select * from student ,course;
42. Display sname,dob and cname of student
select sname,dob,cname from student,course
where student.cid=course.cid;
Instagram mr_faisal_607
Telegram : GUBCA
43. Display records of male student along with their course detail
select sid,sname,dob,gender,cname from student,coursewhere student.cid=course.cid and gender='M';
44. Perform natural join in both the table
select * from student natural join course;
45. Display sname,marks,cname of students who’s name starts with letter ‘S’
select sname,marks,cname from student join COURSE
using(cid)
where cname like 's%';
46. Perform left outer join on both the table
select * from student left outer join course on student.cid = course.cid;
47. Perform right outer join on both the table
select * from student right outer join course on student.cid = course.cid;
48. Perform full outer join on both the table
select * from student full outer join course on student.cid = course.cid;
Example 2 :
Doctor(did,dname,dept,qualification,salary)
Patient(pid,pname,disease,charges,did)
Constraints
1. Provide primary foregin key.
2. Name should be not null.
Queries:
1 . display records of doctors an patient
2. list dname,qualification and salary of doctor belong ot 'surigical' dept.
3. list record of patient who has 'appendix'
4. list records of docors and patients where doctor name start with letter 'S'
5.display dname,charges of patient in desc order of charges
6. display dname, salary whose salary is less than then maximum salary
7. count unique doctor id from patient table
8. give total charges of patient who has 'cancer'
9. display all doctor whose qualification is either 'MBBS' or 'MD' and take charges > 500
10. calculate total charges of patient whose did id 103
11.create view of patient whose charges < 5000
12. display pid , pname,dnamea and charges form tables
13. list doctor details who treats cancer patients
Instagram mr_faisal_607
Telegram : GUBCA
14. display dname,dept,salary and avg salary of doctors who works in I.C.U
-- Queries
-- 4. List records of doctors and patients where doctor name starts with letter 'S'
SELECT * FROM Doctor WHERE dname LIKE 'S%';
SELECT * FROM Patient WHERE did IN (SELECT did FROM Doctor WHERE dname LIKE 'S%');
Instagram mr_faisal_607
Telegram : GUBCA
-- 6. Display dname, salary whose salary is less than the maximum salary
SELECT dname, salary FROM Doctor
WHERE salary < (SELECT MAX(salary) FROM Doctor);
-- 9. Display all doctors whose qualification is either 'MBBS' or 'MD' and take charges > 500
select dname,qualification from doctor join patient
on doctor.did = patient.did
where (qualification='MD' or qualification='MBBS') and charges>5000;
-- 14. Display dname, dept, salary, and average salary of doctors who work in the Surgical
SELECT dname, dept, salary, (SELECT AVG(salary) FROM Doctor) AS avg_salary FROM doctor
WHERE dept = 'Surgical';
Example 3 :
create table
vendor(vid,vname)
product(pid,pname,price,qty,mfgdt,vid)
Constraints
1. Provide primary foregin key.
2. Name should be not null.
3. qty should be greadter then 10
create table vendor(
vid number(3) primary key,
vname varchar2(30) not null);
Instagram mr_faisal_607
Telegram : GUBCA
create table product(
pid number(3) primary key,
pname varchar(30) not null,
price number(5),
qty number(3) check (qty > 10),
mfgdt date,
vid number(3),
FOREIGN KEY (vid) REFERENCES vendor(vid));
Queries:
1.insert 5 records vendor and 10 records in product table
Instagram mr_faisal_607
Telegram : GUBCA
12. add 3 years in prouducts mfg date and rename the column as expire date
select pid,pname,mfgdt,add_months(mfgdt,36) as expire_date from product;
13. list maximum price along with the product detail who's quantity is greater then 20 and name starts with N
select pname,price,qty,(select max (price) from product) as max_price from product
where qty>20 and pname like 'N%'
14. list details of product and vendor who's price is less than avg price of the product
SELECT pname, price,vname FROM product JOIN vendor ON product.vid = vendor.vid
WHERE price < (SELECT AVG(price) FROM product);
19. crate a view product_detail which displays products and it's related vendors the common column should
be display first
CREATE VIEW product_detail AS
SELECT p.pid, p.pname, p.price, p.qty, p.mfgdt, p.vid, v.vname
FROM product p
JOIN vendor v ON p.vid = v.vid;
Instagram mr_faisal_607