Nikhil SQL Assigment 4
Nikhil SQL Assigment 4
Assignment#4
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
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
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 -
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
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
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')
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