0% found this document useful (0 votes)
48 views7 pages

Nikhil SQL Assigment 4

The document provides details for an MS SQL Server assignment involving 10 questions. The questions cover topics like writing queries to display result sets, creating functions to calculate ages and split strings, generating random numbers, inserting data into temporary tables, and more. Sample answers are provided for each question to demonstrate how to complete the tasks in SQL.

Uploaded by

Nikhil Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views7 pages

Nikhil SQL Assigment 4

The document provides details for an MS SQL Server assignment involving 10 questions. The questions cover topics like writing queries to display result sets, creating functions to calculate ages and split strings, generating random numbers, inserting data into temporary tables, and more. Sample answers are provided for each question to demonstrate how to complete the tasks in SQL.

Uploaded by

Nikhil Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MS SQL Server

Assignment#4

Topic: Convert,Cast,Mathematical,functions,Temporary tablesTime: 2 Hr

SQLAssignment # 4–Questions

1. Write a query to Display the below result set from PROJECTS table.
PROJECT_ID DESCR DESCR-PROJECT_ID
401 Inventory Inventory - 401
402 Accounting Accounting - 402
403 Payroll Payroll - 403
404 Contact Mgmt Contact Mgmt - 404

2. Write a query to Display the below result set from PROJECTS table.
PROJECT_ID DESCR START_DATE
401 Inventory 04-01-2011
402 Accounting 08-01-2011
403 Payroll 10-01-2011
404 Contact Mgmt 11-01-2011

3. Write a query prints 25 random numbers between 100 and 200.

4. Create a function which calculates and returns the age of a person in year.

5. Create a function which calculates and returns the age of a person in Month.

6. Create a Functionto Display client name whose project’s ‘Coding’ task and
status ‘In Progress’.

7. Create a Functionto Display names of employees doing ‘System Analysis’ along with
project name.

8. Create aSplitFunctionto pass Comma separated (Delimited) string& Display string as a table
column value.

9. Create a stored procedure to insert employees table data into Temporary Table.

10. To insert department number, names and salaries of employees who are
earning max salary in their departments into Temporary Table& display
Temporary table data.
MS SQL Server
Assignment#4

Topic: Submitted By:

1. Write a query to Display the below result set from PROJECTS table.
PROJECT_ID DESCR DESCR-PROJECT_ID
401 Inventory Inventory - 401
402 Accounting Accounting - 402
403 Payroll Payroll - 403
404 Contact Mgmt Contact Mgmt - 404
Ans -

select PROJECT_ID,DESCR,DESCR + '-' + CAST(PROJECT_ID as nvarchar(30)) as [DESCR-PROJECT_ID] from


acroschema_17.PROJECTS

2. Write a query to Display the below result set from PROJECTS table.
PROJECT_ID DESCR START_DATE
401 Inventory 04-01-2011
402 Accounting 08-01-2011
403 Payroll 10-01-2011
404 Contact Mgmt 11-01-2011

Ans – select PROJECT_ID,DESCR,CONVERT(varchar(10),START_DATE,110) as [START_DATE]


from acroschema_17.PROJECTS

3. Write a query prints 25 random numbers between 100 and 200.


Ans - declare @value int =1
while(@value<=25)
begin
print cast((RAND()*(200-100)+100) as int)
set @value=@value+1
end

4. Create a function which calculates and returns the age of a person in year.
Ans -
create function acroschema_17.AgePerson(@DOB date)
returns int
as
begin
declare @age int
Set @age = DATEDIFF(YEAR,@DOB,GETDATE())
return @age
end

select acroschema_17.AgePerson('2000-07-20') as Age


5. Create a function which calculates and returns the age of a person in Month.
Ans - create function acroschema_17.AgePersonInMonth(@DOB date)
returns int
as
begin
declare @age int
Set @age = DATEDIFF(MM,@DOB,GETDATE())
return @age
end

select acroschema_17.AgePersonInMonth('2000-07-20') as AgeInMonth

6. Create a Function to Display client name whose project’s ‘Coding’ task and
status ‘In Progress’.
Ans - alter function acroschema_17.ClientName()
returns table
as
return (select C.CLIENT_ID,C.CNAME from acroschema_17.EMPPROJECTTASKS as emp
inner join acroschema_17.PROJECTS as p on emp.PROJECT_ID=p.PROJECT_ID
inner join acroschema_17.CLIENTS as C on C.CLIENT_ID=p.CLIENT_ID
where TASK = 'Coding' and STATUS='In Progress')

select * from acroschema_17.ClientName()


7. Create a Function to Display names of employees doing ‘System Analysis’ along with
project name.
Ans -
create function acroschema_17.GetSystem(@TASK varchar(30))
returns table
as
return (select * from acroschema_17.EMPPROJECTTASKS where TASK = @TASK)

select ENAME,DESCR,TASK from acroschema_17.GetSystem('System Analysis') A


inner join acroschema_17.PROJECTS B on A.PROJECT_ID = B.PROJECT_ID
inner join acroschema_17.EMPLOYEES C on A.EMPNO = C.EMPNO;

8. Create a Split Function to pass Comma separated (Delimited) string & Display string as a
table column value.
Ans - declare @char varchar(50) = 'Nikhil,gourav,Vaneet'
declare @char1 varchar(50)
declare @i int =1
declare @a int = len(@char)
while(@i<=@a)
begin
declare @num int
set @char1 = SUBSTRING(@char,@i,CHARINDEX(',',@char,1)-1)
set @i=@i+len(@char1)+1
select @char1
end

9. Create a stored procedure to insert employees table data into Temporary Table.
Ans -
create table acroschema_17.#tempEmployee
(
EMPNO INT, ENAME VARCHAR(30),JOB VARCHAR(50),SALARY INT, DEPTNO INT
)
CREATE PROCEDURE acroschema_17.EmpDetails
as
begin
insert into #tempEmployee
select * from acroschema_17.EMPLOYEES
end
exec acroschema_17.EmpDetails
select * from #tempEmployee
10. To insert department number, names and salaries of employees who are
earning max salary in their departments into Temporary Table& display
Temporary table data.
Ans - create table #SalaryMaxByEmployee
(
deptno int, ename varchar(30),salary int
)
insert into #SalaryMaxByEmployee
select deptno,ename,salary from acroschema_17.EMPLOYEES t1 where SALARY=(select
max(SALARY) from acroschema_17.EMPLOYEES
where DEPTNO=t1.DEPTNO
)
order by SALARY desc
select * from #SalaryMaxByEmployee

You might also like