Untitled
Untitled
Untitled
Here we build and use a database for a hypothetical Health Record Management
System, HRMS
*/
USE master;
GO
USE HRMSDB;
GO
-- Insert 3 more Pharmacy personnel (from the 8 employees you inserted above)
GO
SELECT statement
- SELECT statement is used to query tables and views,
SYNTAX :
SELECT select_list [ INTO new_table ]
[ FROM table_source ]
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
USE HRMSDB
--EXERCISE # 01
--Write SELECT statement to retrive the entire columns of Employee table by
explicitly listing each column.
--NOTE (1) List all the columns to be retrived on SELECT statement
--NOTE (2) The name of each column must be separated by comma
SELECT
empId,empFName,empLName,SSN,DoB,gender,salary,employedDate,strAddress,apt,city,
[state],zipCode,phoneNo,email,empType
FROM Employee
--Repeat the previous exercise, but SSN and DoB must be displated as the 1st and
2nd column
--NOTE : The order of columns list determine their display, regardless of the order
of defination in source table.
SELECT
SSN,DOB,empId,empFName,empLName,gender,salary,employedDate,strAddress,apt,city,
[state],zipCode,phoneNo,email,empType
FROM Employee
--EXERCISE # 03
--Write SELECT statement to retrive the entire columns of Employee table using
"star" character (*)
--NOTE : This method is frequently used for quick test, refrain using this short
cut in production environment
SELECT *
FROM Employee
--EXERCISE # 04
--Write SELECT statement to retrive empId, empFName, empLName, phoneNo and email
columns of Employee table.
SELECT empId,empFName,empLName,phoneNo,email
FROM Employee
/*
--SELECT statement can be used to perform calculations and manipulation of data
Operators,
'+' : Adding or Concatinate
'-' : Subtract
'*' : Multiply
'/' : Divide
'%' : Modulo
The result will appear in a new column, repeated once per row of result set
Note : Calculated expressions must be Scalar, must return only single value
Note : Calculated expressions can operate on other colums in the same row
*/
SELECT 5 % 3
--EXAMPLE :
--Calculate the amount each type of medicine worth in stock
--EXERCISE # 05
--The unit price of each medicine is increased by 6%. Provide the list of medecine
with
--their respective rise in price.
--EXERCISE # 07
--Write a code to display employee's Full Address, and Phone Number
SELECT CONCAT(strAddress,' ', apt,' ', city,' ', [state],' ', zipCode) AS
[Address],phoneNo
FROM Employee
--CONCAT Function
--Is string function that joins two or more string values in an end-to-end manner.
--SYNTAX: CONCAT ( string_value1, string_value2, string_value2, ...)
--EXAMPLE : Write a code to display the Full Name of all employees using CONCAT
function
--EXERCISE # 08
--Write a code to display employee's Full Address, and Phone Number using CONCAT
function
SELECT CONCAT(strAddress,' ', apt,' ', city,' ', [state],' ', zipCode) AS
[Address],phoneNo
FROM Employee
/*
DISTINCT statement
SQL query results are not truly relational, are not always unique, and not
guranteed order
Even unique rows in a source table can return duplicate values for some
columns
DISTINCT specifies only unique rows to apper in result set
--
*/
--EXAMPLE :
--Write a code to retrive the year of employement of all employees in Employee
table
SELECT employedDate
FROM Employee
--Notice there are redudant instances on the result set. Execute the following code
--and observer the change of result set
--EXERCISE # 09
--Write a code that retrive the list of dosages from MedicinePrescribed table, use
DISTINCT to eliminate duplicates
Table Aliases
- Used in FROM clause to provide convenient way of refering table elsewhere
in the query, enhance readability
*/
--EXAMPLE :
--Display all columns of Disease table with full name for each column
SELECT dId,dName,dCategory,dType
FROM Disease
--EXERCISE # 10
--Write the code to display the 10% rise on the price of each Medicine, use
appropriate column alias for comoputed column
SELECT mId,brandName,qtyInStock, SUM(unitprice * 10 + unitprice ) /100 AS [+10% New
Price]------?????
FROM medicine
GROUP BY mId,brandName,qtyInStock,unitPrice
--EXERCISE # 11
--Modify the above code to include the new price column with the name 'New Price'
--EXAMPLE :
--Using '=' signs to assign alias to columns
--EXERCISE # 12
--Use '=' to assign alias for the new price of all medecines with price rise by 13%
SELECT mId,brandName,qtyInStock, SUM(unitprice * 13) /100 + unitprice AS [New
Price]------?????
FROM medicine
GROUP BY mId,unitPrice
--EXAMPLE :
--Using built in function on a column in the SELECT list. Note the name of column
is used as input for the function
--EXAMPLE :
--Write a code to display the the first name and last name of all patients. Use
table alias to refer the columns
SELECT *
FROM Patient
--EXERCISE # 13
--Assign the appropriate aliases for columns displayed on previous exercise.
SELECT pFName , pLName
FROM Patient
/*
Logical Processing Order on Aliases
- FROM, WHERE, and HAVING clauses process before SELECT
- Aliases created in SELECT clauses only visible to ORDER BY clause
- Expressions aliased in SELECT clause may be repeated elsewere in query
--EXAMPLE
--The following code will run free from error, since alias dicleration done before
alias used by ORDER BY clause
--EXAMPLE
--The following code will err out, since alias decleration done after the execution
of WHERE clause
SELECT empId AS [Employee ID], empFName AS [First Name], empLName AS [Last Name]
FROM Employee
WHERE [empFName] LIKE 'R%'
/*
Simple CASE Expression
- Can be used in SELECT, WHERE, HAVING AND ORDER BY clauses
- Extends the ability of SELECT clause to manipulate data as it is retrived
- CASE expression returns a scalar (single-valued) value based on conditional
logic
*/
--EXAMPLE :
--Simple CASE
/*
First
ALTER TABLE Disease
DROP COLUMN dCategory
*/
SELECT * FROM Disease
--EXAMPLE :
--Searched CASE
use HRMSDB
--EXERCISE # 14
--Use simple CASE to list the brand names of medecines available as per Medecine Id
SELECT *
FROM Medicine
SELECT mId,brandName,
(CASE brandName
WHEN 'xaleto' THEN 'Y'
WHEN 'Hexalen' THEN 'Z'
END) AS [brandName IN FULL] -----????
FROM Medicine
--EXERCISE # 15
--Searched CASE to identify label the price of a medicine cheap, moderate and
expensive. Price of cheap ones
--is less than 15 USD, and expensive ones are above 45 USD. Display Medecine Id,
Generic Name, the unit price and
--category of the price.
SELECT mId,unitPrice,genericName,
(CASE
WHEN unitPrice >45.00 THEN 'Expensive'
WHEN unitPrice <15.00 THEN 'Cheap'
ELSE 'Moderate'
END) AS [Category of price]
FROM Medicine
-- Display MRN, Full Name and Gender (as Female and Male) of all Patients -- use
case statement for gender
SELECT mrn,pFName,pLName,
(CASE gender
WHEN 'F' THEN 'Female'
WHEN 'M' THEN 'Male'
END) AS [Gender In Full]
FROM patient
/*
SORTING DATA
- Used to desplay the output of a query in certain order
- Sorts rows in results for presentation purpose
- Uses ORDER BY clause
- ORDER BY clause
- The last clause to be logically processed in SELECT statement
- Sorts all NULLs together
- Refers to columns by name, alias or ordinal position (not
recommended)
- Declare sort order with ASC or DESC
- SYNTAX :
SELECT <select_list>
FROM <table_source>
ORDER BY <order_by_list> [ASC|DESC]
*/
-- Also order by last name - and return the names as lastname, firstname
--EXERCISE # 16
--Write a code to retrive details of Patients in assending order of last names
SELECT pLName
FROM Patient
GROUP BY pLName
use HRMSDB
--EXERCISE # 17
--Write a code to retrive details of Employees in descending order of DoB
--Note : Use column alias on ORDER BY clause
SELECT DoB
FROM Employee
GROUP BY DoB
ORDER BY 1 DESC
--EXAMPLE :
--ORDER BY clause can uses more than one column
*/
--EXERCISE # 18
--Display the list of Patients in the order of the year of their treatement. Use
their First name to order the ones that came
--to the hospital on same year.
SELECT registeredDate,pFName
FROM Patient
GROUP BY registeredDate,pFName
/*
FITERING DATA
- Used to retrive only a subset of all rows stored in the table
- WHERE clause is used to limit which rows to be retured
- Data filtered server-side, can reduce network trafic and client memory
usage
-WHERE clause
- use predicates, expressed as logical conditions
- rows which predicate evaluates to TRUE will be accepted
- rows of FALSUE or UNKNOWNS filitered out
- in terms of precidence, it follows FROM clause
*** - can't see the alias declared in SELECT clause
- can be optimized to use indexes
*/
--EXAMPLE :
--Find below a code to get Employee information (across all columns) - who are
contractors,
--Note : P: Principal Associate, A: Associate, C: Contractor, M:Manager
--EXAMPLE :
--Find below a code to get Employee information (across all columns) - for
Contract and Associate
--EXERCISE # 19
--Find the list of employees with salary above 90,000
SELECT *
FROM Employee
WHERE salary >90000.00
--EXERCISE # 20
--Find the list of employees with salary range betwen 85,000 to 90,000 inclusive
SELECT *
FROM Employee
WHERE salary BETWEEN 85000.00 AND 90000.00
--EXERCISE # 21
--Find the list of patients that had registration to the hospital in the year 2018
--Use 'BETWEEN' operator
SELECT *
FROM Patient
WHERE registeredDate BETWEEN '2018-01-01' AND '2018-12-31'
--EXERCISE # 22
--Get Employees born in the year 1980 (from 1980-01-01 - 1980-12-31)using 'BETWEEN'
operator
SELECT *
FROM Employee
WHERE DoB BETWEEN '1980-01-01' AND '1980-12-31'
--EXERCISE # 23
--Get Employees born in the year 1980 using 'AND' operator
SELECT *
FROM Employee
WHERE DoB BETWEEN '1980-01-01' AND '1980-12-31'
--We can also use the YEAR() function to do the same for the above exercise - but
it is NOT recommended
--EXERCISE # 24
--Write a code that retrives details of Patients that live in Silver Spring.
SELECT *
FROM Patient
WHERE city IN ('Silver Spring')
--EXERCISE # 25
--Write a code retrive the information of contractor Employees with salary less
than 75000.
SELECT *
FROM Employee
WHERE salary <75000.00
--EXERCISE # 26
--Write a code to retrive list of medicines with price below 30 USD.
SELECT *
FROM Medicine
WHERE unitPrice <30.00 ---- Re insert data
--EXERCISE # 27
--Write a code to retrive patients that live in Miami and Seattle
SELECT *
FROM Patient
WHERE city IN ('Miami','Seattle')
--EXERCISE # 28
--Write a code to retrive patients that are not living in Silver Spring
SELECT *
FROM Patient
WHERE city <> ('Silver Spring')
/*
STRING FUNCTIONS
- CONCATENATE
- Returns a character string that is the result of concatenating
string_exp2 to string_exp1.
- SYNTAX
CONCAT( string_exp1, string_exp2, ...)
- LEFT
- Returns the leftmost count characters of string_exp.
- SYNTAX
LEFT( string or string_exp, number_of_characters )
- RIGHT
- To extract a substring from a string, starting from the right-most
character.
- SYNTAX
RIGHT( string or string_exp, number_of_characters )
- CHARINDEX
- returns the location of a substring in a char/string, from the left
side.
- The search is NOT case-sensitive.
- SYNTAX :
CHARINDEX( substring, string, [start_position] )
-> given a string and character CHARINDEX function returns the relative
location (index) of the first occurrence of that character in the string
-LENGTH
- Returns the number of characters in string_exp, excluding trailing
blanks.
- SYNTAX
LEN( string or string_exp )
e.g. LEN ('Complete') -> 8
-LTRIM function
- removes all space characters from the left-hand side of a string.
- SYNTAX
LTRIM( string_exp )
- RTRIM
- removes all space characters from the right-hand side of a string.
- SYNTAX
RTRIM( string_exp )
- TRIM
- removes all space characters from the both sides of a string.
- SYNTAX
TRIM( string_exp )
https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/string-
functions?view=sql-server-2017
*/
SELECT *
FROM Employee
--EXERCISE # 29
--Write a code to display full name for employees
SELECT empFName+'', +empLName AS [full name]
FROM Employee
--EXERCISE : 30
--Get the last four digists of SSN of all Employees together with their id and full
name
SELECT empFName+'', +empLName AS [full name],empId, RIGHT(SSN,4) AS [last four of
social]
FROM Employee
-- Question Write a query to get the Employee with the last four digits of the SSN
is '3456'
-- Q2 - Write a query to get the Employee with the last four digits of the SSN is
'3456' and with DoB = '1980-09-07'
--EXERCISE # 31
--Write a code to retrive the full name and area code of their phone number, use
Employee table
SELECT empFName+' '+empLName AS [Full Name], LEFT (phoneNo,5) AS [Area code]
FROM Employee
ORDER BY[Full Name]
--EXERCISE # 32
--Write a code to retrive the full name and area code of their phone number,
(without a bracket). use Employee table
SELECT empFName+' '+empLName AS [Full Name], SUBSTRING (phoneNo,2,3) AS [Area code]
FROM Employee
ORDER BY[Full Name]
--EXAMPLE # 33
--Run the following codes and explain the result with the purpose of CHARINDEX
function
SELECT CHARINDEX('O', 'I love SQL')
---return 4 i.e counted from left to right, the index of the first accurance of '0'
--EXERCISE # 34
--Modify the above code, so that the output/result will have appopriate column name
SELECT CHARINDEX('O', 'I love SQL') AS [Index for letter 'O']
SELECT CHARINDEX (' ', '9 mccormick circle') -------???
--EXAMPLE # 35
--Write a code that return the index for letter 'q' in the sentence 'I love sql'
SELECT CHARINDEX('q', 'I love SQL') [Index for letter 'Q']
--EXERCISE # 36
--Use the CHARINDEX() function to retrieve the house(building) number of all our
employees
SELECT *
FROM Employee
SELECT CHARINDEX(' ',apt)
FROM Employee ------------------------????
--EXAMPLE :
--Run the following code and explain the result with the purpose of LEN function
SELECT LEN('I love SQL') -------10
--- the LEN() function mainly returns the length of a string
--EXAMPLE :
--Reterive the email's domain name for the entiere employees.
--NOTE : Use LEN(), CHARINDEX() and RIGHT()
--EXERCISE # 37
--Assign a new email address for empId=EMP05 as 'sarah.Kathrin@aol.us'
UPDATE Employee
SET email = 'sarah.Kathrin@aol.us'
WHERE empId = 'EMP05'
--EXERCISE # 38
--Using wildcards % and _ ('%' means any number of charactes while '_' means single
character)
--mostly used in conditions (WHERE clause or HAVING clause)
--Get Employees whose first name begins with the letter 'P'
SELECT *
FROM Employee
WHERE empFName LIKE 'P%'
--EXERCISE # 39
--Get the list of employees with 2nd letter of their frst name is 'a'
SELECT *
FROM Employee
WHERE empFName LIKE '_a%'
--EXERCISE # 40
--Get full name of employees with earning more than 75000. (Add salary information
to result set)
SELECT empFName+'', +empLName AS [full name],salary
FROM Employee
WHERE salary >75000.00
--EXERCISE # 41
--Get Employees who have yahoo email account
--NOTE : the code retrives only the list of employee's with email account having
'yahoo.com'
SELECT *
FROM Employee
WHERE email LIKE 'yahoo.com'
--EXERCISE # 42
--Get Employees who have yahoo email account
--NOTE : Use RIGHT string function.
--EXERCISE # 43
--Get Employees who have yahoo email account
--NOTE : The code must checke only 'yahoo' to retrive the entire employees with
yahoo account
--EXERCISE # 44
--Create a CHECK constraint on email column of Employee table to check if it's a
valid email address
--NOTE : Assume a valid email address contains the '@' character
/* Aggregate Functions
--EXERCISE # 45
--Get total number of Employees
--EXERCISE # 46
--Get number of Employees not from Maryland
--OR
--EXERCISE # 47
--Get the number of Principal Employees, with emptype = 'P')
--EXERCISE # 48
--Get the Minimum salary
--EXERCISE # 49
--Modify the above code to include the Minimum, Maximum, Average and Sum of the
Salaries of all employees
--EXERCISE # 50
--Get Average Salary of Female Employees
--EXERCISE # 51
--Get Average Salary of Associate Employees (empType = 'A')
--EXERCISE # 52
--Get Average salaries for each type of employees?
--EXERCISE # 53
--Get Average Salary per gender
--EXERCISE # 54
--Get Average Salary per sate of residence
--EXERCISE # 55
--Get Employees earning less than the average salary of all employees
/*
NOTE : An aggregate may not appear in the WHERE clause unless it is in a subquery
contained in a
HAVING clause or a SELECT list, and the column being aggregated is an outer
reference.
SUBQUERIES
- Is SELECT statement nested within another query, (queries within queries)
- Can be Scalar, multi-valued, or table-valued
- Scalar subquery : return single value, outer queries handle only
single result
- Multi-valued : return a column table, outer queries must be able to
handle multipe results
- The result of inner query (subquery) are returned to the outer query
- Enables to enhance ability to create effective queries
- Can be either Self-Contained or Correlated
- Self-Contained
- have no dependency to outer query
- Correlated
- one or more column of subquery depends on the outer query.
- Inner query receives input from the outer query & conceptually
executes once per row in it.
- Writing correlated subqueries
- if inner query is scalar, use comparision opetators as '=',
'<', '>', and '<>'in WHERE clause
- if inner query returns multi-values, use and 'IN' predicate
- plan to handle 'NULL' results as required.
*/
--EXERCISE # 58
--Get list of Employees with earning less than the average salary of Associate
Employees
--Note : Use a scalar subquery which is self contained to solve the problem
--EXERCISE # 59
--Get Principal Employees earning less than the average of Contractors
--EXERCISE # 60
--Get Principal Employees earning less than or equal to the average salary of
Pricipal Employees
--EXERCISE # 61
--Get Contractors earning less than or equal to the average salary of Contractors
--EXERCISE # 62
--Get Associate Employees earning less than or equal to the average salary of
Associate Employees
--EXERCISE # 63
--Get Managers earning less than or equal to the average salary of Managers
--EXERCISE # 64
--Get the count of Employees based on the year they were born
--EXERCISE # 65
--Get list of patients diagnoized by each doctor
--NOTE : Use multi-valued subquery to get the list of doctors from 'Doctors' table
/*
Note : Here is the logical structure of the outer query for the above example
Subqueries that have multi-values will follow the same fashion
SELECT docId,MRN,diagDate,diagResult
FROM Diagnosis AS D
WHERE D.docId IN ( MD01, MD02, MD03, MD04 )
ORDER BY DOCID
*/
--EXERCISE # 67
--Get list of patients diagnoized for each disease type
--NOTE : Use multi-valued subquery to get the list of disease from 'Disease' table
--EXAMPLE :
--Get Employees who are earning less than or equal to the average salary of their
gender
--NOTE : Use correlated subquery
--EXERCISE # 68
--Retrieve all Employees earning less than or equal to their groups averages
--NOTE : Use correlated subquery,
--EXAMPLE
--Better way to deal with previous exercise is to use 'JOIN', run the following and
confirm
--try to analizye how the problem is sloved, the next section try to introduce
about JOINing tables
UPDATE Employee
SET department = 'KB10'
WHERE empId IN ('EMP01', 'EMP02', 'EMP03')
UPDATE Employee
SET department = 'VL20'
WHERE empId IN
('EMP05',
'EMP06',
'EMP07',
'EMP08',
'EMP09');
GO
UPDATE Employee
SET department = 'AK12'
WHERE department IS NULL
--EXERCISE # 56
--Note : The answer for the this exercise will be used as subquery to the next
question )
--Get the average salary of all employees
--EXERCISE # 56.1
-- Get the average salary of employees in each department
--EXERCISE # 57
--Get the list of employees with salary less than the average employee's salary
--Note : Use a scalar subquery which is self contained to solve the problem
-- EXERCISE 57 -1
-- Get Employees earning less than or equal to the average salary of thier
corresponding departments
-- We do this in diferent ways
---- Let's use Correlated sub-query
-- First let's do this one by one
---- Q1: Get Employees working in department 'AK12' and earning less than or equal
to the departmental average salary
--- Q2: Get Employees working in department 'HN02' and earning less than or equal
to the departmental average salary
--- Q3: Get Employees working in department 'KB10' and earning less than or equal
to the departmental average salary
--- Q4: Get Employees working in department 'VL20' and earning less than or equal
to the departmental average salary
-- The question is to create one query that returns all the above together
/*
Using 'EXISTS' Predicate with subqueries
- It evaluates whether rows exist, but rather than return them, it returns
'TRUE' or 'FALSE'
- Useful technique for validating data without incurring the overhead of
retriving and counting the results
- Database engine optimize execution for query having this form
*/
--EXAMPLE
--The following code uses 'EXIST' predicate to display the list of doctors that
diagnose a patient
--EXERCISE # 69
--Modify the above code to display list of doctor/s that had never diagnosed a
patient
--EXERCISE # 70
--Write a code that display the list of medicines which are not prescribed to
patients
--EXERCISE # 71
--Write a code that display the list of medicines which are prescribed to patients
- JOIN : is a means for combining columns from one (self-join) or more tables
by using values
common to each.
- Types of JOINs
CROSS JOIN :
: Combines all rows in both tables (Creates Carticial product)
INNER JOIN ( JOIN )
: Starts with Cartecian product, and applies filter to match rows
between tables based on predicate
: MOST COMMONLY USED TO SOLVE BUSINESS PROBLEMS.
OUTER JOIN
: Starts with Cartician product, all rows from designated table
preserved,
matching rows from other table retrived, Additional NULL's
inserted as
place holders
*/
--For demonstration purpose, run the following codes that creates two tables, T1
and T2
--After inserting some data on each tables view the cross product, 'CROSS JOIN' of
the two tables
CREATE TABLE T1
( A CHAR(1),
B CHAR(1),
C CHAR(1)
);
GO
CREATE TABLE T2
( A CHAR(1),
Y CHAR(1),
Z CHAR(1)
);
GO
INSERT INTO T1
VALUES ('a','b','c'),
('d','e','f'),
('g','h','i');
GO
INSERT INTO T2
VALUES ('a','m','n'),
('X','Y','Z'),
('d','x','f');
GO
--Execute the following to get LEFT OUTER JOIN b/n T1 and T2 with condition columns
'A'
--on both tables have same value
--Execute the following to get RIGHT OUTER JOIN b/n T1 and T2 with condition
columns 'A'
--on both tables have same value, Notice the difference with LEFT OUTER JOIN
--Execute the following to get FULL OUTER JOIN b/n T1 and T2 with condition columns
'A'
--on both tables have same value, Again notice the difference with LEFT/RIGHT OUTER
JOINs
--EXERCISE # 72
--Get the CROSS JOIN of Patient and Diagnosis tables
--EXERCISE # 73
--Get the information of a patient along with its diagnosis.
--NOTE : First CROSS JOIN Patient and Diagnosis tables, and retrive only the ones
that share the same 'mrn on both tables
--EXERCISE # 74
-- Retrive MRN, Full Name, Diagnosed Date, Disease Id, Result and Doctor Id for
Patient, MRN = 'PA002'
--EXAMPLE :
--LEFT OUTER JOIN : Returns all rows form the first table, only matches from second
table.
--It assignes 'NULL' on second table that has no matching with first table
--EXERCISE :
--List employees that are not doctors by profession
--NOTE : Use LEFT OUTER JOIN as required
--EXAMPLE :
--RIGHT OUTER JOIN : Returns all rows form the second table, only matches from
first table.
--It assignes 'NULL' on second table that has no matching with second table
--The following query displays the list of doctors that are not employees to the
hospital
--Obviously all Doctors are employees, hence the result has no instance.
--EXAMPLE : The following query displays the list of doctors that had never
diagnosed
--a parient
--EXERCISE # 75
--Display the list of medicines that are prescribed by any of the doctor. (Use
RIGHT OUTER JOIN)
--EXERCISE # 76
--Display the list of medicines that which not prescribed by any of the doctors.
(Use RIGHT OUTER JOIN)
--EXERCISE # 77
--Get Patients with their diagnosis information: MRN, Full Name, Insurance Id,
Diagnosed Date, Disease Id and Doctor Id
--You can get this information from Patient and Diagnosis tables
--EXERCISE # 78
--Get Doctors who have ever diagonosed a patient(s) with the diagnosis date, mrn
--and Disease Id and result of the patient who is diagnosed
--The result should include Doctor Id, Specialization, Diagnosis Date, mrn of
--the patient, Disease Id, Result
--EXERCISE # 79
--Add the Full Name of the Doctors to the above query.
--HINT : Join Employee table with the existing table formed by joining Doctor &
Diagnosis tables on previous exercise
--EXERCISE # 80
--Add the Full Name of the Patients to the above query.
--EXERCISE # 81
--Add the Disease Name to the above query
--EXERCISE # 82
--Join tables as required and retrive PresciptionId, DiagnosisId, PrescriptionDate,
MedicineId and Dosage
--EXERCISE # 83
--Retrive PresciptionId, DiagnosisId, PrescriptionDate, MedicineId, Dosage and
Medicine Name
--EXERCISE # 84
-- Get the MRN, Full Name and Number of times each Patient is Diagnosed
--EXERCISE # 85
--Get Full Name and number of times every Doctor Diagnosed Patients
--EXERCISE # 86
--Patient diagnosis and prescribed Medicine Information
--MRN, Patient Full Name, Medicine Name, Prescibed Date and Doctor's Full Name
/*
OR
*/
1- Get Patients' information: MRN, Patient Full Name, and Diagnosed Date of
those diagnosed for disease with dId = 3
(Use filter in Where clause in addition to Joining tables Patient and
Diagnosis)
2- Get the Employee Id, Full Name and Specializations for All Doctors
3- Get Disease Ids (dId) and the number of times Patients are diagnosed for
those diseases
(Use only Diagnosis table for this)
- Can you put in the order of (highest to lowest) based on the
number of times people diagnosed for the disease?
- Can you get the top most prevalent disease?
SELECT did, count(did)
FROM Diagnosis
GROUP BY did
ORDER BY count(did)
4- Get Medicines (mId) and the number of times they are prescribed.
(Use only the MedicinePrescribed table)
- Also get the mId of medicine that is Prescribed the most
5- Can you add the name of the medicines the above query (question 4)?
(Join MedicinePrescribed and Medicine tables for this)
ALTER TABLE PharmacyPersonel ADD ppId INT PRIMARY KEY (ppId) NOT
NULL
*/
/*
SET OPERATIONS :
-UNION
-UNION ALL
-INTERSECT
-EXCEPT
*/
--EXERCISE # 85
--Correct the above code and use 'UNION' operator to get the list of all customers
in HotelCustomrs and RentalCustomer
--EXERCISE # 86
--Use UNION ALL operator instead of UNION and explain the differece on the
result/output
--EXERCISE # 87
--Get list of customers in both Hotel and Rental Customers ( INTERSECT )
--EXERCISE # 88
--Get list of customers who are Hotel Customers but not Rental ( EXCEPT )
--EXERCISE # 89
--Get list of customers who are Rental Customers but not Hotel (EXCEPT )
SYNTAX :
CREATE PROC <proc name>
[optional Parameter list]
AS
<t-sql code>
GO;
*/
--NOTE : use 'usp' as prefix for new procedures, to mean ' User created Stored
Procedure'
--EXAMPLE # 01
--Write a code that displays the list of patients and the dates they were diagnosed
--EXAMPLE # 02
--Customize the above code to creates a stored proc to gets the same result
--EXAMPLE # 03
--Execute the newly created stored procedure, using EXEC
--EXAMPLE # 04
--Modify the above procedure disply patients that was diagnosed in the year 2018
--EXAMPLE # 05
--Drop the procedure created in the above example
--EXERCISE # 01
--Create a stored procedure that returns the the average salaries for each type of
employees.
--NOTE : use 'usp' as prefix for new procedures, to mean ' user created stored
procedure'
--EXERCISE # 02
--Create a stored procedure to get list of employees earning less than the average
salary of
--all employees
--NOTE : use 'usp' as prefix for new procedures, to mean ' user created stored
procedure'
--EXERCISE # 03
--Create a procedure that returns list of Contractors that earn less than average
salary of Principals
--EXERCISE # 04 (*)
--Create a proc that returns Doctors who diagnosed Patients in a year 2017
--NOTE : (1) The result must include DocId, Full Name, Specialization, Email
Address and DiagnosisDate
--EXERCISE # 05 (*)
--Create a stored proc that returns list of patients diagnosed by a given doctor.
--EXERCISE # 06 (*)
--Create a stored procedure that returns the average salary of Employees with a
given empType
--EXERCISE # 07 (*)
--Create a stored Proc that returns the number of diagnosis each doctor made in a
--given month of the year -> pass both month and year as integer values
/*
*/
--EXAMPLE # 08
--Create a proc that is used to insert data into the Disease table
--to insert new record
--EXERCISE # 09
--Create a procedure to insert data into Doctors table,
--EXERCISE # 10
--Create a stored Proc to deletes a record from RentalCust table with a given SSN
--EXERCISE # 11
--Create the stored procedure that delete a record of a customer in HotelCust table
for a given SSN
--The procedure must display 'invalid SSN' if the given ssn is not found in the
table
--EXERCISE # 12
--Write a stored procedure to delete a record from RentalCust for a given SSN. If
the SSN is not found
--the procedure deletes the entire rows in the table.
--NOTE : First take backup for Employee table before performing this task.
--EXERCISE # 13
--Write a code that displays the list of customers with the middle two numbers of
their SS is 45
--EXERCISE # 14
--Create a Proc that Deletes record/s from RentalCustomer table, by accepting ssn
as a parameter.
--The deletion can only happen if the middle two numbers of SSN is 45
--EXERCISE # 16
--STORED PROCEDURES to update a table
--Create an stored procedure that updates the phone Number of a rental customer,
for a given customer
--Note : The procesure must take two parameters social and new phone number
--EXERCISE # 17
--Create a stored procedure that takes backup for RentalCust table into
RentalCust_Archive table
--Note : RentalCustArchive table must be first created.
--EXERCISE # 18
--Create a stored procedure that takes backup for HotelCust table into
HotelCustArchive table
--Note : Use 'EXECUTE' command to automatically create and populate
HotelCustArchive table.
--Exercise - 17
--Recreate the above stored proc such that, it shouldn't delete (purge the data)
before making
--sure the data is copied. Hint: use conditions (IF...ELSE) and TRY ... CATCH
clauses
-- A simpler version of the above Stored Proc - with no dynamic date value
appending to table name
/*
--************************************ VIEWS
*****************************************************
VIEWS
- Quite simply VIEW is saved query, or a copy of stored query, stored
on the server
- Is Virtual Table - that encapsulate SELECT queriey/es
**** - It doesn't store data persistantly, but creates a Virtual table.
- Can be used as a source for queries in much the same way as tables
themselves
: Can also be used to Join with tables
- Replaces commonly run query/ies
- Can't accept input parameters (Unlike Table Valued Functions (TVFs))
- Components
: A name
: Underlaying query
- Advantages
- Hides the complexity of queries, (large size of codding)
- Used as a mechanism to implement ROW and COLUMN level security
- Can be used to present aggregated data and hide detail data
- SYNTAX
: To create a view:
: To modify
: To drop
DROP VIEW statement
AS
<Select statement>
GO
*/
--EXAMPLE - View # 01
--Write a code that displays patient's MRN, Full Name, Address, Disease Id and
Disease Name
--EXAMPLE - View # 02
--Create simple view named vw_PatientDiagnosed using the above code.
--EXAMPLE - View # 03
--Check the result of vw_PatientDiagnosed by SELECT statement
--EXAMPLE - View # 04
--Use vw_PatientDiagnosed and retrieve only the patients that came from MD
--Note : It is possible to filter Views based on a criteria, similar with tables
--EXAMPLE - View # 05
--Modify vw_PatientDiagnosed so that it returns the patients diagnosed in year 2017
--EXAMPLE - View # 06
--Check the result of modified vw_PatientDiagnosed by SELECT statement
--EXAMPLE - View # 07
--Use sp_helptext to view the code for vw_PatientDiagnosed
--sp_helptext vw_PatientDiagnosed
--EXAMPLE - View # 08
--Drop vw_PatientDiagnosed
--EXERCISE - View # 01
--Create a view that returns Employees that live in state of Maryland, (Employee
empId, FullName, DOB)
--EXERCISE - View # 02
--Create view that displays mId, Medicine ID and the number of times each medicine
was
--prescribed.
--EXERCISE - View # 03
--Join vw_MedicinePrescribed with Medicine table and get mId, brandName,
genericName and
--number of times each medicine was prescribed
--EXERCISE - View # 04
--Create a view that displays all details of a patient along with his/her diagnosis
details
--EXERCISE - View # 05
--Use the view created for 'EXERCISE - View # 04' to get the full detail of the
doctors
-- INSERT INTO vw_Medicine VALUES (13, 'Asprine', 'No name', 2000, 'Pain
killer','2024-02-09', 0.35)
/*
NOTE : Data insertion by views on more than one tables (joined) is not supported
: Data insertion on joined tables is supported by triggers
*/
- Scalar Fuctions
- May or may not have parameter, but always return SINGLE (Scalar)
value
- Return value can be of any data type, except text, ntext, image,
cusror and timestamp.
- Example :
- System functions are scalar functions
- SYNTAX:
TO modify/alter
ALTER FUNCTION function_name
TO drop/delete
DROP FUNCTION function_name
*/
--EXAMPLE : UDF # 01
--Create a scalar valued function that returns the total number of Employees
--Note : UDF without parameter
--EXAMPLE : UDF # 02
--Create a udf that returns the average salary of a given employee type
--Note : UDF with parameter
--EXAMPLE : TVF # 01
--Create a user defined function, tvf_GetEmpTypeAndAVGSalary, that returns empType
and their
--corresponding Average Salaries. ( Creating UDF that returns a table )
--EXAMPLE - TVF # 02
--Create TVF, tvf_TopHighlyPayedEmployees, that can retrieve the full name and dob
of the
--top 'x' number of highly paied employees.
--EXERCISE : UDF # 01
--Get the list of employees that earn less than the average salary of their own
employee type.
--Note : Use udf, that takes empType and returns Salary average, within WHERE
clause to define
--the criteria.
--EXERCISE : UDF # 02
--Use the tdf, tvf_GetEmpTypeAndAVGSalary, created on 'EXAMPLE : TVF # 01' and join
with
--Employee table to get Employees that earn less than the average salaries of their
corresponding types
--EXERCISE - UDF # 03
--Disply the full name, gender, dob and AGE of all employees.
--NOTE : Create udf that returns AGE by taking DoB as an input parameter
--EXERCISE - UDF # 04
--Disply list of Doctor's id along with the number of patients each one had
diagnosed.
--(use a function to determine number of patients diagnosed )
--Method (1)
--Method (2)
--Method (1)
--Method (2)
--Using Method(1)
--EXERCISE - TVF # 01
--Create TVF that can retrive the employee id, full name and dob of the ones that
earn
--more than a given salary.
--EXERCISE - TVF # 02
--Create TVF that can retrive the number of times each medicine was prescribed.
--EXERCISE - TVF # 03
--Create TVF that can return a list of employees with a given gender.
--EXERCISE - TVF # 04
--Create TVF that can return full name of patients that are diagnosed with a given
disease.
--Note : The diseased is identified by name only.
*/
--EXAMPLE - CTE # 01
--Create a CTE that returns medicines and number of times they are prescribed (mId,
NumberOfPrescriptions)
--Then join the created CTE with Medicine table to get the name and number of
prescription of the medecines
--EXAMPLE - CTE # 02
--Create CTE that returns the average salaries of each type of employees
--EXERCISE - CTE # 01
--Modify the above code to sort the output by empType in descending order.
--EXERCISE - CTE # 02
--Create CTE to display PrescriptionId, DiagnossisNo, Prescription Date for each
patient. Then use
--the created CTE to retrive the dossage and number of allowed refills.
/********************************* TRIGGERS
***********************************************
TRIGGERS -
- Are a special type of stored procedures that are fired/executed
automatically in response
to a triggering action
- Helps to get/capture audit information
- Like stored procedures, views or functions, triggers encapsulate code.
- Triggers get fired (and run) by themselves only when the event for which
they are
created for occurs, i.e. We do not call and run triggers, they get
fired/run
by by their own
Types of triggers
- DML triggers
: Fires in response to DML events (Insert/Delete/Update)
- DDL triggers
: Fires in response to DDL events (Create/Alter/Drop)
- login triggers
: Fires in response to login events
DML Triggers
There are two types of DML Triggers: AFTER/FOR and INSTEAD OF, and
there are
three DML events (INSERT, DELETE and UPDATE) under each type -
FOR/AFTER INSERT
DELETE
UPDATE
INSTEAD OF INSERT
DELETE
UPDATE
Syntax:
Whenever a trigger is created, it must be created for a given table,
and for a
specific event,
*/
--Drop/Delete a trigger
/*
The use of 'inserted' table and 'deleted' table by triggers.
- DML trigger statements use two special tables: the deleted table and the
inserted tables.
- SQL Server automatically creates and manages these two tables.
- They are temporary, that only lasts within the life of trigger
- These two tables can be used to test the effects of certain data
modifications
and to set conditions for DML trigger actions.
- In DML triggers, the inserted and deleted tables are primarily used to
perform the following:
- Extend referential integrity between tables.
- Insert or update data in base tables underlying a view.
- Test for errors and take action based on the error.
- Find the difference between the state of a table before and after a
data
modification and take actions based on that difference.
**** - The deleted table stores copies of the affected rows during DELETE and
UPDATE statements.
*** - The inserted table stores copies of the affected rows during INSERT and
UPDATE statements.
*/
--Now create a trigger that automagically inserts data into the MedicineInsertAudit
table,
--whenever data inserted into Medicine table
--Test how the trigger works by inserting one row data to Medicine table
--Assume both the fist name and DoB changed during update, The 'Message' column
should capture
--a customized statement as follows
-- 'First Name changed from 'Abebe' to 'Kebede', DoB changed from 'old DoB' to
'new DoB'
--Similarly the 'Message' column should be able to capture all the changes that
took place.
--Steps to follow,
--Step (1): Drop the Foreign Key Constraints from the Doctor, PharmacyPersonel,
Diagnosis, and MedicineDispense tables
-- and redefine FK constraints for all child tables
--Step (2): Create the Employee_Terminated_Archive table
--Step (3): Create the trigger that inserts the deleted employee data into
Employee_Terminated_Archive
--Step (1): Drop the Foreign Key Constraints from the Doctor, PharmacyPersonel,
Diagnosis, and MedicineDispense tables
-- and redefine FK constraints for all child tables
--Step3: Create the trigger that inserts the deleted employee data into
Employee_Terminated_Archive table
--by checking the role of the employee
--Now test the trigger at work by deleting an Employee - employee data will be
archived, but
--the employee role wont be set as expected
--Step3: Create the trigger that inserts the deleted employee data into
Employee_Terminated_Archive table by checking the role of the employee
--Now test the trigger at work by deleting an Employee - employee data will be
archived, but
--the employee role wont be set as expected
-- There are times where we want to insert data into tables through views,
-- Assume that we hired a doctor (an employee) having all the employee and doctor
information;
-- for this we want to insert data into both tables through the view
vw_DoctorEmployee
-- But this will not go as intended, because of multiple base tables.... see below
--Now create an instead of insert trigger on the view vw_DoctorEmployee -> that
takes the data and inserts into
--the tables individually