Question

Database For this lab you will be using SQL SELECT statements to query your database tables....

Database

For this lab you will be using SQL SELECT statements to query your database tables. You will be turning in the results of the following queries:

1. List all Patients and what Bed they are assigned to

2. List all patients who had Treatments and what Treatment they received

3. List all patients who had tests and what Test they had

4. List the employees (doctors, nurses, etc.) who assisted each patient.

5. List all patients in alphabetical order

6. List all patients who live in Atlanta and had a test completed

7. List all patients who live in either Woodstock or Roswell who had a treatment completed.

If you don't have data in your created tables that will allow you to create these queries - add appropriate data to your tables before doing the queries.

Capture your results from each SQL query (SELECT statement) - that also shows the actual written query - and turn them in.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have created Patients,Employees,Services,Treatments,Tests,

PatientCareTakers,PatientTreatments,PatientServices,PatientTests

tables to hold data. Find creations scripts below.

I have added example records in All tables.

I have used foreign key relationships wherever needed and Inner Joins to fetch data

from tables to get intercepted records.

In the last there are 7 Select queries to get results as per the question.

You have add example records in tables like Patients,Employee,atientCareTakers,PatientTreatments,PatientServices,PatientTests

to get data in result queries otherwise it will give blank result.

Table Creation Scripts :

Create Schema ECH

GO

Create Table [ECH].[Services](

[ServiceId] int Identity(1,1) Primary Key,

[Title] varchar(64) NOT NULL

)

INSERT INTO [ECH].[Services] Values ('Laboratory tests')

INSERT INTO [ECH].[Services] Values ('Radiology procedures')

INSERT INTO [ECH].[Services] Values ('Admissions')

INSERT INTO [ECH].[Services] Values ('In-patient and out-patient surgeries')

INSERT INTO [ECH].[Services] Values ('Labor and delivery services')

INSERT INTO [ECH].[Services] Values ('Intensive care')

INSERT INTO [ECH].[Services] Values ('Cardiology')

INSERT INTO [ECH].[Services] Values ('Open-heart surgery')

INSERT INTO [ECH].[Services] Values ('Neurology department')

INSERT INTO [ECH].[Services] Values ('Pediatric medical and surgical care')

INSERT INTO [ECH].[Services] Values ('Orthopedics')

INSERT INTO [ECH].[Services] Values ('Oncology')

INSERT INTO [ECH].[Services] Values ('Emergency')

Create Table [ECH].[Treatments](

[TreatmentId] int Identity(1,1) Primary Key,

[Title] varchar(64) NOT NULL

)

INSERT INTO [ECH].[Treatments] Values ('Treatment 1')

INSERT INTO [ECH].[Treatments] Values ('Treatment 2')

INSERT INTO [ECH].[Treatments] Values ('Treatment 3')

INSERT INTO [ECH].[Treatments] Values ('Treatment 4')

INSERT INTO [ECH].[Treatments] Values ('Treatment 5')

INSERT INTO [ECH].[Treatments] Values ('Treatment 6')

INSERT INTO [ECH].[Treatments] Values ('Treatment 7')

INSERT INTO [ECH].[Treatments] Values ('Treatment 8')

INSERT INTO [ECH].[Treatments] Values ('Treatment 9')

Create Table [ECH].[Tests] (

[TestId] int Identity(1,1) Primary Key,

[Title] varchar(64) NOT NULL

)

INSERT INTO [ECH].[Tests] Values ('Test 1')

INSERT INTO [ECH].[Tests] Values ('Test 2')

INSERT INTO [ECH].[Tests] Values ('Test 3')

INSERT INTO [ECH].[Tests] Values ('Test 4')

INSERT INTO [ECH].[Tests] Values ('Test 5')

INSERT INTO [ECH].[Tests] Values ('Test 6')

INSERT INTO [ECH].[Tests] Values ('Test 7')

INSERT INTO [ECH].[Tests] Values ('Test 8')

INSERT INTO [ECH].[Tests] Values ('Test 9')

Create Table [ECH].[EmployeeType](

[TypeId] int Identity(1,1) Primary Key,

[Title] varchar(64) NOT NULL

)

INSERT INTO [ECH].[EmployeeType] Values ('Doctor')

INSERT INTO [ECH].[EmployeeType] Values ('Registered nurse')

INSERT INTO [ECH].[EmployeeType] Values ('Laboratory technician')

INSERT INTO [ECH].[EmployeeType] Values ('Counselor')

INSERT INTO [ECH].[EmployeeType] Values ('Rehabilitation therapist')

INSERT INTO [ECH].[EmployeeType] Values ('Social services staff')

INSERT INTO [ECH].[EmployeeType] Values ('Admittance/discharge staff')

INSERT INTO [ECH].[EmployeeType] Values ('Insurance staff')

INSERT INTO [ECH].[EmployeeType] Values ('Janitorial staff,')

INSERT INTO [ECH].[EmployeeType] Values ('Security staff')

Create Table [ECH].[Employees] (

[EmpId] int Identity(1,1) Primary Key,

[EmpType] int NOT NULL,

[Name] varchar(64) NOT NULL

)

-- FOREIGN KEY reference to [TypeId] in [EmployeeType] table

ALTER TABLE [ECH].[Employees] WITH CHECK ADD CONSTRAINT [FK_Employees_EmployeeType] FOREIGN KEY([EmpType])

REFERENCES [ECH].[EmployeeType] ([TypeId])

ALTER TABLE [ECH].[Employees] CHECK CONSTRAINT [FK_Employees_EmployeeType]

Insert Into [ECH].[Employees] (EmpType, Name) Values (1,'Emp 11')

Insert Into [ECH].[Employees] (EmpType, Name) Values (2,'Emp 21')

Insert Into [ECH].[Employees] (EmpType, Name) Values (3,'Emp 31')

Insert Into [ECH].[Employees] (EmpType, Name) Values (4,'Emp 41')

Insert Into [ECH].[Employees] (EmpType, Name) Values (5,'Emp 51')

Insert Into [ECH].[Employees] (EmpType, Name) Values (6,'Emp 61')

Insert Into [ECH].[Employees] (EmpType, Name) Values (7,'Emp 71')

Insert Into [ECH].[Employees] (EmpType, Name) Values (8,'Emp 81')

Insert Into [ECH].[Employees] (EmpType, Name) Values (9,'Emp 91')

Insert Into [ECH].[Employees] (EmpType, Name) Values (1,'Emp 12')

Insert Into [ECH].[Employees] (EmpType, Name) Values (2,'Emp 22')

Insert Into [ECH].[Employees] (EmpType, Name) Values (3,'Emp 32')

Insert Into [ECH].[Employees] (EmpType, Name) Values (4,'Emp 42')

Insert Into [ECH].[Employees] (EmpType, Name) Values (5,'Emp 52')

Insert Into [ECH].[Employees] (EmpType, Name) Values (6,'Emp 62')

Insert Into [ECH].[Employees] (EmpType, Name) Values (7,'Emp 72')

Insert Into [ECH].[Employees] (EmpType, Name) Values (8,'Emp 82')

Insert Into [ECH].[Employees] (EmpType, Name) Values (9,'Emp 92')

Insert Into [ECH].[Employees] (EmpType, Name) Values (1,'Emp 13')

Insert Into [ECH].[Employees] (EmpType, Name) Values (2,'Emp 23')

Insert Into [ECH].[Employees] (EmpType, Name) Values (3,'Emp 33')

Insert Into [ECH].[Employees] (EmpType, Name) Values (4,'Emp 43')

Insert Into [ECH].[Employees] (EmpType, Name) Values (5,'Emp 53')

Insert Into [ECH].[Employees] (EmpType, Name) Values (6,'Emp 63')

Insert Into [ECH].[Employees] (EmpType, Name) Values (7,'Emp 73')

Insert Into [ECH].[Employees] (EmpType, Name) Values (8,'Emp 83')

Insert Into [ECH].[Employees] (EmpType, Name) Values (9,'Emp 93')

Create Table [ECH].[Patients](

[PatientId] int Identity(1,1) Primary Key,

[Name] varchar(64) NOT NULL,

[City] varchar(64) NOT NULL,

[BedNumber] int NULL

)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 1','Woodstock', 1)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 2','Roswell', 2)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 3','Woodstock', 3)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 4','Atlanta', 4)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 5','Woodstock', 5)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 6','Atlanta', 6)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 7','Woodstock', 7)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 8','Roswell', 8)

Insert Into [ECH].[Patients] (Name, City, BedNumber) Values ('Patient 9','Atlanta', 9)

Create Table [ECH].[PatientCareTakers]( [PatientId] int NOT NULL,

[EmpId] int NOT NULL,

[StartTime] datetime NOT NULL,

[EndTime] datetime NULL)

-- FOREIGN KEY reference to Patient Id in Patient table

ALTER TABLE [ECH].[PatientCareTakers] WITH CHECK ADD CONSTRAINT [FK_PatientCareTakers_Patients] FOREIGN KEY([PatientId])

REFERENCES [ECH].[Patients] ([PatientId])

ALTER TABLE [ECH].[PatientCareTakers] CHECK CONSTRAINT [FK_PatientCareTakers_Patients]

-- FOREIGN KEY reference to [EmpId] in [Employees] table

ALTER TABLE [ECH].[PatientCareTakers] WITH CHECK ADD CONSTRAINT [FK_PatientCareTakers_Employees] FOREIGN KEY([EmpId])

REFERENCES [ECH].[Employees] ([EmpId])

ALTER TABLE [ECH].[PatientCareTakers] CHECK CONSTRAINT [FK_PatientCareTakers_Employees]

Insert Into [ECH].[PatientCareTakers] (PatientId, EmpId, StartTime, EndTime)

Select p.PatientId, e.EmpId, GETDATE() , case WHEN p.PatientId % 2 = 0 Then GETDATE() else NULL end

From [ECH].[Patients] p INNER JOIN [ECH].[Employees] e

on p.PatientId = e.EmpType

Create Table [ECH].[PatientTreatments]( [PatientId] int NOT NULL,

[TreatmentId] int NOT NULL,

[StartTime] datetime NOT NULL,

[EndTime] datetime NULL)

-- FOREIGN KEY reference to Patient Id in Patient table

ALTER TABLE [ECH].[PatientTreatments] WITH CHECK ADD CONSTRAINT [FK_PatientTreatments_Patients] FOREIGN KEY([PatientId])

REFERENCES [ECH].[Patients] ([PatientId])

ALTER TABLE [ECH].[PatientTreatments] CHECK CONSTRAINT [FK_PatientTreatments_Patients]

-- FOREIGN KEY reference to [TreatmentId] in [PatientTreatments] table

ALTER TABLE [ECH].[PatientTreatments] WITH CHECK ADD CONSTRAINT [FK_PatientTreatments_Treatments] FOREIGN KEY([TreatmentId])

REFERENCES [ECH].[Treatments] ([TreatmentId])

ALTER TABLE [ECH].[PatientTreatments] CHECK CONSTRAINT [FK_PatientTreatments_Treatments]

Insert Into [ECH].[PatientTreatments] (PatientId,TreatmentId,StartTime)

Select p.PatientId, t.TreatmentId, GETDATE()

From [ECH].[Patients] p INNER JOIN [ECH].[Treatments] t

on p.PatientId = t.TreatmentId

Create Table [ECH].[PatientServices]( [PatientId] int NOT NULL,

[ServiceId] int NOT NULL,

[StartTime] datetime NOT NULL,

[EndTime] datetime NULL)

-- FOREIGN KEY reference to Patient Id in Patient table

ALTER TABLE [ECH].[PatientServices] WITH CHECK ADD CONSTRAINT [FK_PatientServices_Patients] FOREIGN KEY([PatientId])

REFERENCES [ECH].[Patients] ([PatientId])

ALTER TABLE [ECH].[PatientServices] CHECK CONSTRAINT [FK_PatientServices_Patients]

-- FOREIGN KEY reference to [ServiceId] in [PatientServices] table

ALTER TABLE [ECH].[PatientServices] WITH CHECK ADD CONSTRAINT [FK_PatientServices_Services] FOREIGN KEY([ServiceId])

REFERENCES [ECH].[Services] ([ServiceId])

ALTER TABLE [ECH].[PatientServices] CHECK CONSTRAINT [FK_PatientServices_Services]

Insert Into [ECH].[PatientServices] (PatientId,ServiceId,StartTime)

Select p.PatientId, s.ServiceId, GETDATE()

From [ECH].[Patients] p INNER JOIN [ECH].[Services] s

on p.PatientId = s.ServiceId

Create Table [ECH].[PatientTests]( [PatientId] int NOT NULL,

[TestId] int NOT NULL,

[StartTime] datetime NOT NULL,

[EndTime] datetime NULL)

-- FOREIGN KEY reference to Patient Id in Patient table

ALTER TABLE [ECH].[PatientTests] WITH CHECK ADD CONSTRAINT [FK_PatientTests_Patients] FOREIGN KEY([PatientId])

REFERENCES [ECH].[Patients] ([PatientId])

