You are on page 1of 54

CSCI-235 Database

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.

SELECT * FROM ward;


Ward_Code Ward_Name Ward_Head
w001 Neurosurgery Mandy JJ
w002 Cardiology William Joe
w003 Plastic Surgery Mariah Christ
w004 Pediatrics
Null value
w005 ICU Esther Bob
w006 Orthopedics Candy Norbert

* represents all the columns in a table


Query 2: Get the patient ID, patient name and
patient date of birth from patient table. Rename the
column pat_DOB to Date of Birth.

SELECT pat_ID, pat_name, pat_DOB AS Date of Birth


FROM patient;

Pat_ID Pat_name Date of Birth


1 John 04-May-47
2 Cindy 12-Dec-47
3 Helena 03-Jan-50
4 Michael 12-Jun-38
5 Smith 01-Jul-46

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;

Doc_ID Doc_Salary Doc_Name


D1 15,000 Victor
D2 10,000 Julia
D3 22,000 Nicky
D4 23,000 Albert
D5 13,000 Jasmine
D6 13,000 Samantha
Columns can be displayed with any order specified in
the query
Query 4: Get all the details from ward
table where the ward code equal to
w002.

SELECT * FROM ward


WHERE ward_code = ‘w002’;
Ward_Code Ward_Name Ward_Head
w002 Cardiology William Joe

WHERE clause allows conditions to be


specified.
Comparison Operators
•In addition to equal sign, the WHERE clause can
contain other comparison operators as follow:

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.

SELECT doc_ID, doc_name, doc_salary


FROM doctor
WHERE doc_salary > = 15000
ORDER BY doc_name;
Doc_ID Doc_Name Doc_Salary
D4 Albert 23,000
D3 Nicky 22,000
D1 Victor 15,000
*Query 6: Get the doctor ID, doctor
name of the doctors whose salary is not
equal to 15000.
SELECT doc_ID, doc_name FROM doctor
WHERE doc_salary <> 15000;

Doc_ID Doc_Name Doc_Salary

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.

SELECT ward_code, ward_name, ward_head


FROM ward
WHERE ward_head IS NULL;
Ward_Code Ward_Name Ward_Head

w004 Pediatrics <Null>

IS NULL operator is used to retrieve a column


with null value.
Boolean Operator :combine the results of two
conditions to produce a single result.

Condition Condition Result


(1) (2)
AND TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
OR TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
Query 8:Get the ID, name and gender for the
male patients with the ID more than value 1.

SELECT pat_ID, pat_name, pat_DOB, pat_gender


FROM patient
WHERE pat_ID>1 AND pat_gender = ‘M’;
Pat_ID Pat_name Pat_DOB Pat_gender
4 Michael 12-Jun-38 M
5 Smith 01-Jul-46 M
6 Tim 16-Nov-59 M

If two conditions are connected by AND operator,


rows are retrieved when both conditions are true
Query 9:Get the ID, name and gender
for either the male patients or patient
with ID more than value 1.

SELECT pat_ID, pat_name, pat_DOB pat_gender FROM


patient
WHERE pat_ID>1 OR pat_gender = ‘M’;
Pat_ID Pat_name Pat_DOB Pat_gender
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

If two conditions are connected by OR operator, rows


are retrieved when either one condition is true
Query 10Get the ID, name, age and salary for the doctor
whose ID is more than value 1, and age is more than 30, and
earns more than 22000.

SELECT doc_ID, doc_name, doc_age,


doc_salary
FROM doctor
WHERE doc_ID>1 AND doc_age >30
AND doc_salary > 22000 ;
Doc_ID Doc_Name Doc_Age Doc_Salary
D4 Albert 45 23,000
Query 11:Get the ID, name, age and salary for the
doctor whose ID is more than value 1 or age is more
than 30, or earns more than 22000.

SELECT doc_ID, doc_name, doc_age, doc_salary


FROM doctor
WHERE doc_ID>1 OR doc_age >30
OR doc_salary > 22000 ;

Doc_ID Doc_Name Doc_Age Doc_Salary

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

SELECT doc_ID, doc_age, doc_salary, ward_code FROM


