0% found this document useful (0 votes)
177 views8 pages

Lesson 07 Patient Diagnosis Report Solution

This document provides the solutions to a SQL lesson-end project involving creating and querying a patients database table. It includes 14 queries that: 1) Create the patients table; 2) Insert a sample record; 3) Count total patients; 4) Find oldest patient details; 5) Display current date with patients; 6) Change case of doctor names; 7) Find patient name lengths; 8) Abbreviate genders; 9) Combine patient and doctor names; 10) Calculate age logarithms; 11) Extract year from dates; 12) Check for identical patient and doctor names; 13) Flag ages over 40; 14) Find duplicate doctor names. The queries demonstrate skills with SQL functions, aggregation, joins and more.

Uploaded by

Tejas G Srikanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views8 pages

Lesson 07 Patient Diagnosis Report Solution

This document provides the solutions to a SQL lesson-end project involving creating and querying a patients database table. It includes 14 queries that: 1) Create the patients table; 2) Insert a sample record; 3) Count total patients; 4) Find oldest patient details; 5) Display current date with patients; 6) Change case of doctor names; 7) Find patient name lengths; 8) Abbreviate genders; 9) Combine patient and doctor names; 10) Calculate age logarithms; 11) Extract year from dates; 12) Check for identical patient and doctor names; 13) Flag ages over 40; 14) Find duplicate doctor names. The queries demonstrate skills with SQL functions, aggregation, joins and more.

Uploaded by

Tejas G Srikanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SQL Training

Lesson-End Project Solution


Patient Diagnosis Report

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

2. Write a query to insert values into the patients table

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

3. Write a query to display the total number of patients in the table


SQL code:
SELECT COUNT(*) AS total_patients FROM lep_6.patients;

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:

You might also like