0% found this document useful (0 votes)
25 views

SQL PRC

Uploaded by

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

SQL PRC

Uploaded by

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

Telegram : GUBCA

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);

-- Create the STUDENT table


CREATE TABLE STUDENT (
sid NUMBER(3) PRIMARY KEY,
sname VARCHAR2(20) NOT NULL,
gender CHAR(1) CHECK (gender IN ('M', 'F')),
dob DATE,
marks NUMBER(3),
cid NUMBER(2),
FOREIGN KEY (cid) REFERENCES course(cid));
1. Display no. of tables in the database.
select * from tab;
2. To Display discretion of table.
desc course;
of
desc student;
3. Insert record in a table.
insert into course values(1,'BCA');
insert into course(cid,cname) values(2,'BBA');
4. Display records of table
select * from course;
or
select * from STUDENT;
5. Save the changes
commit;
6. List nmae,DOB and marks of the student
select sname,dob,marks from student;
7. Display records of student whose marks < 600
select * from student
where marks < 600;
8. List sid,sname,marks,cid of student who does not belong to BCA course
select sid,sname,marks,cid from student
where cid !=1;
or
select sid,sname,marks,cid from student
where cid <>1;

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.

Create sequence for doctor table as d1 and patient as p1


d1 should start with 101
p1 should start with 401

Insert 10 records using sequence in both tables

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

-- Create Doctor Table


create table doctor(
did number(3) primary key,
dname varchar(20) not null,
dept varchar(30),
qualification varchar(30),
salary number(5));

-- Create Patient Table


create table patient(
pid number(3) primary key,
pname varchar(30) not null,
disease varchar(30),
charges number(5),
did number(3),
foreign key(did) references doctor(did));

-- Create Sequence for Doctor Table as d1 starting with 101


CREATE SEQUENCE d1 START WITH 101;

-- Create Sequence for Patient Table as p1 starting with 401


CREATE SEQUENCE p1 START WITH 401;

-- Queries

-- 1. Display records of doctors and patients


SELECT * FROM Doctor;
SELECT * FROM Patient;

-- 2. List dname, qualification, and salary of doctors belonging to 'Surgical' dept


SELECT dname, qualification, salary FROM Doctor
WHERE dept = 'Surgical';

-- 3. List records of patients who have 'Appendix'


SELECT * FROM Patient
WHERE disease = 'Appendix';

-- 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%');

-- 5. Display dname, charges of patients in descending order of charges


select dname,charges from doctor join patient
on doctor.did = patient.did
order by charges desc;

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);

-- 7. Count unique doctor id from the patient table


SELECT COUNT(DISTINCT did) AS unique_did FROM Patient;

-- 8. Give total charges of patients who have 'Cancer'


SELECT SUM(charges) AS total_charges FROM Patient WHERE disease = 'Cancer';

-- 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;

-- 10. Calculate the total charges of patients whose did id is 103


SELECT SUM(charges) AS total_charges FROM Patient WHERE did = 103;

-- 11. Create a view of patients whose charges < 5000


CREATE VIEW Patients_under_5000 AS
SELECT * FROM Patient WHERE charges < 5000;

-- 12. Display pid, pname, dname, and charges from tables


select pid,pname,dname,charges from doctor join patient
on doctor.did = patient.did;

-- 13. List doctor details who treat cancer patients


SELECT * FROM Doctor WHERE did IN (SELECT did FROM Patient WHERE disease = 'Cancer');

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

2. display records of both the tables


-- Display records of the Vendor table
SELECT * FROM vendor;

-- Display records of the Product table


SELECT * FROM product;

3.list products where price is greater than 500


SELECT * FROM product WHERE price > 500;

4. display year of the producct and it's quantity of vid =2


SELECT EXTRACT(YEAR FROM mfgdt) AS year, qty FROM product WHERE vid = 2;

5. update price of product as 1000 of pid=1


UPDATE product SET price = 1000 WHERE pid = 1;

6. provide 5% discount on all the produucts and rename the column


SELECT pid, pname, price * 0.05 AS discounted_price FROM product;

7. list products manufactured by vid = 2 or 4 using in operator


SELECT * FROM product WHERE vid IN (2, 4);

8. list vendor whose name has 3rd letter 'a'


SELECT * FROM vendor
where vname like '_a%';
9. display pname price,qty,vname where a products mfgdt in january month
select pname,price,qty,vname from vendor join product
on vendor.vid=product.vid
where to_char(mfgdt,'mm')=01;
10. list vendors and there product who's price in 500 to 1000
select * from vendor join product
on vendor.vid=product.vid
where price between 500 and 1000;

11. list current at of the system


SELECT CURRENT_DATE AS current_date;

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);

15. count total products sold by vid = 3


SELECT COUNT(vid) FROM product
WHERE vid = 3;

16. display first 3 character of product name


SELECT pid, SUBSTRING(pname, 1, 3) AS first_three_chars
FROM product;

17. list products which has more than 5 characters


SELECT * FROM product WHERE LENGTH(pname) > 5;

18. count uinque vendor from product table


SELECT COUNT(DISTINCT vid) AS unique_vendors 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;

20. list products in descending order of vname


SELECT p.pid, p.pname, p.price, p.qty, p.mfgdt, p.vid
FROM product p
JOIN vendor v ON p.vid = v.vid
ORDER BY v.vname DESC;

Instagram mr_faisal_607

You might also like