4.structured Query Language - Query
4.structured Query Language - Query
Topic 4
Structured Query Language -
Query
Objectives
Learn the management and control of data
access by using Structured Query Language
Learn the SELECT statement to perform
simple queries from a single table
Learn the comparison and Boolean operators
Learn the IN and BETWEEN operators
Learn the LIKE operators
Learn the aggregate functions
Topics to be Covered
Hospital Database
Query
SELECT statement
Comparison and Boolean operators
IN and BETWEEN operators
LIKE operators
Aggregate functions
4.1 Hospital Database
Hospital System Patient
Ward pat_ID <pk>
ward_code <pk> pat_name
ward_name pat_DOB
ward_head pat_gender
Doctor Treatment
doc_ID <pk> treat_code <pk>
doc_name treat_date
doc_age doc_ID <fk>
doc_salary pat_ID <fk>
ward_code <fk>
Ward Table
Ward_Code Ward_Name Ward_Head
w001 Neurosurgery Mandy JJ
w002 Cardiology William Joe
w003 Plastic Surgery Mariah Christ
w004 Pediatrics <NULL>
w005 ICU Esther Bob
w006 Orthopedics Candy Norbert
Doctor Table
Doc_ID Doc_Name Doc_Age Doc_Salary Ward_code
D1 Victor 34 15,000 W001
D2 Julia 25 10,000 W001
D3 Nicky 46 22,000 W004
D4 Albert 45 23,000 w006
D5 Jasmine 29 13,000 W002
D6 Samantha 26 13,000 w002
Patient Table
Pat_ID Pat_name Pat_DOB Pat_gender
1 John 04-May-47 M
2 Cindy 12-Dec-47 F
3 Helena 03-Jan-50 F
4 Michael 12-Jun-38 M
5 Smith 01-Jul-46 M
6 Tim 16-Nov-59 M
Treatment Table
Treat_code Treat_date Doc_ID Pat_ID
T001 31-May-03 D2 3
T002 01-Jun-03 D3 1
T003 02-Jun-03 D3 4
T004 02-Jun-03 D5 2
T005 03-Jun-03 D1 6
T006 04-Jun-03 D4 2
T007 06-Jun-03 D3 5
T008 05-Jul-03 D4 4
4.2 Query
Query 1: Get the full details of all wards.
Specific
6
columns
Tim
can be specified
16-Nov-59
and displayed
Query 3: Get the doctor ID, doctor
salary, and doctor name from doctor
table.
SELECT doc_ID, doc_salary, doc_name
FROM doctor;
Comparison Operators
< Less than
> Greater than
>= Greater than or equal
<= Less than or equal
<> (!=) Not equal
Query 5 Get the ID, name and salary for the doctors
who earn more than or equal 15000. Arrange the result
according to the alphabetical order of doctor name.
D2 Julia 10,000
D3 Nicky 22,000
D4 Albert 23,000
D6 Samantha 13,000
Query 7:Get the code, name and head for the
wards which do not have a ward head.
D2 Julia 25 10,000
D3 Nicky 46 22,000
D4 Albert 45 23,000
D5 Jasmine 29 13,000
D6 Samantha 26 13,000
Query 12:Get the ID, age, salary and ward code for the doctors
who are either 25 years old and earn more than 12000, or work for
ward with ID w001
1 John 04-May-47 M
2 Cindy 12-Dec-47 F
Query 20:Get the details of patients
whose patient ID is not 2, 3 or 4.
Highest Salary
------ ------ ------ ------ ------ ------ ------
23000
Query 25:Get the lowest salary from doctor
table. Name the result heading as Lowest
Salary.
Lowest Salary
------ ------ ------ ------ ------ ------ ------
10000
Query 26: Get the total salary from doctor
table. Name the result heading as Lowest
Salary.
Total Salary
------ ------ ------ ------ ------ ------ ------
96000
Query 27: Get the average salary from doctor
table.
AVG (doc_salary)
------ ------ ------ ------ ------ ------ ------
16000
Query 28:Count the total number of rows
of Treatment table.
T004 02-Jun-03 D5 2
T005 03-Jun-03 D1 6
T006 04-Jun-03 D4 2
T007 06-Jun-03 D3 5
T008 05-Jul-03 D4 4
Treat_cod Treat_date Doc_I Pat_ID
Query 29 e
T001 31-May-03
D
D2 3
T002 01-Jun-03 D3 1
T003 02-Jun-03 D3 4
SELECT COUNT(distinct doc_ID)
T004 02-Jun-03 D5 2
FROM treatment;
T005 03-Jun-03 D1 6
W001 25000
W002 26000
W004 22000
W006 23000
Query 32:Calculate the total salary of doctors work for the same
wards. Display both the ward code and the total salary for the
doctors working for the same ward. Only display the total salary
more than 22000.
SELECT ward_code, SUM(doc_salary) AS Total Salary
FROM doctor
GROUP BY ward_code
HAVING SUM(doc_salary) >22000;
W001 25000
W002 26000
W006 23000 Note: HAVING - is used to specify
certain conditions on rows, retrieved
by using GROUP BY clause.
HAVING clause should only be used
when there is a preceded GROUP BY
clause.
Query 33:Get the name of the patient
with smallest ID.
Pat_name
---- ---- ----- ----- ----- ----- ----
John
4.3 Summary
4.3 Summary
SELECT <column names> FROM <table name>
WHERE <conditions by Boolean, IN, BETWEEN, LIKE>
ORDER BY <column names>