Professional Documents
Culture Documents
CYCLE SHEET – 2
Q2. In Patient table, replace address with three attributes namely street, city and
pincode.
CODE:
ALTER TABLE patient
Aakar Gupta(19BCE0379)
Q3. Add an attribute Test_Time which can accept only two values “Before food” and
“After food” with proper constraint name.
CODE:
ALTER table lab_tests
ADD Test_Time VARCHAR2(20);
ALTER table lab_tests
ADD CONSTRAINT CheckTest
CHECK (Test_Time in ('Before food','After food'));
Aakar Gupta(19BCE0379)
Aakar Gupta(19BCE0379)
Q5. Drop address attribute from staff table and add attributes door_no, street, city, and
pincode.
CODE:
ALTER TABLE staff
DROP COLUMN address;
ALTER TABLE Staff
ADD (door_no NUMBER(5),
Aakar Gupta(19BCE0379)
street VARCHAR2(50),
city VARCHAR2(50),
pincode NUMBER(6));
Aakar Gupta(19BCE0379)
Q7. Remove the attributes dosage and brand from Prescribed_Medicines and alter the
medicine_name attribute as a foreign key referencing the new table Medicines.
CODE:
ALTER TABLE prescribed_medicines
DROP (dosage,brand);
Aakar Gupta(19BCE0379)
Q8. Create a view for doctors who are specialized in ‘Cardiology’ from Doctor table with
attributes doc_id, doc_name and gender.
CODE:
CREATE VIEW doctors AS
SELECT doc_id,doc_name,gender
FROM doctor
WHERE specialist = 'Cardiology';
Aakar Gupta(19BCE0379)
Q9. Add an attribute No_of_staff in Department table and create a constraint with
constraint name to make sure the number is > 0.
CODE:
ALTER TABLE department
ADD No_of_staff NUMBER(5)
CONSTRAINT CHK_No_of_staff
CHECK (No_of_staff > 0);
Aakar Gupta(19BCE0379)
Q10. Add an attribute with In_Patient_prescription to store the Room_Type which can
store the values “AC” and “Non-AC”.
CODE:
ALTER TABLE in_patient_prescription
ADD Room_Type VARCHAR2(10)
CONSTRAINT CHK_Room_Type
Aakar Gupta(19BCE0379)
Q2. Find the list of all patients who were admitted in bed number ‘B101’.
CODE USING CARTESIAN PRODUCT:
SELECT pat_name,bed_no FROM patient,in_Patient
WHERE patient.pat_id = in_patient.pat_id
AND bed_no = 'B101';
FROM patient
INNER JOIN in_patient ON patient.pat_id = in_patient.pat_id
WHERE in_patient.bed_no = 'B101';
Q3. Display all the prescribed medicines of patient with Pat_ID ‘P101’.
CODE USING CARTESIAN PRODUCT:
SELECT medicine_name FROM
prescribed_Medicines,prescription,in_patient_prescription
WHERE prescription.pres_id = prescribed_medicines.pres_id
AND prescription.pres_id = in_patient_prescription.pres_id
AND in_patient_prescription.pat_id = 'P101';
Aakar Gupta(19BCE0379)
Q5. Display all bills of bill amount more than 10000 rupees and paid by the patient
‘Steve’.
CODE USING CARTESIAN PRODUCT:
SELECT pat_name,bill_amount
FROM hospital_bill,patient
WHERE patient.pat_id = hospital_bill.pat_id
AND patient.pat_name = 'Steve'
AND hospital_bill.bill_amount > 10000;
FROM hospital_bill
INNER JOIN patient ON patient.pat_id = hospital_bill.pat_id
WHERE patient.pat_name = 'Steve'
AND hospital_bill.bill_amount > 10000;
Q6. Find the category and address of the nurse who attended the patient with pat_no
‘P220’.
Since we’ve already dropped address attribute of staff entity and added door_no,
street, city, pincode instead in Ques – 4 of previous exercise, therefore address can’t
be referred to now.
Q7. Find the list of doctors who worked in the department which is started on or after
’10-May-2018’.
CODE USING CARTESIAN PRODUCT:
SELECT doc_name
Aakar Gupta(19BCE0379)
FROM doctor,department
WHERE doctor.doc_id = department.hod
AND department.est_date >= date '2018-05-10';
FROM staff,test_types
WHERE test_types.technician = staff.staff_id
AND test_types.description = 'Blood test';
Q9. Display the details of all patients who were hospitalized between ’10-Mar2020’ and
’10-Jul-2020’.
Aakar Gupta(19BCE0379)
Since we’ve already dropped address attribute of patient entity and replaced it with
street, city, pincode instead in Ques – 2 of previous exercise, therefore address can’t
be referred to now.
Q10. Display the in-patient prescription of the patient whose name is ‘Gayle’.
Since we’ve already dropped address attribute of patient entity and replaced it with
street, city, pincode instead in Ques – 2 of previous exercise, therefore address can’t
be referred to now.
Also we’ve added the Room_type attribute in In_Patient_prescription table in Ques –
10 of previous exercise.
Also we’ve dropped attributes dosage and brand from Prescribed_Medicines in Ques –
7 of previous exercise.
Q1. Find the number of doctors who are working in the department ‘D101’.
CODE:
SELECT COUNT(*)
AS "NO OF DOCTORS WORKING IN DEPARTMENT D101"
FROM DOCTOR WHERE DEPT_NO = 'D101';
Q2. Count the number of male patients who are treated by the doctor with doctor id
‘D0001’.
CODE:
Aakar Gupta(19BCE0379)
Q4. Find the name and address of the patient who paid the highest bill of all patients.
Since we’ve already dropped address attribute of patient entity and replaced it with
street, city, pincode instead in Ques – 2 of previous exercise, therefore address can’t
be referred to now.
Instead of Address we will take street and city as the references.
CODE:
SELECT patient.pat_name, patient.street,patient.city, hospital_bill.bill_amount
FROM patient
INNER JOIN hospital_bill ON patient.pat_id = hospital_bill.pat_id
WHERE hospital_bill.bill_amount = (SELECT MAX(bill_amount) FROM hospital_bill);
Aakar Gupta(19BCE0379)
Q5. Get the specialization of doctors whose name start with the letter ‘M’.
CODE USING SUBSTR:
SELECT doctor.doc_name, doctor.specialist
FROM doctor
Aakar Gupta(19BCE0379)
Q6. Find the all the patients details whose name is exactly 5 characters long.
Since we’ve already dropped address attribute of patient entity and replaced it with
street, city, pincode instead in Ques – 2 of previous exercise, therefore address can’t
be referred to now.
CODE:
SELECT * FROM patient
WHERE LENGTH(patient.pat_name) = 5;
Aakar Gupta(19BCE0379)
FROM patient
WHERE patient.gender = 'F';
Q10. Get the total number tests conducted in any particular date.
CODE:
SELECT COUNT(test_id) AS "Number of Tests performed on 09-FEB-20"
FROM lab_tests
WHERE lab_tests.dte = date '2020-02-09'
Q1. All of the queries in “SQL queries with JOIN operation” section can be tried with
subqueries concept.
a) Find the HOD of doctor ‘RAGHVAN’.
CODE:
SELECT hod FROM department WHERE dept_no = (SELECT dept_no FROM doctor
WHERE doc_name = 'RAGHVAN');
Aakar Gupta(19BCE0379)
b) Find the list of all patients who were admitted in bed number ‘B101’.
CODE:
SELECT pat_name FROM patient where pat_id in (SELECT pat_id FROM in_patient
WHERE bed_no = 'B101');
CODE:
SELECT medicine_name FROM prescribed_medicines WHERE pres_id in (SELECT
pres_id FROM in_patient_prescription WHERE pat_id = 'P101');
CODE:
SELECT result FROM test_results WHERE test_id = (SELECT test_id FROM
lab_tests,patient WHERE lab_tests.pat_id = patient.pat_id
AND pat_name ='Mani');
e) Display all bills of bill amount more than 10000 rupees and paid by the patient
‘Steve’.
CODE:
SELECT bill_amount FROM hospital_bill WHERE pat_id = (SELECT pat_id FROM
patient WHERE pat_name = 'Steve') AND bill_amount > 10000;
f) Find the category and address of the nurse who attended the patient with pat_no
‘P220’.
Since we’ve already dropped address attribute of staff entity and added door_no,
street, city, pincode instead in Ques – 4 of previous exercise, therefore address
can’t be referred to now.
Aakar Gupta(19BCE0379)
g) Find the list of doctors who worked in the department which is started on or after
’10-May-2018’.
CODE:
SELECT doc_name FROM doctor WHERE dept_no IN (SELECT dept_no
FROM department WHERE est_date >= date '2018-05-10');
Aakar Gupta(19BCE0379)
CODE:
SELECT staff_name FROM staff WHERE staff_id IN (SELECT technician FROM
test_types WHERE description = 'Blood test');
i) Display the details of all patients who were hospitalized between ’10-Mar2020’ and
’10-Jul-2020’.
Since we’ve already dropped address attribute of patient entity and replaced it
with street, city, pincode instead in Ques – 2 of previous exercise, therefore
address can’t be referred to now.
CODE:
Aakar Gupta(19BCE0379)
SELECT * FROM patient WHERE pat_id IN (SELECT pat_id FROM in_patient WHERE
start_time >= date '2020-03-10' AND end_time <= date '2020-07-10');
Since we’ve already dropped address attribute of patient entity and replaced it with
street, city, pincode instead in Ques – 2 of previous exercise, therefore address can’t be
referred to now.
Also we’ve added the Room_type attribute in In_Patient_prescription table in Ques –
10 of previous exercise.
Also we’ve dropped attributes dosage and brand from Prescribed_Medicines in Ques –
7 of previous exercise.
CODE:
Q2. Display the list of doctors who are working in the department with more number of
doctors using sub-query and IN operator.
CODE:
SELECT doc_name FROM doctor WHERE dept_no IN(SELECT dept_no FROM
(SELECT COUNT(doc_id) MyCount, dept_no
FROM doctor
GROUP BY dept_no) WHERE MyCount IN (SELECT MAX(MyCount) FROM (SELECT
COUNT(doc_id) MyCount, Dept_No
FROM doctor
GROUP BY dept_no)));
Aakar Gupta(19BCE0379)
As in my database all the list of doctors working in the all the department are printed
because in my database only one doctor was there in each department.
Q3. Find the name and id of all patients who are older than all the doctors in the entire
‘cardiology’ department. Use subqueries and ALL operator.
CODE:
SELECT patient.pat_id, patient.pat_name
FROM patient
WHERE patient.dob < ALL (SELECT dob FROM doctor
WHERE doctor.specialist = 'Cardiology');
We didn’t have any data that could fulfil this query hence no data found, otherwise this
query works fine.
Aakar Gupta(19BCE0379)
Q4. Find the prescription ids of all prescription that included a medicine from the brand
‘Ranbaxy’ using nested subqueries.
CODE:
SELECT pres_id FROM prescription WHERE pres_id IN (SELECT pres_id FROM
prescribed_medicines WHERE brand = 'Ranbaxy');
Q5. Find the list of patients who paid their bill through either ‘credit card’ or ‘debit card’
using subquery.
CODE:
SELECT pat_name FROM patient WHERE pat_id IN (SELECT pat_id FROM
hospital_bill WHERE payment_type = 'Credit card' or payment_type = 'Debit card');
Aakar Gupta(19BCE0379)
Q1. Find the last date of month from the established date for a department with
department number ‘D105’.
CODE:
Q2. Find the date after adding 4 months in the date with appointment id ‘A105’.
CODE:
SELECT add_months(dte,4) AS date_after_adding_4_months FROM
appointment WHERE app_id = 'A105';
Q1. Find the Square Root of Bill Amount of patient having patient id as ‘P104’.
CODE:
SELECT bill_amount,sqrt(bill_amount) FROM Hospital_Bill WHERE pat_id = 'P104';
Aakar Gupta(19BCE0379)
Q1. Concatenate the string ‘Smith’ in the patient having name ‘Steve’.
CODE:
SELECT concat('Steve',' Smith') AS "Concatenated Patient Name" FROM patient WHERE
pat_name = 'Steve';
Aakar Gupta(19BCE0379)
CODE:
SELECT pat_name,upper(pat_name) AS "PATIENT NAMES IN UPPERCASE" FROM patient;
Aakar Gupta(19BCE0379)