Вы находитесь на странице: 1из 10

1.What's the difference between a primary key and a unique key?

Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only. 2. What is bit datatype and what's the information that can be stored inside a bit column? Bit datatype is used to store boolean information like 1 or 0 (true or false). Untill SQL Server 6.5 bit datatype could hold either a 1 or 0 and there was no support for NULL. But from SQL Server 7.0 onwards, bit datatype can represent a third state, which is NULL. 3. Define candidate key, alternate key, composite key? A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary key of the table. If the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys. A key formed by combining at least two or more columns is called composite key. 4. What are defaults? Is there a column to which a default can't be bound? A default is a value that will be used by a column, if no value is supplied to that column while inserting data. IDENTITY columns and timestamp columns can't have defaults bound to them. See CREATE DEFUALT in books online. 5. CREATE INDEX myIndex ON myTable(myColumn) What type of Index will get created after executing the above statement? Non-clustered index. Important thing to note: By default a clustered index gets created on the primary key, unless specified otherwise. 6. What's the difference between DELETE TABLE and TRUNCATE TABLE commands? DELETE TABLE is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow. TRUNCATE TABLE also deletes all the rows in a table, but it won't log the deletion of each row, instead it logs the deallocation of the data pages of the table, which makes it faster. Of course, TRUNCATE TABLE can be rolled back.

7. What are constraints? Explain different types of constraints. Constraints enable the RDBMS enforce the integrity of the database automatically, without needing you to create triggers, rule or defaults. Types of constraints: NOT NULL, CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY 8. What are the steps you will take to improve performance of a poor performing query? This is a very open ended question and there could be a lot of reasons behind the poor performance of a query. But some general issues that you could talk about would be: No indexes, table scans, missing or out of date statistics, blocking, excess recompilations of stored procedures, procedures and triggers without SET NOCOUNT ON, poorly written query with unnecessarily complicated joins, too much normalization, excess usage of cursors and temporary tables. 9. What is a join and explain different types of joins.? Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table. Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER JOINS.
OUTER JOIN is a type of join which gives both the matching and unmatching in the table specified in the left side of the join statement if it is a left outer join and the matched, unmatched records of the table in the right side of the join statement if its a right outer join.

10. What is a self join? Explain it with an example? Self join is just like any other join, except that two instances of the same table will be joined in the query. Here is an example: Employees table which contains rows for normal employees as well as managers. So, to find out the managers of all the employees, you need a self join. CREATE TABLE emp ( empid int, mgrid int, empname char(10) ) INSERT emp SELECT 1,2,'Vyas' INSERT emp SELECT 2,3,'Mohan' INSERT emp SELECT 3,NULL,'Shobha'

INSERT emp SELECT 4,2,'Shridhar' INSERT emp SELECT 5,2,'Sourabh' SELECT t1.empname [Employee], t2.empname [Manager] FROM emp t1, emp t2 WHERE t1.mgrid = t2.empid Here's an advanced query using a LEFT OUTER JOIN that even returns the employees without managers (super bosses) SELECT t1.empname [Employee], COALESCE(t2.empname, 'No manager') [Manager] FROM emp t1 LEFT OUTER JOIN emp t2 ON t1.mgrid = t2.empid 11. How do you implement one-to-one, one-to-many and many-to-many relationships while designing tables? One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships. One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships. Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table. 12. State the advatage and disadvantage of Cursor's?
advantage :In pl/sql if you want perform some actions more than one records you should user these cursors only. bye using these cursors you process the query records. you can easily move the records and you can exit from procedure when you required by using cursor attributes. Disadvantage: using implicit/explicit cursors are depended by sutiation. If the result set is les than 50 or 100 records it is better to go for implicit cursors. if the result set is large then you should use exlicit cursors. Other wise it will put burdon on cpu.

Each time you fetch a row from the cursor, it results in a network roundtrip, where as a normal SELECT query makes only one rowundtrip, however large the resultset is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations). Furthere, there are restrictions on the SELECT statements that can be used with some types of cursors. 13. What is a transaction ?
It is a operation on database / table. which has to be confirmed after completion. It may be single sql stm or multiple stm. Transaction is logical unit between two commits and commit and rollback.

14. What is a join ?


Join is a query which retrieves related columns or rows from multiple tables 15. Explain the different types of joins ? Join is a query which retrieves related columns or rows from multiple tables.Self Join - Joining the table with itself.Equi Join - Joining two tables by equating two common columns.Non-Equi Join - Joining two tables by equating two common columns.Outer Join - Joining two tables in such a way that query can also retrieve rows that do not have corresponding join value in the other table. 16. What is the sub-query ? Sub query is a query whose return values are used in filtering conditions of the main query. 17. What is correlated sub-query? Need to explore Correlated sub query is a sub query which has reference to the main query. 18. Explain CONNECT BY PRIOR Need to explore

