Lesson 07 Patient Diagnosis Report Solution
Lesson 07 Patient Diagnosis Report Solution
1. Write a query to create a patients table with the date, patient ID, patient name, age,
weight, gender, location, phone number, disease, doctor name, and doctor ID fields
SQL code:
CREATE TABLE lep_6.patients (
date DATE NOT NULL,
pid varchar(45) NOT NULL,
p_name varchar(45) NOT NULL,
age INT NOT NULL,
weight INT NOT NULL,
gender varchar(45) NOT NULL,
location varchar(45) NOT NULL,
phone_no INT NOT NULL,
disease varchar(45) NOT NULL,
doctor_name varchar(45) NOT NULL,
doctor_id INT NOT NULL,
PRIMARY KEY(pid));
SQL code:
INSERT INTO lep_6. patients
(date,pid,p_name,age,weight,gender,location,phone_no,disease,doctor_name,docto
r_id) VALUES ('2019-06-
15','AP2021','Sarath','67','76','Male','chennai','5462829','Cardiac','Mohan','21');
Output:
4. Write a query to display the patient ID, patient name, gender, and disease of the
oldest (age) patient
SQL code:
SELECT pid,p_name,gender,disease, MAX(AGE) AS MAX_AGE FROM lep_6.patients;
Output:
5. Write a query to display patient id and patient name with the current date.
SQL code:
SELECT pid,p_name ,NOW() as CurrentDate FROM lep_6.patients;
Output:
6. Write a query to display the old patient name and new patient name in uppercase
SQL code:
SELECT doctor_name,UCASE(doctor_name) AS UpperCase_D_name FROM
lep_6.patients;
Output:
7. Write a query to display the patients' names along with the total number of
characters in their name
SQL code:
SELECT p_name,length(p_name) AS lengthofp_name FROM lep_6.patients;
Output:
8. Write a query to display the gender of the patient as M or F along with the patient's
name
SQL code:
SELECT p_name,MID(gender,1,1) AS GENDER FROM lep_6.patients;
Output:
9.Write a query to combine the patient's name and doctor's name in a new column
SQL code:
SELECT p_name,doctor_name,CONCAT(p_name,doctor_name) AS
patient_doctor_name FROM lep_6.patients;
Output:
10. Write a query to display the patients’ age along with the logarithmic value (base 10) of
their age
SQL code:
SELECT age,LOG10(age) as LOG_AGE FROM lep_6.patients;
Output:
11. Write a query to extract the year for a given date and place it in a separate column
SQL code:
SELECT *,YEAR(date) AS Year FROM lep_6.patients;
Output:
12. Write a query to check the patient’s name and doctor’s name are similar and
display NULL, else return the patient’s name
SQL code:
SELECT NULLIF(p_name,doctor_name) FROM lep_6.patients;
Output:
13. Write a query to check if a patient’s age is greater than 40 and display Yes if it is and No
if it isn't
SQL code:
SELECT age,IF(age>40,'Yes','No') AS Agegreater40 FROM lep_6.patients;
Output:
14. Write a query to display duplicate entries in the doctor name column
SQL code:
SELECT doctor_name,COUNT(*) occurences FROM lep_6.patients GROUP BY
doctor_name HAVING COUNT(*)>1;
Output: