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

SQL Programs (1-15)

Uploaded by

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

SQL Programs (1-15)

Uploaded by

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

Program 1

'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: Create the doctor table in the hospital database and insert some
records or tuples.
'''
use hospital;
create table doctor
( No integer primary key,
Name varchar(30) not null,
Age integer,
Department varchar(20),
DateofAdmn date,
Charges decimal(5,0),
Gender char(1));
insert into doctor values(1,'Sandeep',65,'Surgery','23/02/07', 300, 'M');
insert into doctor values(2,'Ravina',24,'Orthopedic','20/01/07',200,'F');
insert into doctor values(3,'Karan',45,'Orthopedic','19/02/07', 200,'M');
insert into doctor values(4,'Tarun',12,'Surgery','01/01/07',300,'M');
insert into doctor values(5,'Zubin',36,'ENT','12/01/07',250,'M');
insert into doctor values(6,'Ketaki',16,'ENT','24/02/07',300,'F');
insert into doctor values(7,'Ankita',29,'Cardiology','20/02/07',800,'F');
insert into doctor values(8,'Zareen',45,'Gynecology','22/02/07',300,'F');
insert into doctor values(9,'Kush',19,'Cardiology','13/01/07',800,'M');
insert into doctor values(10,'Shailya',31,'Nuclear Medicine','19/02/07',400,'F');

OUTPUT:
use hospital
select * from doctor;

+----+---------+------+------------------+------------+---------+--------+
| No | Name | Age | Department | DateofAdmn | Charges | Gender |
+----+---------+------+------------------+------------+---------+--------+
| 1 | Sandeep | 65 | Surgery | 2023-02-07 | 500 | M |
| 2 | Ravina | 24 | Orthopedic | 2020-01-07 | 200 | F |
| 3 | Karan | 45 | Orthopedic | 2019-02-07 | 200 | M |
| 4 | Tarun | 12 | Surgery | 2001-01-07 | 500 | M |
| 5 | Zubin | 36 | ENT | 2012-01-07 | 250 | M |
| 6 | Ketaki | 16 | ENT | 2024-02-07 | 300 | F |
| 7 | Ankita | 29 | Cardiology | 2020-02-07 | 800 | F |
| 8 | Zareen | 45 | Gynecology | 2022-02-07 | 300 | F |
| 9 | Kush | 19 | Cardiology | 2013-01-07 | 800 | M |
| 10 | Shailya | 31 | Nuclear Medicine | 2019-02-07 | 400 | F |
+----+---------+------+------------------+------------+---------+--------+
Program 2
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To show all information about the patients of cardiology
department.
'''
use hospital
select * from doctor where department='Cardiology';

OUTPUT:

+----+--------+------+------------+------------+---------+--------+
| No | Name | Age | Department | DateofAdmn | Charges | Gender |
+----+--------+------+------------+------------+---------+--------+
| 7 | Ankita | 29 | Cardiology | 2020-02-07 | 800 | F |
| 9 | Kush | 19 | Cardiology | 2013-01-07 | 800 | M |
+----+--------+------+------------+------------+---------+--------+
Program 3
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To list the names of female patients who are in orthopedic
department.
'''
use hospital
select * from doctor where gender='F' and department='Orthopedic';

OUTPUT:

+----+--------+------+------------+------------+---------+--------+
| No | Name | Age | Department | DateofAdmn | Charges | Gender |
+----+--------+------+------------+------------+---------+--------+
| 2 | Ravina | 24 | Orthopedic | 2020-01-07 | 200 | F |
+----+--------+------+------------+------------+---------+--------+
Program 4
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To list names of all patients with their date of admission in
ascending order.
'''
use hospital
select name from doctor order by dateofadmn;

OUTPUT:

+---------+
| name |
+---------+
| Tarun |
| Zubin |
| Kush |
| Karan |
| Shailya |
| Ravina |
| Ankita |
| Zareen |
| Sandeep |
| Ketaki |
+---------+
Program 5
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To display patient’s name, charges, age for male patients only.
'''
use hospital
select name, charges, age from doctor where gender='M';

OUTPUT:

+---------+---------+------+
| name | charges | age |
+---------+---------+------+
| Sandeep | 500 | 65 |
| Karan | 200 | 45 |
| Tarun | 500 | 12 |
| Zubin | 250 | 36 |
| Kush | 800 | 19 |
+---------+---------+------+
Program 6
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To count the number of patients with Age > 30.
'''
use hospital
select count(name) from doctor where age>30;

OUTPUT:

+-------------+
| count(name) |
+-------------+
| 5 |
+-------------+
Program 7
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To display all the patients of the orthopedic department.
'''
use hospital
select * from doctor where department='Orthopedic';

OUTPUT:

+----+--------+------+------------+------------+---------+--------+
| No | Name | Age | Department | DateofAdmn | Charges | Gender |
+----+--------+------+------------+------------+---------+--------+
| 2 | Ravina | 24 | Orthopedic | 2020-01-07 | 200 | F |
| 3 | Karan | 45 | Orthopedic | 2019-02-07 | 200 | M |
+----+--------+------+------------+------------+---------+--------+
Program 8
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To delete all the tuples whose department is cardiology.
'''
use hospital
delete from doctor where department='Cardiology';

OUTPUT:
use hospital
select * from doctor;

+----+---------+------+------------------+------------+---------+--------+
| No | Name | Age | Department | DateofAdmn | Charges | Gender |
+----+---------+------+------------------+------------+---------+--------+
| 1 | Sandeep | 65 | Surgery | 2023-02-07 | 500 | M |
| 2 | Ravina | 24 | Orthopedic | 2020-01-07 | 200 | F |
| 3 | Karan | 45 | Orthopedic | 2019-02-07 | 200 | M |
| 4 | Tarun | 12 | Surgery | 2001-01-07 | 500 | M |
| 5 | Zubin | 36 | ENT | 2012-01-07 | 250 | M |
| 6 | Ketaki | 16 | ENT | 2024-02-07 | 300 | F |
| 8 | Zareen | 45 | Gynecology | 2022-02-07 | 300 | F |
| 10 | Shailya | 31 | Nuclear Medicine | 2019-02-07 | 400 | F |
+----+---------+------+------------------+------------+---------+--------+
Program 9
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To increase the charges of surgery department by 400.
'''
use hospital
update doctor set charges=charges+400 where department='Surgery';

OUTPUT:
use hospital
select * from doctor;

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

| No | Name | Age | Department | DateofAdmn | Charges | Gender |

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

| 1 | Sandeep | 65 | Surgery | 2023-02-07 | 900 | M |

| 2 | Ravina | 24 | Orthopedic | 2020-01-07 | 200 | F |

| 3 | Karan | 45 | Orthopedic | 2019-02-07 | 200 | M |

| 4 | Tarun | 12 | Surgery | 2001-01-07 | 900 | M |

| 5 | Zubin | 36 | ENT | 2012-01-07 | 250 | M |

| 6 | Ketaki | 16 | ENT | 2024-02-07 | 300 | F |

| 7 | Ankita | 29 | Cardiology | 2020-02-07 | 800 | F |

| 8 | Zareen | 45 | Gynecology | 2022-02-07 | 300 | F |

| 9 | Kush | 19 | Cardiology | 2013-01-07 | 800 | M |

| 10 | Shailya | 31 | Nuclear Medicine | 2019-02-07 | 400 | F |

+----+---------+------+------------------+------------+---------+--------+
Program 10
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To list the total charges of patients of each department.
'''
use hospital
select department, sum(charges) from doctor group by department;

OUTPUT:

+------------------+--------------+
| department | sum(charges) |
+------------------+--------------+
| Surgery | 1000 |
| Orthopedic | 400 |
| ENT | 550 |
| Cardiology | 1600 |
| Gynecology | 300 |
| Nuclear Medicine | 400 |
+------------------+--------------+
Program 11
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To list the average age and charges for male and female patients.
'''
use hospital
select gender, avg(age), avg(charges) from doctor group by gender;

OUTPUT:

+--------+----------+--------------+
| gender | avg(age) | avg(charges) |
+--------+----------+--------------+
| M | 35.4000 | 610.0000 |
| F | 29.0000 | 400.0000 |
+--------+----------+--------------+
Program 12
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To find the highest and lowest age patient in the hospital.
'''
use hospital
select max(age), min(age) from doctor;

OUTPUT:

+----------+----------+
| max(age) | min(age) |
+----------+----------+
| 65 | 12 |
+----------+----------+
Program 13
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To count the total number of departments in the hospital.
'''
use hospital
select count(distinct department) from doctor;

OUTPUT:

+----------------------------+
| count(distinct department) |
+----------------------------+
| 6 |
+----------------------------+
Program 14
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To display all the patients name having ‘n’ character in their
name.
'''
use hospital
select * from doctor where name like '%n%';

OUTPUT:

+----+---------+------+------------+------------+---------+--------+
| No | Name | Age | Department | DateofAdmn | Charges | Gender |
+----+---------+------+------------+------------+---------+--------+
| 1 | Sandeep | 65 | Surgery | 2023-02-07 | 500 | M |
| 2 | Ravina | 24 | Orthopedic | 2020-01-07 | 200 | F |
| 3 | Karan | 45 | Orthopedic | 2019-02-07 | 200 | M |
| 4 | Tarun | 12 | Surgery | 2001-01-07 | 500 | M |
| 5 | Zubin | 36 | ENT | 2012-01-07 | 250 | M |
| 7 | Ankita | 29 | Cardiology | 2020-02-07 | 800 | F |
| 8 | Zareen | 45 | Gynecology | 2022-02-07 | 300 | F |
+----+---------+------+------------+------------+---------+--------+
Program 15
'''
Name: Paarth Handa
Class: XII-A
Roll No.: 12
PROGRAM: To display the name, department and charges for a week for
patients considering that charges per day is stored in the table.
'''
use hospital
select name, department, charges*7 as 'charges/week' from doctor;

OUTPUT:

+---------+------------------+--------------+
| name | department | charges/week |
+---------+------------------+--------------+
| Sandeep | Surgery | 3500 |
| Ravina | Orthopedic | 1400 |
| Karan | Orthopedic | 1400 |
| Tarun | Surgery | 3500 |
| Zubin | ENT | 1750 |
| Ketaki | ENT | 2100 |
| Ankita | Cardiology | 5600 |
| Zareen | Gynecology | 2100 |
| Kush | Cardiology | 5600 |
| Shailya | Nuclear Medicine | 2800 |
+---------+------------------+--------------+

You might also like