SQL Assignment 3
SQL Assignment 3
Assignment # 3
1. Create a stored procedure to Display the number of Employee by each Job from Employees
table.
Ans - create procedure acroschema_17.emp
as begin
select JOB,count(JOB) as EMPNO FROM acroschema_17.EMPLOYEES group by JOB
end
exec acroschema_17.EMPLOYEES
2. Create a stored procedure to Display client name whose project’s ‘Coding’ task and status ‘In
Progress’.
Ans - create procedure acroschema_17.A1
as begin
select t1.CNAME from acroschema_17.CLIENTS t1 inner join
acroschema_17.PROJECTS t2 ON t2.CLIENT_ID = t1.CLIENT_ID
inner join acroschema_17.EMPPROJECTTASKS t3 on t3.PROJECT_ID=t2.PROJECT_ID
and t3.TASK='Coding' and t3.STATUS = 'In Progress'
end
exec acroschema_17.A1
3. Create a stored procedure to Display names of employees doing ‘System Analysis’ along with project
name
Ans -
create procedure acroschema_17.A2
as begin
select t1.ENAME,t3.DESCR from acroschema_17.EMPLOYEES t1 inner join
acroschema_17.EMPPROJECTTASKS t2 ON t2.EMPNO = t1.EMPNO
inner join acroschema_17.PROJECTS t3 on t3.PROJECT_ID=t2.PROJECT_ID
and t2.TASK='System Analysis' group by t1.ENAME,t3.DESCR
end
exec acroschema_17.A2
4. Display the Ename and count of Ename characters from the Employee table whose Ename
characters count greater than 4.
Ans - select ENAME,LEN(ENAME) AS char from acroschema_17.EMPLOYEES where
LEN(ENAME) > 4 group by ENAME
5. Display the Cname with remove blank on the left hand side & remove blank on the right hand
side from the Clients table.
Ans -
select LTRIM(RTRIM(CNAME)) as CLIENTS from acroschema_17.CLIENTS
6. Write a query to Display the below result set from Clients table.
Ans - select SUBSTRING(EMAIL,CHARINDEX('@',EMAIL)+1,LEN(EMAIL)-
CHARINDEX('@',EMAIL))
AS EMAILDOMAIN FROM acroschema_17.CLIENTS
EmailDomain
trackon.com
acmeutil.com
lawful.com
moneysaver.com
7. Write a query to Display the below result set from Clients table.
Ans -
select EMAIL,SUBSTRING(EMAIL,1,CHARINDEX('@',EMAIL)-1) AS
NAME ,SUBSTRING(EMAIL,CHARINDEX('@',EMAIL)+1,LEN(EMAIL)-CHARINDEX('@',EMAIL))
AS EMAILDOMAIN FROM acroschema_17.CLIENTS group by EMAIL
CLIENT_I
D RepeatName
1001 ACME UtilitiesACME UtilitiesACME Utilities
1002 Trackon ConsultantsTrackon ConsultantsTrackon Consultants
1003 MoneySaver DistributorsMoneySaver DistributorsMoneySaver Distributors
1004 Lawful CorpLawful CorpLawful Corp
10. Write a query to Display the below result set from Clients table.
Ans -
select EMAIL,STUFF(EMAIL,2,CHARINDEX('@',EMAIL),'*****')
FROM acroschema_17.CLIENTS
group by EMAIL
Email StuffedEmail
consult@trackon.com c*****rackon.com
contact@acmeutil.com c*****cmeutil.com
justice@lawful.com j*****awful.com
save@moneysaver.com s*****oneysaver.com
11. Write a query to Display the below result set from PROJECTS table.
Ans -
select PROJECT_ID ,DESCR,START_DATE,DATENAME(WEEKDAY,START_DATE) AS START_DATE_DAY,
MONTH(START_DATE) AS START_DATE_MONTH_NUMBER,
DATENAME(MONTH,START_DATE) AS START_DATE_MONTH_NAME,
YEAR(START_DATE) AS START_DATE_YEAR FROM acroschema_17.PROJECTS
PROJECT_I
D DESCR START_DATE START_DATE_Day START_DATE_MonthNumber START_DATE_MonthName START_DATE_Year
401 Inventory 01-04-2011 Friday 4 April 2011
402 Accounting 01-08-2011 Monday 8 August 2011
403 Payroll 01-10-2011 Saturday 10 October 2011
Contact
404 Mgmt 01-11-2011 Tuesday 11 November 2011
MS SQL Server
Assignment # 3