4
1. CREATE TABLE CountNULL ( ID INT NULL ) GO INSERT INTO CountNULL(ID) VALUES (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (2), (3), (8), (9), (NULL) SELECT COUNT(*) NULLCount FROM CountNULL WHERE ID IS NULL SELECT COUNT(0) NULLCount FROM CountNULL WHERE ID IS NULL SELECT COUNT(1) NULLCount FROM CountNULL WHERE ID IS NULL SELECT COUNT(5) NULLCount FROM CountNULL WHERE ID IS NULL Check the output. Explain 2. IF (OBJECT_ID('[dbo].[Student]', 'U') IS NOT NULL) BEGIN DROP TABLE [dbo].[Student] END GO CREATE TABLE [dbo].[Student] (

Sql Assignment 3

Embed Size (px)

Citation preview

Page 1: Sql Assignment 3

1. CREATE TABLE CountNULL( ID INT NULL)GO INSERT INTO CountNULL(ID)VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(2),(3),(8),(9),(NULL)

SELECT COUNT(*) NULLCount FROM CountNULL WHERE ID IS NULLSELECT COUNT(0) NULLCount FROM CountNULL WHERE ID IS NULLSELECT COUNT(1) NULLCount FROM CountNULL WHERE ID IS NULLSELECT COUNT(5) NULLCount FROM CountNULL WHERE ID IS NULL

Check the output. Explain

2. IF (OBJECT_ID('[dbo].[Student]', 'U') IS NOT NULL)BEGIN DROP TABLE [dbo].[Student]END

GOCREATE TABLE [dbo].[Student](

[Gender] bit NULL,[Name] varchar(20) NULL,[ID] int NULL

)GO

Page 2: Sql Assignment 3

INSERT INTO [Student] VALUES(1, 'abc1',1),(0, 'abc1',2),(1, 'abc1',3),(1, 'def1',4),(1, 'def1',5),(1, 'ghi',6),(0, 'ghi',7),(1, 'jkl',8),(1, 'jkl',9),(1, 'jkl ',10),(1, 'mno',11),(1, 'mno',12),(1, 'mno',13),(1, 'mno ',14),(1, 'pqr',15),(1, 'stu',16),(1, 'stu',17)GOWrite a query to do the followingDelete the first row among all sets of duplicate rows in the table. If a record has no duplicates, delete it.

3. Suppose I have a Shop table and an Item table. Write a query to get the sales of each Shop and each Item. The query should also return the details of Shops which had no sales for any item. The table structures are givenCREATE TABLE [dbo].[Shop](

[ShopName] varchar(20) NULL,[ShopID] int NULL

)GOCREATE TABLE [dbo].[Item](

[ItemName] varchar(20) NULL,[ItemID] int NULL,[ShopID] int NULL

)GOCREATE TABLE [dbo].[Sales](

Page 3: Sql Assignment 3

[ID] int NULL,[ItemID] int NULL,[ShopID] int NULL,[Amount] money NULL

)GO

4. Write a query to add a column into the existing table Employee called Age. It must be a computed column. Would you make it a persisted or non-persisted computed column? Why?

5. Suppose you need to add a column Email in a table with millions of records. Write query for it. Keep in mind the situation where you would need to search/update email ids of some/all records in the table.

6. You have created foreign key constraints. What are DELETE CASCADE and UPDATE CASCADE? Benefits of using them if any.

7. Which normal form do you generally use? Why? Demerits of other normal forms?8. Create a DATA DICTIONARY for the EmployeeDB. What is its use?9. Suppose a table has columns with names – ID, Name, Address. I need to add a column –

‘Date of Birth’ to the table in such a way that the column order is ID, First Name, Date of Birth, Address. How can you do that?

10. Find the output of the query DECLARE @value bitIF @value = 1 DECLARE @Sample TABLE (id int, textdata varchar(50) )ELSE INSERT INTO @Sample (id, textdata) select 1, 'sample data'

SELECT * FROM @Sample

Explain the output.

11. Example to illustrate the difference between UNION and UNION ALL.12. When will you use EXCEPT and INTERSECT?

For the scripts, send me as .sql files. For the answers, send me as .doc files.