You are on page 1of 5

1) What is the difference between PRIMARY KEY and UNIQUE KEY?

A UNIQUE constraint is similar to PRIMARY key, but you can have more than one UNIQUE constraint per table. When you declare a UNIQUE constraint, SQL Server creates a UNIQUE index to speed up the process of searching for duplicates. In this case the index defaults to NONCLUSTERED index, because you can have only one CLUSTERED index per table.

2) As shown below, TABLE1 has columns COL_A, COL_B and COL_C TABLE2 has columns COL_X, COL_Y and COL_Z The data in the tables are,
TABLE1 COL_A TIGER LION FOX COL_B TRUE TRUE FALSE COL_C ONE ONE ONE TABLE2 COL_X FOX WOLF DOG COL_Y FALSE FALSE TRUE COL_Z TWO TWO TWO

Question: What is the result of the below query? SELECT COL_A FROM TABLE1 UNION SELECT COL_X FROM TABLE2
(a) TIGER LION WOLF DOG (b) TIGER LION FOX FOX WOLF DOG (c) TIGER LION FOX WOLF DOG (d) none of (a), (b), (c)

Answer (choose a or b or c or d): C 3) What is the difference between DELETE and TRUNCATE commands?
TRUNCATE is a DDL command and cannot be rolled back. All of the memory space is released back to the server. DELETE is a DML command and can be rolled back.

4) How many clustered index can be created in a table which has 3 columns? a) 3 b) 6 c) 255

At the same t ime. Clustered indexes and non-clustered indexes in MS SQL. Non-clustered indexes are stored as B-tree structures (so do clustered indexes). depending up on the absence or presence of clustered index on the table. with the leaf level nodes having the index key and it’s row locater. all the indexes need to be updated. If you create an index on each column of a table. all the rows in the table are stored in the order of the clustered index key. Procedure may or not return value. DELETE) will become slow. there can be only one clustered index per table. Non-clustered indexes have their own storage separate from the table data storage. Procedure is pre-compiled set of statements. as every time data changes in the table. The row located could be the RID or the Clustered index key.d) None of the above Answer (choose a or b or c or d): C 5) What are the advantages and disadvantages of having an index on every column in a table? When you create a clustered index on a table. more disk space is used. But function has to return value. 6) What types of indexes can be created in MS-SQL / Oracle ? Indexes are of two types. Another disadvantage is that. So. 7) What is a CURSOR or what is the usage of a CURSOR? CURSOR is mostly used in stored procedures to take the set of value of a sql query & loop it each individual record to check / set / update conditions record by record. . it improves the query performance. 8) What is the difference between a PROCEDURE and a FUNCTION? 1. data modification operations (such as INSERT. indexes need disk space. UPDATE. the more indexes you have. 2. but function will compile every time you execute it. as the query optimizer can choose from all the existing indexes to come up with an efficient execution plan.

Having Clause is basically used only with the GROUP BY function in a query. DELETE. 10) What is the difference between a HAVING clause and a WHERE clause? HAVING can be used only with the SELECT statement. CUSTOMER CUST_ID 100 200 300 400 500 600 CUSTOMER_NAME JAMES PETER AZIZ RAJ THOMAS FAHAD ACCOUNT CUST_ID 100 200 300 400 600 600 600 ACCOUNT ACC_01 ACC_11 ACC_21 ACC_31 ACC_41 ACC_45 ACC_46 CREATED_ON 2007/12/11 2008/06/25 2008/09/30 2008/10/15 2008/12/01 2009/02/12 2009/03/03 ACTIVE YES YES YES NO YES NO YES Write SQL query to get the following items (i) Customers without any account SELECT CUSTOMER_NAME FROM CUSTOMER WHERE CUST_ID NOT IN (SELECT DISTINCT CUST_ID FROM ACCOUNT) . UPDATE statements. WHERE Clause can be used for specifying the search criteria in SELECT. 11) Consider the following tables. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used.9) How to force the results of a query to be sorted on a column called “First_Name”? Using the Order by clause to Column First_Name. HAVING behaves like a WHERE clause.

CREATED_ON.112).ACCOUNT FROM CUSTOMER C.CUST_ID = A.CUST_ID WHERE ACTIVE =”NO” (iv) Accounts created in the year 2008 SELECT C. INNER JOIN ACCOUNT A ON C.CUSTOMER_NAME.CUST_ID END .CUSTOMER_NAME.CUST_ID = A. A.Usp_Fetch_Customer @customer as varchar(20) output.ACCOUNT FROM CUSTOMER C. @acct as varchar(20) output AS BEGIN Select @customer = c. please show that as well.CUST_ID = A.3) =”2008” (v) Please write a stored procedure / function to return [Customer_name] receiving [Account] value.CUST_ID WHERE 2 < (SELECT CUST_ID FROM ACCOUNT) (iii) Customers and their Inactive accounts SELECT C. INNER JOIN ACCOUNT A ON C. If you think of more than one way. @acct =a.customer_name. INNER JOIN ACCOUNT A ON C. INNER JOIN ACCOUNT A ON C.CUST_ID = A.CUST_ID WHERE (CONVERT(VARCHAR.CUSTOMER_NAME. A.ACCOUNT FROM CUSTOMER C.(ii) Customers who have more than one account SELECT C. CREATE PROCEDURE dbo. A.account From CUSTOMER C.

12. (2) please show how you can convert DATE type value into a STRING value in ‘YYYYMMDD’ format. and (3) how you can convert STRING value in ‘YYYYMMDD’ into DATE type value. In any RDBMS that you are familiar with.getdate(). (1) please show how you get today’s system date with current time. MS SQL (1) Select getdate() (2) Convert(varchar.112) (3) Cast(‘20101231’ as datetime) .