Got it 👍 — I couldn’t reliably read your .xls file in this environment, but no problem. I’ll still create realistic dynamic SQL examples based on a typical Excel structure, and you can easily map them to your file.
🎯 Assume your Excel table looks like this (very common)
Table: StudentsData
🔥 Dynamic SQL Examples (Based on Excel Data)
🔹 1. Dynamic Filter (Based on Excel Input Cell)
👉 Example: User selects City in Excel → pass to SQL
🔹 2. Dynamic Multiple Filters (Excel Form Scenario)
👉 Suppose Excel has filters:
👉 Perfect for teaching search forms
🔹 3. Dynamic Column Selection (User selects column in Excel)
👉 Example: User selects "Fees" or "Name"
🔹 4. Dynamic Sorting (Excel Dropdown → Sort Option)
🔹 5. Dynamic Date Filter (Very Practical 🔥)
🔹 6. Best Practice Version (sp_executesql ✅)
👉 Teach this as professional way
🔹 7. Dynamic Table (Multiple Excel Sheets)
👉 Suppose:
-
Sheet1 → Students2023
-
Sheet2 → Students2024
🎓 Classroom Teaching Idea (Very Powerful 💡)
Give students this task:
👉 “Create a dynamic search query where user can filter by:
This combines everything:
-
WHERE 1=1
-
Conditions
-
Concatenation
⚠️ Important Concept to Teach
❌ Bad (SQL Injection risk)
✅ Good
🚀 If you want next level
I can:
-
Use your actual Excel file columns (if you re-upload as
.xlsx)
-
Create stored procedure for your exact data
-
Build real-time Excel + SQL integration example
-
Give student assignment sheet + answers
Just tell me 👍
For providence Hospi
**************************************************************************************************
select * from ENZYME24TWA02_PROVIDENCE_NEWBERG_MEDICAL_CENTER order by Reference_Added_Date desc
--🔹 1. Select Patients
SELECT Patient_ID, [Patient_Name ], Age, Gender
FROM tblPatient_Infomation;
--🔹 2. Filter Patients (Age > 40)
SELECT [Patient_Name ], Age, Gender
FROM tblPatient_Infomation
WHERE Age > 40
group by [Patient_Name ], Age, Gender
--🔹 3. Procedures with High Cost
SELECT [Procedure_ID ], Cost
FROM [tbProcedures Table]
WHERE Cost > 5000;
--🔹 4. Paid Bills Only
SELECT *
FROM [tblBiling Table]
WHERE [Payment_Status ] = 'Paid';
--🔹 4. Paid Bills Only
SELECT
[Patient_ID ],
SUM([Total_Amount ]) AS Total_Billing
FROM [tblBiling Table]
GROUP BY [Patient_ID ];
--🔹 6. Average Procedure Cost
SELECT
AVG(Cost) AS Avg_Procedure_Cost
FROM [tbProcedures Table];
--🔹 7. Count Reports by Status
SELECT
[Report_Status ],
COUNT(*) AS Total_Reports
FROM [tblReports Table]
GROUP BY [Report_Status ];
--🔹 8. Doctors with Experience > 10 Years
SELECT
[Doctor_Name ], [Experience_Years ]
FROM [tblDoctore Information]
WHERE [Experience_Years ] > 10;
-------------------------------------JOINS----------------------
--🔹 9. Patient + Procedure
SELECT
P.[Patient_Name ],
Pr.[Procedure_Name ],
Pr.Cost
FROM [tblPatient_Infomation] P
JOIN [tbProcedures Table] Pr
ON P.Patient_ID = Pr.[Patient_ID ];
--🔹 10. Patient + Billing
SELECT
P.[Patient_Name ],
B.[Total_Amount ],
B.[Payment_Status ]
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ];
--🔹 11. Procedure + Report
SELECT
Pr.[Procedure_Name ],
R.[Report_Status ],
R.[Severity_Score ]
FROM [tbProcedures Table] Pr
JOIN [tblReports Table] R
ON Pr.[Procedure_ID ] = R.[Procedure_ID ];
--🔹 12. Total Revenue + Avg Procedure Time per Patient ( Errr0r)
SELECT
P.[Patient_Name ],
SUM(B.[Total_Amount ]) AS Total_Revenue,
AVG(Pr.[Duration_Minutes ]) AS Avg_Time
FROM [tblPatient_Infomation] P
JOIN [tbProcedures Table] Pr
ON P.Patient_ID = Pr.[Patient_ID ]
JOIN [tblBiling Table] B
ON Pr.[Procedure_ID ] = B.[Procedure_ID ]
GROUP BY P.[Patient_Name ];
--🔹 13. Patients with Pending Payments
SELECT
P.[Patient_Name ],
SUM(B.[Total_Amount ] - B.[Paid_Amount ]) AS Pending_Amount
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY P.[Patient_Name ]
HAVING SUM(B.[Total_Amount ] - B.[Paid_Amount ]) > 0;
--🔹 14. Most Expensive Procedure per Patient
SELECT
[Patient_ID ],
MAX(Cost) AS Max_Cost
FROM [tbProcedures Table]
GROUP BY [Patient_ID ];
--🔹 15. Ranking Patients by Billing (Window Function 🔥)
SELECT
[Patient_ID ],
SUM([Total_Amount ]) AS Total_Billing,
RANK() OVER (ORDER BY SUM([Total_Amount ]) DESC) AS Rank_By_Billing
FROM [tblBiling Table]
GROUP BY [Patient_ID ];
--🔹 16. Reports with High Severity
SELECT
[Patient_ID ],
[Severity_Score ],
[Report_Status ]
FROM [tblReports Table]
WHERE [Severity_Score ] > 7;
SELECT
[Imaging Modality],
AVG([Radiation Dose]) AS Avg_Dose
FROM [ENZYME24TWA02_PROVIDENCE_NEWBERG_MEDICAL_CENTER]
GROUP BY [Imaging Modality];
------------------------Advance Level-----------------
--🧠 Q1. Find total revenue, total bills, and average bill amount per patient
SELECT
P.Patient_ID,
P.[Patient_Name ],
COUNT(B.Bill_ID) AS Total_Bills,
SUM(B.[Total_Amount ]) AS Total_Revenue,
AVG(B.[Total_Amount ]) AS Avg_Bill
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ]
ORDER BY Total_Revenue DESC;
--🧠 Q2. Find patient procedure stats (count, avg cost, max cost)
SELECT
P.Patient_ID,
P.[Patient_Name ],
COUNT(Pr.[Procedure_ID ]) AS Total_Procedures,
AVG(Pr.Cost) AS Avg_Cost,
MAX(Pr.Cost) AS Max_Cost
FROM [tblPatient_Infomation] P
LEFT JOIN [tbProcedures Table] Pr
ON P.Patient_ID = Pr.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ]
ORDER BY Total_Procedures DESC;
--🧠 Q3. Combine billing + procedures (very powerful)
SELECT
P.Patient_ID,
P.[Patient_Name ],
COUNT(DISTINCT Pr.[Procedure_ID ]) AS Total_Procedures,
SUM(B.[Total_Amount ]) AS Total_Billing,
AVG(Pr.Cost) AS Avg_Procedure_Cost
FROM [tblPatient_Infomation] P
JOIN [tbProcedures Table] Pr
ON P.Patient_ID = Pr.[Patient_ID ]
JOIN [tblBiling Table] B
ON Pr.[Procedure_ID ] = B.[Procedure_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ]
ORDER BY Total_Billing DESC;
--🧠 Q4. Pending amount + number of pending bills per patient
SELECT
P.Patient_ID,
P.[Patient_Name ],
COUNT(B.Bill_ID) AS Total_Bills,
SUM(B.[Total_Amount ] - B.[Paid_Amount ]) AS Pending_Amount,
AVG(B.[Total_Amount ] - B.[Paid_Amount ]) AS Avg_Pending
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ]
HAVING SUM(B.[Total_Amount ] - B.[Paid_Amount ]) > 0
ORDER BY Pending_Amount DESC;
--🧠 Q5. Report analysis per patient (with severity) Error
SELECT
P.Patient_ID,
P.[Patient_Name ],
COUNT(R.Report_ID) AS Total_Reports,
AVG(R.[Severity_Score ]) AS Avg_Severity,
MAX(R.[Severity_Score ]) AS Max_Severity
FROM [tblPatient_Infomation] P
JOIN [tblReports Table] R
ON P.Patient_ID = R.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ]
ORDER BY Avg_Severity DESC;
--🧠 Q6. Patients with no billing (LEFT JOIN case)
SELECT
P.Patient_ID,
P.[Patient_Name ],
COUNT(B.Bill_ID) AS Total_Bills,
ISNULL(SUM(B.[Total_Amount ]),0) AS Total_Billing,
ISNULL(AVG(B.[Total_Amount ]),0) AS Avg_Billing
FROM [tblPatient_Infomation] P
LEFT JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ]
HAVING COUNT(B.Bill_ID) = 0;
--🧠 Q7. Full report with date filter (VERY IMPORTANT) Error
SELECT
P.Patient_ID,
P.[Patient_Name ],
COUNT(R.Report_ID) AS Total_Reports,
AVG(R.[Severity_Score ]) AS Avg_Severity,
SUM(B.[Total_Amount ]) AS Total_Billing
FROM [tblPatient_Infomation] P
JOIN [tblReports Table] R
ON P.Patient_ID = R.[Patient_ID ]
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
WHERE R.[Reference_Added_Date ]
BETWEEN '2026-03-01' AND '2026-03-03'
GROUP BY
P.Patient_ID,
P.[Patient_Name ]
ORDER BY Total_Billing DESC;
--🧠 Q1. Find total billing per patient (with patient details)
SELECT
P.Patient_ID,
P.[Patient_Name ],
P.Gender,
P.Age,
SUM(B.[Total_Amount ]) AS Total_Billing
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ],
P.Gender,
P.Age
ORDER BY Total_Billing DESC;
--🧠 Q2. Count number of procedures per patient
SELECT
P.Patient_ID,
P.[Patient_Name ],
P.Gender,
Pr.[Procedure_Name ],
COUNT(Pr.[Procedure_ID ]) AS Total_Procedures
FROM [tblPatient_Infomation] P
LEFT JOIN [tbProcedures Table] Pr
ON P.Patient_ID = Pr.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ],
P.Gender,
Pr.[Procedure_Name ]
ORDER BY Total_Procedures DESC;
--🧠 Q3. Average procedure cost per patient
SELECT
P.Patient_ID,
P.[Patient_Name ],
P.Age,
Pr.[Procedure_Name ],
AVG(Pr.Cost) AS Avg_Cost
FROM [tblPatient_Infomation] P
JOIN [tbProcedures Table] Pr
ON P.Patient_ID = Pr.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ],
P.Age,
Pr.[Procedure_Name ]
ORDER BY Avg_Cost DESC;
--🧠 Q4. Find patients having more than 2 bills
SELECT
P.Patient_ID,
P.[Patient_Name ],
P.Gender,
B.[Payment_Status ],
COUNT(B.Bill_ID) AS Total_Bills
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ],
P.Gender,
B.[Payment_Status ]
HAVING COUNT(B.Bill_ID) > 2;
--🧠 Q5. Find average severity score by report status Error
SELECT
R.[Report_Status ],
R.[Patient_ID ],
P.[Patient_Name ],
R.[Findings ],
AVG(R.[Severity_Score ]) AS Avg_Severity
FROM [tblReports Table] R
JOIN [tblPatient_Infomation] P
ON R.[Patient_ID ] = P.Patient_ID
GROUP BY
R.[Report_Status ],
R.[Patient_ID ],
P.[Patient_Name ],
R.[Findings ];
--🧠 Q6. Find total pending amount per patient
SELECT
P.Patient_ID,
P.[Patient_Name ],
B.[Payment_Status ],
B.[Billing_Date ],
SUM(B.[Total_Amount ] - B.[Paid_Amount ]) AS Pending_Amount
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ],
B.[Payment_Status ],
B.[Billing_Date ]
HAVING SUM(B.[Total_Amount ] - B.[Paid_Amount ]) > 0;
--🧠 Q7. Count reports within a date range
SELECT
P.Patient_ID,
P.[Patient_Name ],
R.[Report_Status ],
R.[Reference_Added_Date ],
COUNT(R.Report_ID) AS Total_Reports
FROM [tblPatient_Infomation] P
JOIN [tblReports Table] R
ON P.Patient_ID = R.[Patient_ID ]
WHERE R.[Reference_Added_Date ]
BETWEEN '2026-03-01' AND '2026-03-03'
GROUP BY
P.Patient_ID,
P.[Patient_Name ],
R.[Report_Status ],
R.[Reference_Added_Date ];
--🧠 Q8. CONCAT example (Full name style + aggregation)
SELECT
P.Patient_ID,
CONCAT(P.[Patient_Name ], ' - ', P.Gender) AS Patient_Info,
B.[Payment_Status ],
B.[Billing_Date ],
SUM(B.[Total_Amount ]) AS Total_Billing
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY
P.Patient_ID,
CONCAT(P.[Patient_Name ], ' - ', P.Gender),
B.[Payment_Status ],
B.[Billing_Date ];
--🧠 Q9. Find procedures with high average cost (> 3000)
SELECT
Pr.[Procedure_Name ],
Pr.[Patient_ID ],
P.[Patient_Name ],
Pr.[Procedure_Date ],
AVG(Pr.Cost) AS Avg_Cost
FROM [tbProcedures Table] Pr
JOIN [tblPatient_Infomation] P
ON Pr.[Patient_ID ] = P.Patient_ID
GROUP BY
Pr.[Procedure_Name ],
Pr.[Patient_ID ],
P.[Patient_Name ],
Pr.[Procedure_Date ]
HAVING AVG(Pr.Cost) > 3000;
--🧠 Q10. Count patients by gender (with extra columns)
SELECT
P.Gender,
P.Age,
P.[Patient_Name ],
P.[Registration_Date ],
COUNT(P.Patient_ID) AS Total_Count
FROM [tblPatient_Infomation] P
GROUP BY
P.Gender,
P.Age,
P.[Patient_Name ],
P.[Registration_Date ]
ORDER BY Total_Count DESC;
--🧠 Q1. Categorize patients based on total billing
SELECT
P.Patient_ID,
P.[Patient_Name ],
SUM(B.[Total_Amount ]) AS Total_Billing,
CASE
WHEN SUM(B.[Total_Amount ]) > 10000 THEN 'High Value'
WHEN SUM(B.[Total_Amount ]) BETWEEN 5000 AND 10000 THEN 'Medium Value'
ELSE 'Low Value'
END AS Patient_Category
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
GROUP BY
P.Patient_ID,
P.[Patient_Name ];
--🧠 Q2. Payment status summary (Paid vs Pending)
SELECT
B.[Payment_Status ],
COUNT(B.Bill_ID) AS Total_Bills,
CASE
WHEN B.[Payment_Status ] = 'Paid' THEN 'Completed'
ELSE 'Pending Review'
END AS Status_Remark
FROM [tblBiling Table] B
GROUP BY
B.[Payment_Status ];
--🧠 Q3. Procedure cost category
SELECT
Pr.[Procedure_Name ],
Pr.[Patient_ID ],
Pr.Cost,
CASE
WHEN Pr.Cost > 5000 THEN 'Expensive'
WHEN Pr.Cost BETWEEN 2000 AND 5000 THEN 'Moderate'
ELSE 'Low Cost'
END AS Cost_Category,
COUNT(Pr.[Procedure_ID ]) AS Total_Count
FROM [tbProcedures Table] Pr
GROUP BY
Pr.[Procedure_Name ],
Pr.[Patient_ID ],
Pr.Cost;
--🧠 Q4. Find patients whose billing is above average
SELECT
P.Patient_ID,
P.[Patient_Name ],
B.[Total_Amount ]
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
WHERE B.[Total_Amount ] > (
SELECT AVG([Total_Amount ])
FROM [tblBiling Table]
);
--🧠 Q7. Find patients with maximum bill amount
SELECT
P.Patient_ID,
P.[Patient_Name ],
B.[Total_Amount ]
FROM [tblPatient_Infomation] P
JOIN [tblBiling Table] B
ON P.Patient_ID = B.[Patient_ID ]
WHERE B.[Total_Amount ] = (
SELECT MAX([Total_Amount ])
FROM [tblBiling Table]
);
Queries without alias
**************************************************************************************************
🧠 Q1. Find total revenue, total bills, and average bill amount per patient
🧠 Q2. Find patient procedure stats (count, avg cost, max cost)
🧠 Q3. Combine billing + procedures (very powerful)
🧠 Q4. Pending amount + number of pending bills per patient
🧠 Q5. Report analysis per patient (with severity)
🧠 Q6. Patients with no billing (LEFT JOIN case)
🧠 Q7. Full report with date filter (VERY IMPORTANT)
No comments:
Post a Comment