ALTER TABLE [ECH].[PatientTests] CHECK CONSTRAINT [FK_PatientTests_Patients]

-- FOREIGN KEY reference to [ServiceId] in [PatientServices] table

ALTER TABLE [ECH].[PatientTests] WITH CHECK ADD CONSTRAINT [FK_PatientTests_Tests] FOREIGN KEY([TestId])

REFERENCES [ECH].[Tests] ([TestId])

ALTER TABLE [ECH].[PatientTests] CHECK CONSTRAINT [FK_PatientTests_Tests]

Insert Into [ECH].[PatientTests] (PatientId,TestId,StartTime)

Select p.PatientId, t.TestId, GETDATE()

From [ECH].[Patients] p INNER JOIN [ECH].[Tests] t

on p.PatientId = t.TestId

Select Queries to get final results :

-- 1

Select Name, BedNumber From [ECH].Patients

SOLQuery2.sql DESKTOP-0HBMUHNAIDBSERVER.tempdb (DESKTOP-0H8MUHNSunny (53) Microsoft SOL Server Management Studio Eile Edit View Query Project Debug Iools Window Help SQLQuery2.sql - D.. 8MUH Sunny (53)) XSO SOLQuery1.sql-D...8MUH Sunny (52)) Select Name, BedNumber From [ECH] . Patients 100 % Resuts EMessoges Name 1Patient 11 2 Patient 2 2 3 Patient 33 4 Patient 4 4 5 Patient 5 5 6 Patient 6 6 7 Patient 7 7 8 Patient 8 9 Patient 9 9 BedNumber Query executed successfully. DESKTOP DESKTOP-0H8MUHASunnytempdb 00:00:00 9 rows Ready Ln 8 Col 1 Ch 1 INS 1:08 PM A M ) 7/14/2018

-- 2

Select p.Name, t.Title 'Treatment Name', pt.StartTime, pt.EndTime

From [ECH].[Patients] p INNER JOIN [ECH].[PatientTreatments] pt

On p.PatientId = pt.PatientId

INNER JOIN [ECH].Treatments t

On pt.[TreatmentId] = t.TreatmentId

-- 3

Select p.Name, t.Title 'Test Name', pt.StartTime, pt.EndTime

From [ECH].[Patients] p INNER JOIN [ECH].[PatientTests] pt

On p.PatientId = pt.PatientId

INNER JOIN [ECH].Tests t

On pt.TestId = t.TestId

-- 4

Select p.Name 'Patient', e.Name 'Care Taker', et.Title 'Designation'

From [ECH].[Patients] p INNER JOIN [ECH].[PatientCareTakers] ct

On p.PatientId = ct.[PatientId]

INNER JOIN [ECH].Employees e

ON ct.EmpId = e.EmpId

INNER JOIN [ECH].EmployeeType et

On et.TypeId = ct.[EmpId]

-- 5

Select Name From [ECH].Patients Order By Name

-- 6

Select Name

From [ECH].Patients p INNER JOIN [ECH].[PatientTests] t

ON p.PatientId = t.PatientId

Where p.City = 'Atlanta'

AND t.EndTime IS NOT NULL

-- 7

Select Name

From [ECH].Patients p INNER JOIN [ECH].[PatientTreatments] t

ON p.PatientId = t.PatientId

Where (p.City = 'Woodstock' OR p.City = 'Roswell')

AND t.EndTime IS NOT NULL