doctor WHERE (doc_age >25 AND doc_salary>12000)
OR ward_code = ‘w001’;

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

Note: all the expressions within the parentheses will be


executed 1st in the sequence from left to right
*Query 13:Get ID, name and gender of non
male patient whose patient ID is not equal to
2.

SELECT pat_ID, pat_name, pat_gender FROM


patient
WHERE pat_gender<> ‘M’ AND pat_ID <> 2
Pat_ID Pat_name Pat_gender
3 Helena F
IN and BETWEEN Operator
IN Operator
 Use to specify a series of individual
expressions
 The result is true if the value conforms to
one of the expressions
BETWEEN Operator
 Use to specify a range of condition
Query 14: Get all details of wards with the ward_code equal to
w001 or w003 or w006. Arrange the result according to the
alphabetical order of ward head.

SELECT * FROM ward SELECT * FROM ward


WHERE ward_code =‘w001’
WHERE ward_code IN (‘w001’,
‘w003’, ‘w006’) ward_code=‘w003’ or
ORDER BY ward_head; ward_code=‘006’
ORDER BY ward_head;

Ward_Code Ward_Name Ward_Head


w006 Orthopedics Candy Norbert
w001 Neurosurgery Mandy JJ
w003 Plastic Surgery Mariah Christ
Query 15
SELECT * FROM ward
WHERE ward_code IN (‘w001’, ‘w003’, ‘w006’)
AND ward_name = ‘Orthopedics’;

SELECT * FROM ward


WHERE (ward_code = ‘w001’ OR ward_code =
‘w003’ OR ward_code
=‘w006’)
AND
Ward_Code
ward_name =
Ward_Name
‘Orthopedics’
Ward_Head
w006 Orthopedics Candy Norbert
Query 16:Get all the details of wards which
have the ward code not equal to w001 or
w003 or w006.

SELECT * FROM ward


WHERE ward_code NOT IN (‘w001’,
‘w003’, ‘w006’);
Ward_Code Ward_Name Ward_Head

w002 Cardiology William Joe


w004 Pediatrics <NULL>

w005 ICU Esther Bob

NOT IN operator is used retrieve the rows which do not


conform to the expressions specified in the query.
Query 17Get all the details for patients
whose name is not equal to John or Tim.
SELECT * FROM patient
WHERE pat_name NOT IN (‘John’,
‘Tim’);
Pat_ID Pat_name Pat_DOB Pat_gender
2 Cindy 12-Dec-47 F
3 Helena 03-Jan-50 F
4 Michael 12-Jun-38 M
5 Smith 01-Jul-46 M
Query 18:Get the details for patients whose
patient ID is either 2 or 3 or 4.
SELECT * FROM patient
SELECT * FROM patient
WHERE pat_ID >=2 AND
WHERE pat_ID BETWEEN 2 AND 4;
Pat_ID <= 4
Pat_ID Pat_name Pat_DOB Pat_gender
2 Cindy 12-Dec-47 F
3 Helena 03-Jan-50 F
4 Michael 12-Jun-38 M

Note: lowest and highest boundary values in the BETWEEN operator


Query 19:Get the details of patients who
were born in the year of 1947.

SELECT * FROM patient


WHERE pat_DOB BETWEEN ‘01-Jan-47’
AND ‘31-Dec-47’;
Pat_ID Pat_name Pat_DOB Pat_gender

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.

SELECT * FROM patient


WHERE pat_ID NOT BETWEEN 2 AND
4;
Pat_ID Pat_name Pat_DOB Pat_gender
1 John 04/05/47 M
5 Smith 01/07/46 M
6 Tim 16/11/59 M
LIKE Operator
Use to search a character pattern
% is used to match zero or more
characters, any number of characters
_ is used to match exactly one
character
Query 21: Get all the details for patients whose
name starts with character “T”

SELECT * FROM patient


WHERE pat_name LIKE ‘T%’;
Pat_ID Pat_name Pat_DOB Pat_gender
6 Tim 16/11/59 M
Query 22:Get all the details for patients whose
name contains character “i” as the second
character. ? I ? ? ? ?

SELECT * FROM patient