19. Difference between SUBSTR and INSTR?


INSTR (String1,String2(n,(m)),INSTR returns the position of the mth occurrence of the string 2 instring1. The search begins from nth position of string1.SUBSTR (String1 n,m)SUBSTR returns a character string of size m in string1, starting from nth position of string1 20. Explain UNION, MINUS, UNION ALL and INTERSECT? INTERSECT : Returns all distinct rows selected by both queries. MINUS : Returns all distinct rows selected by the first query but not by the second. UNION : Returns all distinct rows selected by either query. UNION ALL : Returns all rows selected by either query, including all duplicates. 21. What is ROWID ? ROWID is a pseudo column attached to each row of a table. It is 18 character long, blockno, rownumber are the components of ROWID. 22. What is an integrity constraint ? Integrity constraint restricts values of a column so that only meaningful values as per the business logic of the table be entered into the column. Integrity constraints are of the following types:

Not null Unique Primary Key Foreign Key (referential integrity) Check -- custom constraints e.g. check sal > 0, check joiningdate > date()

23. What is the usage of SAVEPOINTS ?


SAVEPOINTS are used to subdivide a transaction into smaller parts. It enables rolling back part of a transaction. Maximum of five save points are allowed. 24. What are the data types allowed in a table ? CHAR,VARCHAR2,NUMBER,DATE,RAW,LONG and LONG RAW. 25. What is difference between CHAR and VARCHAR2 ? CHAR pads blank spaces to the maximum length. VARCHAR2 does not pad blank spaces. For CHAR it is 255 and 2000 for VARCHAR2. 26. How many LONG columns are allowed in a table? Only one LONG columns is allowed. maximum of 5 is allowed 27. Is it possible to use LONG columns in WHERE clause or ORDER BY? Not possible. 28. What are the pre-requisites to modify datatype of a column and to add a column with NOT NULL constraint ? 1) To modify datatype of a column - ??? 2) To add a column with NOT NULL constraint - the table must have 0 rows. 29. Where the integrity constraints are stored in data dictionary ? The integrity constraints are stored in USER_CONSTRAINTS. 30. How will you activate/deactivate integrity constraints ? The integrity constraints can be enabled or disabled by ALTER TABLE ENABLE constraint/DISABLE constraint 31. If unique key constraint on DATE column is created, will it validate the rows that are inserted with SYSDATE ? If there is a unique key defined on a column which has date as the data type then one can insert the same date more than once.The date is always stored in the format dd-mon-yyyy hh:mi:ss.The reason why it will accept the same date again is that the time taken for you to enter the date for the first time and entering the date again for the second time there will be a gap of atleast one secon which makes that date value

unique. ONE CAN INSERT THE SAME DATE VALUE IN A UNIQUE COLUMN MORE THAN ONCE. 32. What is a database link ? Database Link is a named path through which a remote database can be accessed.

33. How to access the current value and next value from a sequence ?
I would like to give you a small example to use the sequence.currval and sequence.nextval create sequence seq_name start with 1 minvalue 1 maxvalue 999 increment by 1 nocycle insert into table_name (sno,name) values (seqname.nextval,'abc'); select seqname.currval from dual 34. Is it possible to access the current value in a session before accessing next value ? correct answer: NEXTAVAL must be issued for that sequence before CURRVAL contaons a value from Oracle Server SQL references 8 35. What is CYCLE/NO CYCLE in a Sequence ? CYCLE specifies that the sequence continues to generate values after reaching either maximum or minimum value. After pan ascending sequence reaches its maximum value, it generates its minimum value. After a descending sequence reaches its minimum, it generates its maximum.NO CYCLE specifies that the sequence cannot generate more values after reaching its maximum or minimum value 36. What are the advantages of VIEW ? Advantages of view: 1. Restricts the access to particular columns and rows of the base tables. 2. Hide the data complexity. 3. Can access the data for two different base tables with out performing a join. 4. Can display the data in different form from the base tables.(i.e. In the column names can can be changed with effecting the column names of the base tables).

37. Can a view be updated/inserted/deleted? If a view is update, deleted , or inserted wil the changes be refelected on the base table

38. If Yes - under what conditions?


An Insert,Update,delete can be done through views is 1. Inserts/updates/deletes done only in one base table at a time 2. Primary key columns should be part of view 3. Columns which are going to be updated must be part of vew def. 4. It shouldn't voielet the constraints 39. If a view on a single base table is manipulated will the changes be reflected on the base table ? If view on based on a single table then u can execute any DML directly on it and can see the changes in the base table. And if view is based on join of 2 tables then only one base table can be modified at one time so in order to make changes in both the tables use insted of triggers. 40. Explicit Cursor attributes? %IS OPEN,%FOUND,%NOT FOUND,%ROW COUNT. 41. Implicit Cursor attributes? SQL%FOUND,SQL%NOT FOUND, SQL%IS OPEN, SQL%ROW COUNT 42. To view installed Oracle version information? From the SQL> prompt, type the following: select * from v$version; 43. Display the number value in Words? select sal, (to_char(to_date(sal,'j'), 'jsp')) from emp; 44. Display Odd/ Even number of records? Odd number of records: select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp); 1 3 5 Even number of records: select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp) 2 4 6 45. Which date function returns number value ?

Months_betweenThis date function takes 2 valid dates and returns number of months in between them. 46. Any three PL/SQL Exceptions? TOO_MANY_ROWS NO_DATA_FOUND INVALID_CURSORS CURSOR_ALREADY_OPEN DUP_VAL_ON_INDEX 47. What are PL/SQL Cursor Exceptions? Cursor_already_open, Invalid_cursor 48. Other way to replace query result null value with a text? NVL or Decode. 49. What are the more common pseudo-columns ? SELECT rownum, rowid FROM sometable. 50. What are the different tablespaces in database? A tablespace is a collection of one or more datafiles.all database objects are stored in tablespaces. diff types of tablespaces:1)System tablespace. 2)Temp tablespace. 3)Tools tablespace 4)Users tablespace. 5)Rollback tablespace. 6)Data and index tablespace. 51. How to drop the index? Drop Index Indexname. 52. How to drop the column in a table? alter table"table name" drop column "column name", This drops the column immediately. However if there is huge data and you would like to postpone the task of dropping the columns you can make the columns unused and drop the unused columns during the weekend or less peak activity time.

53. What are the different types of SQL ? There are 5 types 1.Data definition type 2.Data manipulation 3.Data control 4.Transaction control 5.Data query 54. What do you know about subqueries ? Subqueries will be executed once for the entire parent statement. 55. Subquery vs Join? -- Need to explore Subquery retrive the data depending on certain condition or manupulation in inner query. where as joins will join the enitire data depending on the conditions given it cannot manupulate the data or we cannot be selective while using joins. 56. The use of HAVING , WHERE and GROUPBY in one SQL? "where" filters data before grouping "Having" filters data after grouping 57. What is meant by Scrollable cursor ? Need to explore A scrollable cursor, however, can move forward and backward, and can seek any desired record in the cursor. Such operations are common in applications that present results sets in scrolling windows. With a scrollable cursor, application developers do not need to create and manage their own buffer for the records 58. What is an Integrity Constraint ? Integrity constraint is a rule that restricts values to a column in a table. 59. What is ON DELETE CASCADE ? When ON DELETE CASCADE is specified ORACLE maintains referential integrity by automatically removing dependent foreign key values if a referenced primary or unique key value is removed. 60. find the two minimum salaries among table? select a.* from (select * from emp order by sal)a where rownum <3; 61. how do you generate prime numbers in sql not in plsql? need to explore 62. What is the difference between ROWNUM and ROWID?

The ROWNUM is a number(like serial no) which is dynamically changes.but the ROWID does not change.Because it is having the unique address,which is generated by the oracle.Both are pseudo columns. rowid is a hexadecimal representation and rownum is normal representation of rows uniquely. 63. What is Cartesian product in the SQL? when a join condition is omited when getting result from two tables then that kind of query gives us Cartesian product, in which all combination of rows displayed. All rows in the first table is joined to all rows of second table...... 64. Question is been asked in the interview., I have table Department with details like DeptId Dname Dlocation 10 Finance Del 20 Sales Mum 30 Marketing Blore The output should be in this format 10 20 30 Finance Sales Marketing Del Mum Blore The query I need in SQL not using any transformation. Can anyone help me out in this ? 65. Delete duplicate rows in a table delete from emp where (rowid,empno) not in (select min(rowid),empno from emp group by empno); 66. Write a query to display employee records having same salary? select * from emp where sal in (select sal from emp where rowid not in (select max(rowid) from emp group by sal)); 67. What is the Difference between stored procedures and external procedures? An external procedure is simply a program unit that is not written in Oracles SQL or PL/SQL and resides outside of the database. External procedures must be stored as shared libraries at the operating system level. In a Windows NT environment, the procedure would be compiled into a DLL. where as stored procedure is written in sql 68. What is the data type of the column of Dual table? varchar2 69.

Вам также может понравиться