Add a comment
Know the answer?
Add Answer to:
Database For this lab you will be using SQL SELECT statements to query your database tables....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am using Oracle SQL and am new to it. I have seven tables, one of...

    I am using Oracle SQL and am new to it. I have seven tables, one of them is a subtable of two of the others. I need to do the following queries: 1. List all Patients and what Bed they are assigned to 2. List all patients who had Treatments and what Treatment they received 3. List all patients who had tests and what Test they had 4. List the employees (doctors, nurses, etc.) who assisted each patient. 5. List...

  • Create the database and tables for the database. Show all SQL statements. Include primary and foreign...

    Create the database and tables for the database. Show all SQL statements. Include primary and foreign keys. Insert data into each table. Show select statements and display the output of each table. Note:Student’s name must be inserted into table as part of the data! Perform the SQL below: Query one table and use WHERE to filter the results. The SELECT clause should have a column list, not an asterisk (*). State the purpose of the query; show the query and...

  • Subject : computer science -web development Qus: using the database tables below write the SQL queries:...

    Subject : computer science -web development Qus: using the database tables below write the SQL queries: ( SELECT; FORM; WHERE; ORDER BY; DISTINCT; GROUP BY; COUNT(); SUM(); AVG(); MAX(); MIN(); JOIN; ON) Student (StudentId, Major, GPA) Transcript( Studentid, CourseNo, Gread); Course( CourseNo, Title, Credits) Display the title of the course(s) that are 3 credits? Display the average GPA of the students Majoring in ‘CSC’ List non duplicated major(s) of students who have received a grades of ‘A’ in CSC350 List...

  • Please help me to solve Please, No handwriting COURSE; introduction to database Q- write a query ...

    Please help me to solve Please, No handwriting COURSE; introduction to database Q- write a query SQL by Using the info below A. Normalize the Tables (in 3NF at least) B. Create the Normalized Tables and Populate them with at least 5 Rows C. Write the Wholesale Management System requested Queries & Execute them VERY IMPORTANT Screenshots from MySQL (or any other software you use) of all the tables after queries result. - Database system for a Wholesale Management System...

  • Prepare and execute each of the queries listed using the "Adventureworks2012" database in SQL: Server: http://msftdbprodsamples.codeplex.com/releases/view/93587...

    Prepare and execute each of the queries listed using the "Adventureworks2012" database in SQL: Server: http://msftdbprodsamples.codeplex.com/releases/view/93587 When all of your queries are complete, cut and paste the SQL Syntax into a word document. In order to see what the column names are, you need to click on the table and then Columns to see the field names. Make sure to include column headings that make sense in the queries (use the as “Field Name” after the field selected). Multi-table Queries...

  • Please access to TUN SQL/Assistant using the default database = db_pvfc12_std 1.- Run a query to...

    Please access to TUN SQL/Assistant using the default database = db_pvfc12_std 1.- Run a query to retrieve all the data field from db_pvfc12_std in table CUSTOMER_T 2.- Run a query to retrieve record --> CustomerID = 11, How many records did you get and why? 3.- Run a query to retrieve all the data field from table Order_T               Once retrieved:                              3.1.- Please name the fields showing: Primary Key, Foreign Key and other fields                              3.2.- What type of...

  • Return the acnum of academic(s) who wrote the largest number of papers. You must NOT use MAX. An SQL query that lists all academics in decreasing order of their total number of papers is incorrect....

    Return the acnum of academic(s) who wrote the largest number of papers. You must NOT use MAX. An SQL query that lists all academics in decreasing order of their total number of papers is incorrect. Question 1. SQL (10 points) In addition to the lecture notes, you should also study by yourself the SQL Plus tutorial on Canvas (the Oracle section) and other resources for syntax and useful functions The relational schema for the Academics database is as follows descrip,...

  • Topic: Inventory Manangement Part A. Database Creation Your database should have a minimum of 4 tables...

    Topic: Inventory Manangement Part A. Database Creation Your database should have a minimum of 4 tables and include both one-to-many and many-to-many relationships. Be sure to include some numeric and/or date fields. Define all appropriate constraints, using the proper naming conventions (see Structure Notes below). Populate your database with at least 30 records in the main table(s), and whatever is needed in related tables. Submit the following: • a short description of the purpose of the database and what the...

  • Tables: Create table Item(                 ItemId                 char(5) constraint itmid_unique primar

    Tables: Create table Item(                 ItemId                 char(5) constraint itmid_unique primary key,                 Decription           varchar2(30),                 Unitcost               number(7,2)); Create table Customer(                 custID                   char(5) constraint cid.unique primary key,                 custName          varchar2(20),                 address                                varchar2(50)); Create table Orderdata( orderID                char(5) constraint oid_uniq primary key,                 orderdate           date,                 shipdate              date,                 ItemId                  char(5) references Item.ItemId,                 No_of_items     number(4),                 Unitcost               number(7,2),                 Order_total        number(7,2),                 custID                   char(5) references customer.custID); Insert Into Item values(‘A123’,’Pencil’,2.5); Insert Into Item values(‘B123’,’Pen’,15); Insert Into...

  • SQL Queries – in this assignment you will be asked to create several SQL queries relating...

    SQL Queries – in this assignment you will be asked to create several SQL queries relating to the Sakila database that we installed in class. You may freely use DBeaver to create these queries, but I will expect your solution to show me the SQL code to answer or complete the tasks below. Write a query that produces the last name, first name, address, district, and phone number for every customer in the customer table. (You don’t need to include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT