Tuesday, June 3, 2008

SQL Interview Questions Answers - Vol 1

1. What is SQL?
A:SQL stands for 'Structured Query Language'.

2. What is SELECT statement?
A:The SELECT statement lets you select a set of values from a table in a database. The values selected from the database table would depend on the various conditions that are specified in the SQL query.

3. How can you compare a part of the name rather than the entire name?
A:SELECT * FROM people WHERE empname LIKE '%ab%'Would return a recordset with records consisting empname the sequence 'ab' in empname .

4. What is the INSERT statement?
A:The INSERT statement lets you insert information into a database.

5. How do you delete a record from a database?
A:Use the DELETE statement to remove records or any particular column values from a database.

6. How could I get distinct entries from a table?
A:The SELECT statement in conjunction with DISTINCT lets you select a set of distinct values from a table in a database. The values selected from the database table would of course depend on the various conditions that are specified in the SQL query. ExampleSELECT DISTINCT empname FROM emptable

7. How to get the results of a Query sorted in any order?
A:You can sort the results and return the sorted results to your program by using ORDER BY keyword thus saving you the pain of carrying out the sorting yourself. The ORDER BY keyword is used for sorting.SELECT empname, age, city FROM emptable ORDER BY empname

8. How can I find the total number of records in a table?
A:You could use the COUNT keyword , exampleSELECT COUNT(*) FROM emp WHERE age>40

9. What is GROUP BY?
A:The GROUP BY keywords have been added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called. Without the GROUP BY functionality, finding the sum for each individual group of column values was not possible.

10. What is the difference among "dropping a table", "truncating a table" and "deleting all records" from a table.
A:Dropping : (Table structure + Data are deleted), Invalidates the dependent objects ,Drops the indexes
Truncating: (Data alone deleted), Performs an automatic commit, Faster than delete
Delete : (Data alone deleted), Doesn’t perform automatic commit

11. What are the Large object types suported by Oracle?
A:Blob and Clob.

12. Difference between a "where" clause and a "having" clause.
A:Having clause is used only with group functions whereas Where is not used with.

13. What's the difference between a primary key and a unique key?
A: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.

14. What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors?
A:Cursors allow row-by-row prcessing of the resultsets.
Types of cursors: Static, Dynamic, Forward-only, Keyset-driven. See books online for more information.
Disadvantages of cursors: 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.
Most of the times, set based operations can be used instead of cursors.

15. What are triggers? How to invoke a trigger on demand?
A:Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table.
Triggers can't be invoked on demand. They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens on the table on which they are defined.
Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints for this purpose, instead of triggers, as constraints are much faster.

16. What is a join and explain different types of joins.
A: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.

17. How would you find out the total number of rows in a table?
A:Use SELECT COUNT(*) ... in query

18. How do you eliminate duplicate values in SELECT ?
A:Use SELECT DISTINCT ... in SQL query

19. How you insert records into a table
A:Using SQL INSERT statement

20. How do you delete record from a table ?
A:Using DELETE statementExample : DELETE FROM EMP

21. How do you select a row using indexes?
A:Specify the indexed columns in the WHERE clause of query.

22. How do you find the maximum value in a column?
A:Use SELECT MAX(...) .. in query

23. How do you retrieve the first 5 characters of FIRSTNAME column of table EMP ?
A:SELECT SUBSTR(FIRSTNAME,1,5) FROM EMP

24. My SQL statement SELECT AVG(SALARY) FROM EMP yields inaccurate results. Why?
A:Because SALARY is not declared to have NULLs and the employees for whom the
salary is not known are also counted.

25. How do you concatenate the FIRSTNAME and LASTNAME from EMP table to give a complete name?
A:SELECT FIRSTNAME ‘ ‘ LASTNAME FROM EMP

26. What is UNION,UNION ALL in SQL?
A:UNION : eliminates duplicatesUNION ALL: retains duplicatesBoth these are used to combine the results of different SELECT statements.
Suppose I have five SQL SELECT statements connected by UNION/UNION ALL, how many timesshould I specify UNION to eliminate the duplicate rows?
Once.

27. In the WHERE clause what is BETWEEN and IN?
A:BETWEEN supplies a range of values while IN supplies a list of values.

28. Is BETWEEN inclusive of the range values specified?
A:Yes.

29. What is 'LIKE' used for in WHERE clause? What are the wildcard characters?
A:LIKE is used for partial string matches. ‘%’ ( for a string of any character ) and ‘_’ (for any single character ) are the two wild card characters.

30. When do you use a LIKE statement?
A:To do partial search e.g. to search employee by name, you need not specify the complete name; using LIKE, you can search for partial string matches. Example SQL : SELECT EMPNO FROM EMP WHERE EMPNAME LIKE 'RAMESH%' % is used to represent remaining all characters in the name.This query fetches all records contains RAMESH in six characters.

31. What do you accomplish by GROUP BY ... HAVING clause?
A:GROUP BY partitions the selected rows on the distinct values of the column on which you group by. HAVING selects GROUPs which match the criteria specified

32. Consider the employee table with column PROJECT nullable. How can you get a listof employees who are not assigned to any project?
A:SQL : SELECT EMPNO FROM EMP WHERE PROJECT IS null;

33. What are the large objects supported by oracle and db2?
A:Blob , Clob ( Binary Large Objects, Character Large Objects)

34. What's the difference between a primary key and a unique key?
A:Primary key wont allow nulls, unique key allow nulls.Both Primary key and Unique key enforce the uniqueness of the column on which they are defined.

35. What is a join and explain different types of joins?
A:INNER JOINOUTER JOINLEFT OUTER JOINRIGHT OUTER JOINFULL OUTER JOIN

36. What is a self join?
A:Joining two instances of a same table. Sample SQL : SELECT A.EMPNAME , B.EMPNAME FROM EMP A, EMP B WHERE A.MGRID = B.EMPID

37. What is a transaction and ACID?
A:Transaction - A transaction is a logicl unint of work. All steps must be commited or rolled back.
ACID - Atomicity, Consistency, Isolation and Duralbility, these are properties of a transaction.

38. Materialized Query Tables in db2 ( This feature might not be available in oracle) ?
A:Materialized Query Tables or MQTs are also known as automatic summary tables. A materialized query table (MQT) is a table whose definition is based upon the result of a query. The data that is contained in an MQT is derived from one or more tables on which the materialized query table definition is based. MQT improve the query performance. Sample SQL to creat MQT. CREATE TABLE CUSTOMER_ORDER AS (SELECT SUM(AMOUNT) AS TOTAL_SUM, TRANS_DT, STATUS FROM DB2INST2.CUSTOMER_ORDER WHERE TRANS_DT BETWEEN '1/1/2001' AND '12/31/2001' GROUP BY TRANS_DT, STATUS) DATA INITIALLY DEFERRED REFRESH DEFERRED;

39. The IN operator may be used if you know the exact value you want to return for at least one of the columns.
A:SELECT column_name FROM table_name WHERE column_name IN (value1,value2,..)

40. BETWEEN ... AND
A:SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND value2 The values can be numbers, text, or dates.

No comments: