7

Click here to load reader

Sql Assignment 2

Embed Size (px)

Citation preview

Page 1: Sql Assignment 2

Assignment 2

• Create trigger for the table EmployeeAllowance for insert, delete and update

Create a table say logtable ( intID - auto incrementing Primary Key EmployeeID, AllowanceID, Amount, Operation - vrachra(15), ChangedDate ) Insert trigger should insert an entry for the newly inserted record Update trigger should insert an entry for the record before update Delete trigger should insert for the record which is going to be deleted The logtable should have a text field (operation) which has the values - insert/update/delete. The ChangedDate should have the date at the time of insert/update/delete.

• CREATE TABLE #temp (

name NVARCHAR(50) NULL, middlename NVARCHAR(50) NULL, lastname NVARCHAR(50) NULL ); INSERT INTO #temp VALUES( 'Name', NULL, 'Last Name' ); INSERT INTO #temp VALUES( 'Name', 'Middle Name', 'Last Name' ); SELECT name +middlename + lastname AS Result FROM #temp; Without using ISNULL how can you return the same output for both the select statements?

• Query to get the string after the second hyphen in any string. Suppose the string is SQL-Server-2012-Training. Output must be 2012-Training

• Query to insert 2012 in the string ‘SQL Server Training’. Output must be SQL Server 2012 Training

• Suppose I have a table with a bit column ‘Gender’ with values True and False. I need to make the True values to False and False values to True. Write a Query for it. CREATE TABLE [dbo].[StudentMarks] ( [Id] int NULL, [Name] varchar(20) NULL, [Marks] float null ) INSERT INTO [StudentMarks] VALUES (1, 'Abin ', 100), (2, 'Prasad',90), (3, 'Arjun',80), (4, 'Anish',82), (5, 'Reshma',70),

Page 2: Sql Assignment 2

(6, 'Ajay',100), (7, 'Jisna',83), (8, 'Jins',90), (9, 'Reshmi',65), (10, 'Ajayan ',23), (11, 'Antony',45), (12, 'Bhaskar',70), (13, 'Lekshmi',55), (14, 'Ajaya ',90), (15, 'Anto',100), (16, 'Kalyani',65) I need to get the Rank of students. Note: Students with the same mark must have the same rank

• CREATE TABLE [dbo].[SplitColumns] ( [Id] [int] NULL, [Name] [varchar](20) NULL ) INSERT INTO [SplitColumns] VALUES (1, 'Abin,Ashok'), (2, 'Prasad,G'), (3, 'Arjun,Ramachandran'), (4, 'Anish,Kumar'), (5, 'Reshma,Rajan'), (6, 'Ajay,Krishnan'), (7, 'Jisna,Antony'), (8, 'Jins,Peter') GO The output must be as below:

Write 3 queries using the three following methods

• Substring and charindex • XML • Parsename

• Is a primary key a clustered index? Why? • CREATE TABLE [dbo].[Team](

[MemberId] [int] , [Name] [varchar](50) , [LeadId] [int] ,

Page 3: Sql Assignment 2

) INSERT INTO dbo.Team ([MemberId], [Name], [LeadId]) VALUES (1,'Mathew',NULL), (2,'John',NULL), (3,'Manmohan',NULL), (4,'Kannan',1), (5,'Jibu',2), (6,'Kiran',3), (7,'Sandeep',2), (8,'Joe',4), (9,'Prasad',3), (10,'Abin',3), (11,'Minu',1), (12,'Rekha',2), (13,'Ismail',5) Output is required in the following format:

Write 2 queries one with Self Join and the other with CTE.

• DECLARE @ProjectPlan TABLE ( ProjectCode VARCHAR(10), PlanCode VARCHAR(10), StartDate DATE ) INSERT INTO @ProjectPlan VALUES (' Proj00001', 'P00001', '1-Sep-2014') INSERT INTO @ProjectPlan VALUES (' Proj00001', 'P00002', '1-Oct-2014') INSERT INTO @ProjectPlan VALUES (' Proj00001', 'P00003', '10-Oct-2014') INSERT INTO @ProjectPlan VALUES (' Proj00001', 'P00004', '25-Oct-2014')

Page 4: Sql Assignment 2

INSERT INTO @ProjectPlan VALUES (' Proj00002', 'P00001', '1-Oct-2014') INSERT INTO @ProjectPlan VALUES (' Proj00002', 'P00002', '1-Nov-2014') Output must be as below

For Proj00001, the Plan P00001 starts on 1-Sep-2014 and as Plan P00002 starts at 1-Oct-2014, the plan P00001 ends on 30-Sept-2014.

• CREATE TABLE dbo.Location(

LocationID int NOT NULL, LocationName varchar(100) NOT NULL, ParentLocationID int NULL, LocationType varchar(20) NOT NULL CONSTRAINT PK_Location PRIMARY KEY CLUSTERED ( LocationID ASC ) ON [PRIMARY]) INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(1, 'India', null, 'Country') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(2, 'United States', null, 'Country') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(3, 'Kerala', 1, 'State') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(4, 'Kollam', 3, 'City') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(5, 'Kochi', 3, 'City') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(6, 'Kozhikode', 3, 'City') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(7, 'Florida', 2, 'State') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(8, 'Miami', 7, 'City')

Page 5: Sql Assignment 2

INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(9, 'Illinois', 2, 'State') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(10, 'Chicago', 9, 'City') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(11, 'Chennai', 1, 'State') INSERT INTO dbo.Location(LocationID,LocationName,ParentLocationID,LocationType) VALUES(12, 'Nagercoil', 11, 'City') The Country name will be given as input. Write a query to get the cities in the country.

• Write script to add Check constraints in the EmployeeAllowance table which will make sure the

Amount is not null and always greater than zero.

• Write script to add Unique constraint for the column AllowanceName in the Allowance table

• Write a query to find the nth highest salary from the EmployeeAllowance table. Choose the best

method. Why?

• CREATE TABLE [dbo].[Details]

(

[Id] int NULL,

[Name] nvarchar(50) NULL,

[Address] nvarchar(200) NULL,

[DateofEntry] datetime NULL

)

INSERT INTO [Details] VALUES

(1, 'Abin,Ashok', 'Address1Address1Address1', '2015-10-25'),

(2, 'Prasad,G', 'Address1Address1', '2000-11-01'),

(3, 'Arjun,Ramachandran', 'Address1', '1998-05-10'),

(4, 'Anish,Kumar', 'Address1Address1Address1', '1999-01-01'),

(5, 'Reshma,Rajan', 'Address1Address1', '1999-12-12'),

Page 6: Sql Assignment 2

(6, 'Ajay,Krishnan', 'Address1Address1', '2013-06-01'),

(7, 'Jisna,Antony', 'Address1vAddress1', '2011-07-01'),

(8, 'Jins,Peter', 'Address1Address1', '1999-08-21') ,

(9, 'Abin,Ashok', 'Address1Address', '1996-09-22'),

(10, 'Abin,Ashok', 'Address1Address', '1980-02-23'),

(11, 'Abin,Ashok', 'Address1AddressAddress', '1970-03-24'),

(12, 'Arjun,Ramachandran', 'Address1AddressAddress', '1960-04-25'),

(13, 'Anish,Kumar', 'Address1000', '1950-02-26'),

(14, 'Reshma,Rajan', 'Address1Address1000', '1962-04-27'),

(15, 'Ajay,Krishnan', 'Address1Address1000', '1945-08-28'),

(16, 'Arjun,Ramachandran', 'Address1Address1000', '1985-10-29'),

(17, 'Anish,Kumar', 'Address1Address1000Address1000', '1942-12-30'),

(18, 'Reshma,Rajan', 'Address1Address1000Address1000Address1000', '1974-12-31'),

(19, 'Ajay,Krishnan', 'Address1Address1000', '1975-11-01'),

(20, 'Arjun,Ramachandran', 'Address1Address1000', '2015-12-02'),

(21, 'Anish,Kumar', 'Address2000Address2000', '2009-01-03'),

(22, 'Reshma,Rajan', 'Address1Address2000', '2008-04-04'),

(23, 'Ajay,Krishnan', 'Address1Address2000', '2004-09-05'),

(24, 'Prasad,G', 'Address1Address2000Address2000', '2013-02-13'),

(25, 'Prasad,G', 'Address1Address2000Address2000', '1993-11-20')

Write a query to find the latest address of each person.

Write another query to find the number of duplicate records of each person

• Input is a string and a character( can be a single quotes, an alphabet, a number, a comma , any

special character) within the string.

Page 7: Sql Assignment 2

Write a query to delete the characters in the string after the input character.

Eg: the string is ‘SQLServer-2000’ and the character is -. The output will be ‘SQLServer’