473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need help writing T-SQL statements?

I have the following database model:

Employee -Table
PK EmpSSN varchar (13)
FirstNm varchar (40)
LastNm varchar (40)

Dependent - Table
PK DepSSN varchar (13)
FirstNm varchar (40)
LastNm varchar (40)
EmpSSN varchar (13)

BenefitDetail - Table
PK BenefitID int
PK SSN varchar (13)
PK EffectiveDate datetime
Termination Date datetime

Benefit - Table
PK BenefitID int
BenefitName varchar(25)

The Employee table describes the employee eligible for health care benefits. The Dependent table has a foreign key relationship with the Employee. The Benefit table has an identity primary key id which assigns a unique value. The BenefitName column will be populated with values like MEDICAL, DENTAL, and VISION. The BenefitDetail table lists the benefits a person has whether they are an employee or dependent, the effective date of the benefit, and the termination date. The termination date is not a required field (the field will be null if the benefit is not terminated).

1. Write a T-SQL statement that will return benefits that are not terminated.

2. Write a T-SQL statement that will return all of the employees who have terminated benefits, have no active benefits, and have no dependents. List the employee social security number, last name, and firstname.

3. Write a T-SQL statement that will list in order all of employees followed by their dependents. List the employee social security number, dependent social security number(if the person is a dependent), last name, and firstname.

Example:
EmpSSN DepSSN Lastname Firstname
123-45-6789 Doe John
123-45-6789 567-34-3432 Doe John
987-43-9746 Smith Rob
987-43-9746 345-34-3434 Smith Jane

I have been away from technology for awhile. I am trying to re-enter the industry. I have attempted to write the queries. I would appreciate any help you could give me.

Thanks,
Leah Phillips
Feb 5 '07 #1
2 1777
iburyak
1,017 Expert 512MB
1. Write a T-SQL statement that will return benefits that are not terminated.
[PHP]
SELECT SSN, b.BenefitName
FROM BenefitDetail bd
JOIN Benefit b on bd.BenefitID = b.BenefitID
WHERE bd.[Termination Date] is null[/PHP]

2. Write a T-SQL statement that will return all of the employees who have terminated benefits,
have no active benefits, and have no dependents.
List the employee social security number, last name, and firstname.

[PHP]
SELECT e.EmpSSN, e.LastNm, e.FirstNm
FROM Employee e
JOIN BenefitDetail bd on e.EmpSSN = bd.SSN
WHERE bd.[Termination Date] is null -- terminated benefits
and e.EmpSSN not in (SELECT EmpSSN from Dependent) -- have no dependents[/PHP]

3. Write a T-SQL statement that will list in order all of employees followed by
their dependents. List the employee social security number, dependent social
security number(if the person is a dependent), last name, and firstname.

Example:
EmpSSN DepSSN Lastname Firstname
123-45-6789 Doe John
123-45-6789 567-34-3432 Doe John
987-43-9746 Smith Rob
987-43-9746 345-34-3434 Smith Jane



[PHP]--GET EMPLOYEES
SELECT EmpSSN, '' DepSSN, LastNm, FirstNm
FROM Employee
UNION
--GET DEPENDENTS
SELECT d.EmpSSN, d.DepSSN, d.LastNm, d.FirstNm
FROM Employee e
JOIN Dependent d on e.EmpSSN = d.EmpSSN
ORDER BY 1,2

--ORDER BY will always put employee first because he has dependent ''[/PHP]

Good Luck
Feb 5 '07 #2
Iburyak,

Thank you for your help. I really appreciate it.

Ldphill
Feb 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: michaaal | last post by:
When writing an ASP app, it has been my experience that usually I write code that is compatible with IE6 but not previous versions of Internet Explorer. Does anyone have any practical advice for...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
2
by: Lau Lei Cheong | last post by:
Hello, I'm writing UserControls as stated earlier. Now I come to the problem that I need to preserve viewstate in my controls. If I want to save the informations on the controls, in which...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
1
by: Rizwan | last post by:
I am writing this code for file upload in asp 3.0 on my webpage........ It is working on local system but when i want to upload an image on my website from client machine it is not working ..........
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.