WHERE pat_name LIKE ‘_i%’;
Pat_ID Pat_name Pat_DOB Pat_gender
2 Cindy 12-Dec-47 F
4 Michael 12-Jun-38 M
6 Tim 16-Nov-59 M
Query 23:Get all the details for male patients
whose name contains character “i” as the
second character.

SELECT * FROM patient


WHERE pat_name LIKE ‘_i%’ AND
pat_gender =‘M’;
Pat_ID Pat_name Pat_DOB Pat_gender
4 Michael 12-Jun-38 M
6 Tim 16-Nov-59 M
Aggregate Functions
MAX function
 Use to compute the largest value
MIN function
 Use to compute the smallest value
AVG function
 Use to compute the average value
SUM function
 Use to compute the total value
COUNT function
 COUNT(*) – compute the number of rows
 COUNT(column_name) – compute the value in the column
Query 24: Get the highest salary from doctor
table. Name the result heading as Highest
Salary.

SELECT MAX(doc_salary) AS Highest Salary


FROM doctor;

Highest Salary
------ ------ ------ ------ ------ ------ ------

23000
Query 25:Get the lowest salary from doctor
table. Name the result heading as Lowest
Salary.

SELECT MIN(doc_salary) AS Lowest Salary


FROM doctor;

Lowest Salary
------ ------ ------ ------ ------ ------ ------

10000
Query 26: Get the total salary from doctor
table. Name the result heading as Lowest
Salary.

SELECT SUM(doc_salary) AS Total Salary


FROM doctor;

Total Salary
------ ------ ------ ------ ------ ------ ------

96000
Query 27: Get the average salary from doctor
table.

SELECT AVG(doc_salary) FROM doctor;

AVG (doc_salary)
------ ------ ------ ------ ------ ------ ------

16000
Query 28:Count the total number of rows
of Treatment table.

SELECT COUNT(*) FROM treatment;


Treat_cod Treat_date Doc_I Pat_ID
e D
COUNT (*) T001 31-May-03 D2 3
------ ------ ------ ------ ------ ------ ------
T002 01-Jun-03 D3 1
8 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
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

COUNT ( distinct doc_ID) T006 04-Jun-03 D4 2


------ ------ ------ ------ ------ ------ ------ ------ ------ ------
T007 06-Jun-03 D3 5
5
T008 05-Jul-03 D4 4

Note: DISTINCT: eliminates the


counting for similar doctor ID. In another
words, columns with the same doctor ID is
only counted 1.
Query 30:Calculate the total number of doctors
work for various wards. Display both the ward code
and the count for each ward code.

SELECT ward_code, COUNT(Doc_id) AS Number of Doctors


FROM doctor
GROUP BY ward_code;

Ward Code Number of Doctors Doc_ID Doc_Na Doc_A Doc_Salary Ward_cod


----------------------------------- ---------------------------------------------------------------- me ge e
W001 2 D1 Victor 34 15,000 W001

W004 1 D2 Julia 25 10,000 W001


W006 1
D3 Nicky 46 22,000 W004
W002 2
D4 Albert 45 23,000 w006

D5 Jasmine 29 13,000 W002

D6 Samant 26 13,000 w002


ha
Query 31: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.

SELECT ward_code, SUM(doc_salary) AS Total Salary


FROM doctor
GROUP BY ward_code;

Ward Code Total Salary


----------------------------------- ----------------------------------------------------------------

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;

Ward Code Total Salary


----------------------------------- ----------------------------------------------------------------

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.

SELECT pat_name FROM patient


WHERE pat_ID IN
(SELECT MIN (pat_ID) FROM patient);

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>

SELECT AGGREGATE <column names> FROM <table name>


GROUP BY <column name>
HAVING <conditions by Boolean, IN, BETWEEN, LIKE>
ORDER BY <column names>
End of Topic 4
Announcement
Test ONE
Week 6 (7/6)
Topic 1 / 2/ 3
Announcement

Assignment Two (uploaded)


Due date : week 7 (14/6)
Assignment (2)
Table 1: Explain
A relation consists of
Relation schema
Describe / Specifies the relation’s name, and the
fields
relationship

SQL Create table command


Print screen of your command

SQL Insert table command


Print screen of your command

You might also like