Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

ASG SQL Test-60 Minutes

Test Preparation
1. You will need a 1 hour time slot to take the test and a database environment
2. Any Relational Database Allowed
3. All answers must be grammatically correct and runnable.
TEST

SQL Problem 1 (10 min)


Assume we have loaded a flat file with patient diagnosis data into a table called “Data”. The table structure is:

Create table Data (


Firstname varchar(50),
Lastname varchar(50),
Date_of_birth datetime,
Medical_record_number varchar(20),
Diagnosis_date datetime,
Diagnosis_code varchar(20))

The data in the flat file looks like this:

'jane','jones','2/2/2001','MRN-11111','3/3/2009','diabetes'
'jane','jones','2/2/2001','MRN-11111','1/3/2009','asthma'
'jane','jones','5/5/1975','MRN-88888','2/17/2009','flu'
'tom','smith','4/12/2002','MRN-22222','3/3/2009','diabetes'
'tom','smith','4/12/2002','MRN-33333','1/3/2009','asthma'
'tom','smith','4/12/2002','MRN-33333','2/7/2009','asthma'
'jack','thomas','8/10/1991','MRN-44444','3/7/2009','asthma'

You can assume that no two patients have the same firstname, lastname, and date of birth combination. However
one patient might have several visits on different days. These should all have the same medical record number.
The problem is this: Tom Smith has 2 different medical record numbers. Write a query that would always show all
the patients
who are like Tom Smith – patients with more than one medical record number.
This problem has many solutions, but if you know SQL, you should be able to find one that uses a single query with
no subqueries.

SELECT a.firstname,
a.lastname,
a.date_of_birth,
a.medical_record_number
FROM data a, data b
WHERE a.firstname = b.firstname
AND a.lastname = b.lastname
AND a.date_of_birth = b.date_of_birth
AND a.medical_record_number <> .medical_record_number
GROUP BY a.firstname,
a.lastname,
a.date_of_birth,
a.medical_record_number
SQL Problem 2 (20 min)
Given the following tables and data:

create table months (monthIndex int);


create table invoice(invoiceId int, month int)
create table lineitems(lid int, invoiceID int, charged_amount decimal(6,2), contracted_rate decimal(6,2))

Insert into months values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);

Insert into invoice values


                (1,1),
         (2,1),
                (3,2),
                (4,2),
                (5,4),
                (6,5),
                (7,5),
                (8,8),
                (9,8),
                (10,3),
                (11,3),
                (12,6),
                (13,7),
                (14,10),
                (15,11),
                (16,11);

Insert into lineitems values


                (1,1,1000.10,1000.10),
                (2,1,1500.20,1200.00),
                (3,1,1300.10,1300.10),
                (4,2,2100.30,2100.30),
                (5,2,1100.10,1000.10),
                (6,2,1100.40,1100.40),
                (7,3,6000.10,6000.40),
                (8,3,1400.10,1400.40),
                (9,4,4500.10,4500.10),
                (10,5,3300.10,3000.18),
                (11,5,2900.10,2900.10),
                (12,6,8900.10,8900.10),
                (13,6,2200.10,2200.10),
                (14,8,3700.10,3700.50),
                (15,9,7000.10,7000.60),
                (16,10,2200.10,2200.10),
                (17,10,2200.10,2200.10),
                (18,11,2200.10,2200.10),
                (19,12,2200.10,2200.10),
                (20,13,2200.10,2200.10),
                (21,13,2200.10,2200.10),
                (22,14,1100.10,1000.10),
                (23,14,1100.40,1100.40),
                (24,14,6000.10,6000.40),
                (25,15,1400.10,1400.40),
                (26,16,4500.10,4500.10),
                (27,16,3300.10,3000.18);

Develop a single SQL Query that computes the total invoices amount for each month, and the percentage of
change from the previous month.  If there are no invoices for the month, the computed value should be null. 

Here is the result that the query should generate:

select m.monthindex,sum(charged_amount) totalchargedamount, trunc((sum(charged_amount)-


LAG(sum(charged_amount)) OVER (

ORDER BY m.monthindex))/sum(charged_amount),4)*100 changepercentage from months m,


invoice i,lineitems l

where l.invoiceid(+)=i.invoiceid

and i.month(+)=m.monthindex

group by m.monthindex

order by 1
SQL Problem 3 (30 min)

A banking client provides a monthly snapshot of banking data (tens of millions of accounts)
The data is sent with one row per account each month. This is stored in table called “ACCOUNTS”
Fields on the record include

Column Description Format/Example


*YRMTH Fiscal year and month of snapshot 6 digit numeric, 4 digit year
appended to 2 digit month, example
201401, 201411
*AccountID Unique identifier for an account. Integer, Example 10090207032
Starting Balance Starting balance of account on close $25,790
of previous month
Closing Balance Closing balance of account on close $29,320
of month
Investment Class ..... ...
..... ...... .....

There are many more columns, but the two starred (*) columns are the only ones you will need for the query.
There are no columns to tell when an account is closed, or just opened, and no separate “Account Master” – you
must use this table alone for your resultant query.
The table has been in place starting 200301, so many accounts have dozens of rows, if they have been open for
many years, and others just 1 or 2 rows, if new.
Write a query to give just a list of accountIDs that meet the following criteria.

1) Has a record for the specific month 201403


2) Also has a record for the specific month 201502

3) Is missing one or more month records between these two.

A good account that has been open from 201403 to 201502 inclusive would have rows for
201403
201404
201405
201406
201407
201408
201409
201410
201411
201412
201501
201502
It is fine to hardcode the start month (201403), end month (201502) and the actual count of the months between
these (10 exclusive or 12 inclusive) as part of your query.
Again, the accounts must meet all 3 criteria to be a problem account. If they only have a partial set of these
records but don’t have the start month or don’t have the end month, it is not an issue, only when they have both
the specified start and end and not a full set between.

Select accountid from account where ymtd =201403 and ymtd= 201502
and
ymtd = ANY (select to_char(add_months(date '2014-03-01', level - 1), 'yyyymm') mth
from dual
connect by level <= 10)

You might also like