You are on page 1of 24

1) Display the patient_id, patient_name , phone number, ( if phone number is not there display

address) for the people who are staying more then 10 days.

SELECT pid, release_date, adm_date, datediff(release_date, adm_date) as 'Days'

FROM room_allocation

WHERE datediff(release_date, adm_date)>10;

+-------+--------------+------------+------+

| pid | release_date | adm_date | Days |

+-------+--------------+------------+------+

| P0001 | 2012-10-26 | 2012-10-15 | 11 |

| P0002 | 2012-11-26 | 2012-11-15 | 11 |

| P0003 | 2012-12-30 | 2012-12-01 | 29 |

| P0001 | 2013-01-30 | 2013-01-01 | 29 |

+-------+--------------+------------+------+

4 rows in set (0.00 sec)

>>>>>>>>>>>>>>>FINAL:

SELECT pid, name, coalesce(phoneno, address) 'Contact'

FROM patient_master

WHERE pid in (SELECT pid

FROM room_allocation

WHERE datediff(release_date, adm_date)>10);


+-------+--------+------------+

| pid | name | Contact |

+-------+--------+------------+

| P0001 | Gita | 9867145678 |

| P0002 | Ashish | 9845675678 |

| P0003 | Radha | 9867166678 |

+-------+--------+------------+

3 rows in set (0.00 sec)

__________________________________________________________________________________
____________________________

2) Display the patient_id, patient_name, phone number( +91-999-999-9999 format) ,


type_of_ailment .

type of ailement is decided with the number of days stay in hospital.

if stay <=5 ---- Minor


if stay >5 and <=15 days ----Medium

if stay >15 days ---- Major

SELECT p.pid, p.name, datediff(r.release_date,r.adm_date) as 'Days',

concat('+','91-',substr(p.phoneno,1, 3), "-", substr(p.phoneno,4, 3), "-", substr(p.phoneno,7, 4)) as


'PHONE',

case when (datediff(r.release_date,r.adm_date))<=5 then "Minor"

when (datediff(r.release_date,r.adm_date))>5 AND


(datediff(r.release_date,r.adm_date))<=15 then "Medium"

when (datediff(r.release_date,r.adm_date))>15 then "Major"

end as 'type_of_ailment'

FROM patient_master p, room_allocation r

WHERE p.pid=r.pid;

+-------+--------+------+------------------+-----------------+

| pid | name | Days | PHONE | type_of_ailment |

+-------+--------+------+------------------+-----------------+

| P0001 | Gita | 11 | +91-986-714-5678 | Medium |

| P0002 | Ashish | 11 | +91-984-567-5678 | Medium |

| P0003 | Radha | 29 | +91-986-716-6678 | Major |

| P0001 | Gita | 29 | +91-986-714-5678 | Major |

+-------+--------+------+------------------+-----------------+

4 rows in set (0.00 sec)


__________________________________________________________________________________
____________________________

3) Display the doctor_id and doctor name who is treating maximum patients.

SELECT d.doctorid, d.doctorname, count(p.pid)

FROM patient_master p, doctor_master d

WHERE p.doctorid=d.doctorid

GROUP BY doctorid;

+----------+------------+--------------+

| doctorid | doctorname | count(p.pid) |

+----------+------------+--------------+

| D0003 | Smita | 1|

| D0004 | Bhavan | 1|

| D0005 | Netra | 1|

+----------+------------+--------------+

3 rows in set (0.00 sec)


SELECT doctorid, doctorname

FROM(

SELECT d.doctorid, d.doctorname, count(p.pid)

FROM patient_master p, doctor_master d

WHERE p.doctorid=d.doctorid

GROUP BY doctorid

HAVING count(p.pid) in (SELECT max(c)

FROM (SELECT count(*) c

FROM patient_master

GROUP BY pid)a

)s;

+----------+------------+

| doctorid | doctorname |

+----------+------------+

| D0003 | Smita |

| D0004 | Bhavan |

| D0005 | Netra |

+----------+------------+

3 rows in set (0.00 sec)


__________________________________________________________________________________
____________________________

4) Display the patients who were admitted in the month of january.

SELECT * FROM ROOM_ALLOCATION;

+---------+-------+------------+--------------+

| room_no | pid | adm_date | release_date |

+---------+-------+------------+--------------+

| R0001 | P0001 | 2012-10-15 | 2012-10-26 |

| R0002 | P0002 | 2012-11-15 | 2012-11-26 |

| R0002 | P0003 | 2012-12-01 | 2012-12-30 |

| R0003 | P0001 | 2013-01-01 | 2013-01-30 |

+---------+-------+------------+--------------+

4 rows in set (0.00 sec)


SELECT *

FROM patient_master

WHERE pid in (SELECT pid FROM room_allocation WHERE monthname(adm_date)='January');

+-------+------+-----+--------+--------+---------+------------+---------------+----------+

| pid | name | age | weight | gender | address | phoneno | disease | doctorid |

+-------+------+-----+--------+--------+---------+------------+---------------+----------+

| P0001 | Gita | 35 | 65 | F | Chennai | 9867145678 | Eye Infection | D0003 |

+-------+------+-----+--------+--------+---------+------------+---------------+----------+

1 row in set (0.01 sec)


__________________________________________________________________________________
____________________________

5) Display the patient_id and patient_name who paid more then once.

SELECT * FROM bill_payment;

+--------+-------+---------+

| billid | pid | amount |

+--------+-------+---------+

| B0001 | P0001 | 1200.56 |

| B0002 | P0002 | 3200.49 |

| B0003 | P0003 | 4200.49 |

+--------+-------+---------+

3 rows in set (0.00 sec)

SELECT pid, name

FROM patient_master

WHERE pid in (SELECT pid FROM bill_payment GROUP BY pid HAVING count(pid)>1);

Empty set (0.01 sec)


__________________________________________________________________________________
____________________________

6) Display the doctor_id, doctor_name and count of patients. Display the data in descending order of
the cont_of_patients.

SELECT d.doctorid, d.doctorname, count(p.pid) 'count_of_patient'

FROM doctor_master d, patient_master p

WHERE d.doctorid=p.doctorid

GROUP BY d.doctorid

ORDER by count_of_patient desc;


+----------+------------+------------------+

| doctorid | doctorname | count_of_patient |

+----------+------------+------------------+

| D0004 | Bhavan | 1|

| D0005 | Netra | 1|

| D0003 | Smita | 1|

+----------+------------+------------------+

3 rows in set (0.00 sec)


__________________________________________________________________________________
____________________________

7) Display the room_no, room_type which are allocated more then once.

SELECT * FROM room_allocation;

+---------+-------+------------+--------------+

| room_no | pid | adm_date | release_date |

+---------+-------+------------+--------------+

| R0001 | P0001 | 2012-10-15 | 2012-10-26 |

| R0002 | P0002 | 2012-11-15 | 2012-11-26 |

| R0002 | P0003 | 2012-12-01 | 2012-12-30 |

| R0003 | P0001 | 2013-01-01 | 2013-01-30 |

+---------+-------+------------+--------------+

4 rows in set (0.01 sec)

SELECT room_no, room_type

FROM room_master

WHERE room_no in (SELECT room_no FROM room_allocation GROUP BY room_no HAVING


count(room_no)>1);
+---------+-----------+

| room_no | room_type |

+---------+-----------+

| R0002 | Suite |

+---------+-----------+

1 row in set (0.00 sec)


__________________________________________________________________________________
____________________________

8) Display the room_no, room_type which are allocated more then once to the same patient.

SELECT * FROM room_allocation;

+---------+-------+------------+--------------+

| room_no | pid | adm_date | release_date |

+---------+-------+------------+--------------+

| R0001 | P0001 | 2012-10-15 | 2012-10-26 |

| R0002 | P0002 | 2012-11-15 | 2012-11-26 |

| R0002 | P0003 | 2012-12-01 | 2012-12-30 |

| R0003 | P0001 | 2013-01-01 | 2013-01-30 |

+---------+-------+------------+--------------+

4 rows in set (0.01 sec)

SELECT room_no, room_type

FROM room_master

WHERE room_no IN (SELECT room_no FROM room_allocation GROUP BY pid HAVING count(pid)>1);
__________________________________________________________________________________
____________________________

9) display the patient_id,patient_name, doctor_id, doctor_name, room_id, room_type, adm_date,


bill_id, amount . Amount should be rounded of.

SELECT p.pid, p.name,

d.doctorid, d.doctorname,

rm.room_no, rm.room_type,

ra.adm_date,

b.billid, round(b.amount) as Amount


FROM doctor_master d, patient_master p, room_master rm, room_allocation ra, bill_payment b

WHERE d.doctorid=p.doctorid AND p.pid=ra.pid AND rm.room_no=ra.room_no AND p.pid=b.pid;

+-------+--------+----------+------------+---------+-----------+------------+--------+--------+

| pid | name | doctorid | doctorname | room_no | room_type | adm_date | billid | Amount |

+-------+--------+----------+------------+---------+-----------+------------+--------+--------+

| P0001 | Gita | D0003 | Smita | R0001 | AC | 2012-10-15 | B0001 | 1201 |

| P0002 | Ashish | D0004 | Bhavan | R0002 | Suite | 2012-11-15 | B0002 | 3200 |

| P0003 | Radha | D0005 | Netra | R0002 | Suite | 2012-12-01 | B0003 | 4200 |

| P0001 | Gita | D0003 | Smita | R0003 | NonAC | 2013-01-01 | B0001 | 1201 |

+-------+--------+----------+------------+---------+-----------+------------+--------+--------+

4 rows in set (0.00 sec)


__________________________________________________________________________________
____________________________

10) Display the patient_id, patient_name, billid, amount. Amount should be rounded of to single
place of decimal.

SELECT p.pid, p.name, b.billid, round(b.amount,1)

FROM patient_master p, bill_payment b

WHERE p.pid=b.pid;

+-------+--------+--------+-------------------+

| pid | name | billid | round(b.amount,1) |

+-------+--------+--------+-------------------+
| P0001 | Gita | B0001 | 1200.6 |

| P0002 | Ashish | B0002 | 3200.5 |

| P0003 | Radha | B0003 | 4200.5 |

+-------+--------+--------+-------------------+

3 rows in set (0.00 sec)

__________________________________________________________________________________
____________________________

11) Display the room_no which was never allocated to any patient.

SELECT * FROM ROOM_ALLOCATION;

+---------+-------+------------+--------------+

| room_no | pid | adm_date | release_date |

+---------+-------+------------+--------------+

| R0001 | P0001 | 2012-10-15 | 2012-10-26 |

| R0002 | P0002 | 2012-11-15 | 2012-11-26 |

| R0002 | P0003 | 2012-12-01 | 2012-12-30 |


| R0003 | P0001 | 2013-01-01 | 2013-01-30 |

+---------+-------+------------+--------------+

4 rows in set (0.00 sec)

SELECT room_no

FROM room_allocation

GROUP by room_no

HAVING count(room_no)>1;

+---------+

| room_no |

+---------+

| R0002 |

+---------+

1 row in set (0.00 sec)


__________________________________________________________________________________
____________________________

12) Display the the doctors_id who never treated any patients.

SELECT doctorid

FROM doctor_master

WHERE doctorid NOT IN (SELECT doctorid FROM patient_master);

+----------+

| doctorid |

+----------+

| D0001 |

| D0002 |

| D0006 |

+----------+

3 rows in set (0.00 sec)


__________________________________________________________________________________
____________________________

13) The depatment which are having the maximum number of doctors.

SELECT dept, count(doctorid)

FROM doctor_master

GROUP BY dept;

+---------+-----------------+

| dept | count(doctorid) |

+---------+-----------------+

| ENT | 2|

| Eye | 1|

| Surgery | 3|

+---------+-----------------+

3 rows in set (0.00 sec)

SELECT dept, MAX[count(doctorid)]

FROM doctor_master

GROUP BY dept;
SELECT dept

FROM doctor_master

GROUP BY dept

HAVING count(doctorid) in (SELECT max(c)

FROM (SELECT count(*) c

FROM doctor_master

GROUP BY dept)a

);

+---------+

| dept |

+---------+

| Surgery |

+---------+

1 row in set (0.00 sec)


__________________________________________________________________________________
____________________________

14) Count the number of male and female patients.

SELECT gender, count(pid)

FROM patient_master

GROUP BY gender;

+--------+------------+

| gender | count(pid) |

+--------+------------+

|F | 2|

|M | 1|

+--------+------------+

2 rows in set (0.00 sec)

OR
SELECT m.mc as MALES, f.fc as FEMALES

FROM

(SELECT count(*) mc FROM patient_master WHERE gender='m')m,

(SELECT count(*) fc FROM patient_master WHERE gender='f')f;

+-------+---------+

| MALES | FEMALES |

+-------+---------+

| 1| 2|

+-------+---------+

1 row in set (0.00 sec)

__________________________________________________________________________________
____________________________

15) Count the %age of male and female

MALE 20% FEMALE 80%


SELECT concat(round(((m.mc)/((m.mc)+(f.fc)))*100), "%") as MALES,

concat(round(((f.fc)/((m.mc)+(f.fc)))*100), "%") as FEMALES

FROM (SELECT count(*) mc FROM patient_master WHERE gender='m')m,

(SELECT count(*) fc FROM patient_master WHERE gender='f')f;

+-------+---------+

| MALES | FEMALES |

+-------+---------+

| 33% | 67% |

+-------+---------+

1 row in set (0.00 sec)

You might also like