Você está na página 1de 4

SQL INTERVIEW QUESTIONS AND ANSWERS

1) What are the Difference between Functions and Procedures? Answer: 1. Functions are used for computations where as Procedures can be used for performing business logic 2. Functions MUST return a value, Procedures need not be. 3) you can have DML(insert, update, delete) statements in a Function. But, you cannot call such a function in a SQL query.. eg: suppose, if u have a function that is updating a table.. you can't call that function in any sql query. - select myFunction(field) from sometable; will throw error. 4) function parameters are always IN, no OUT is possible 5) function returns 1 value only. procedure can return multiple values(max. 1024) 6) we can select the fields from function.in the case of procdure we cannot select the fields. 7) Function do not return the images,text whereas stored procedures returns all 2) What is the difference between UNION and UNION ALL ? Answer: UNION ALL selects all records between two queries including duplicates, whereas UNION takes the time to sort records in order to remove duplicates. In short UNION only selects distinct values, UNION ALL selects all values. e.g. SELECT Date FROM table1 UNION SELECT Date FROM table2 SELECT Date FROM table1 UNION ALL SELECT Date FROM table2 3) What is a join and explain different types of joins. Answer: 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. <b> Types of joins </b> INNER JOIN, OUTER JOIN, CROSS JOIN. OUTER JOIN is further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER JOINS. Table details <b> Student </b> ID FirstName LastName BirthDate <b> Attendance </b> ID AttendanceDate AttendanceCode MinutesAbsent <b> StudentSchedule </b> ID Course <b> Cross Join </b> - A cross join merges two tables on every record in a geometric fashion, every record of one table is combined with every record from the other table. Two tables of 10 records each in a cross join will create a table of 100 (10 X 10) records. <b> Inner Join </b> - An Inner join is used to match two tables based on values of a common field. An inner join gets data from both tables where the specified data exists in both tables. For E.g. List of students in the database that were absent on December 4, 2003, you would use an inner join between the two tables "Student" and "Attendance"

SELECT Student.ID, Student.FirstName, Student.LastName, Attendance . AttendanceCode, Attendance.MinutesAbsent FROM Student INNER JOIN Attendance ON Student.ID=Attendance.ID WHERE Attendance.AttendanceDate='12/4/2003' <b> Outer Join </b> - An outer join gets data from the source table at all times, and returns data from the outer joined table ONLY if it matches the criteria. When using outer joins, fields will be set to NULL if data does not exist in the outer-joined table. For E.g. SELECT Student.ID, Student.FirstName, Student.LastName, Attendance. AttendanceCode , Attendance.MinutesAbsent FROM Student INNER JOIN StudentSchedule ON StudentSchedule.ID=Student.ID < br /> LEFT OUTER JOIN Attendance ON Student.ID=Attendance.ID AND Attendance.AttendanceDate='12/4/2003' WHERE StudentSchedule. Course = 'ENGLISH 9' <i> a </i>; Left Outer join - All outer joins retrieve records from both tables, just as an inner join does. However, an outer join retrieves all of the records from one of the tables. A column in the result is NULL if the corresponding input table did not contain a matching record.<i>; b </i> Right Outer join - The right outer join is similar to the left outer join in that it retrieves all the records from one side of the relationship , but this time it's the right table. Only records where the condition values match are retrieved from the left. <i> c </i> Full Outer Join - The full outer join retrieves all records from both the left and the right table. For 'n' number of SQL SELECT statements connected by UNION, how many times should we specify UNION to eliminate the duplicate rows? Answer: Only Once In the WHERE clause what is BETWEEN and IN? Answer: IN keyword helps to limit the selection criteria to one or more discrete values, the BETWEEN keyword allows for selecting a range. The syntax for the BETWEEN clause is as follows: SELECT "column_name" FROM "table_name" WHERE "column_name" BETWEEN 'value1' AND 'value2' This will select all rows whose column has a value between 'value1' and 'value2'. The syntax for using the IN keyword is as follows: SELECT "column_name"FROM "table_name" WHERE "column_name" IN ('value1', 'value2', ...) The number of values in the parenthesis can be one or more, with each values separated by comma. Values can be numerical or characters. If there is only one value inside the parenthesis, this commend is equivalent to WHERE "column_name" = 'value1' 4) Is BETWEEN inclusive of the range values specified? Answer: Yes 5) What is 'LIKE' used for in WHERE clause? What are the wildcard characters? Answer: LIKE is another keyword that is used in the WHERE clause. Basically, LIKE allows you to do a search based on a pattern rather than specifying exactly what is desired (as in IN) or spell out a range (as in BETWEEN). The syntax for is as follows: SELECT "column_name" FROM "table_name" WHERE "column_name" LIKE {PATTERN} % ( for a string of any character ) and _ (for any single character ) are the two wild card characters.

6) When do you use a LIKE statement? Answer: 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. 7) What is a self join and give an example. Answer: Joining two instances of a same table. Sample SQL : SELECT A.EMPNAME , B.EMPNAME FROM EMP A, EMP B WHERE A.MGRID = B.EMPID 8) What is a transaction? Answer: A transaction is a logical unit of work where all steps must be committed or rolled back. 9) What is ACID? Answer: ACID stands for Atomicity, Consistency, Isolation and Duralbility. These are the properties of a transaction. 1. Atomicity - states that database modifications must follow an "all or nothing" rule. Each transaction is said to be "atomic." If one part of the transaction fails, the entire transaction fails. It is critical that the database management system maintain the atomic nature of transactions in spite of any DBMS, operating system or hardware failure. 2. Consistency - states that only valid data will be written to the database. If, for some reason, a transaction is executed that violates the databases consistency rules, the entire transaction will be rolled back and the database will be restored to a state consistent with those rules. On the other hand, if a transaction successfully executes, it will take the database from one state that is consistent with the rules to another state that is also consistent with the rules. 3. Isolation - requires that multiple transactions occurring at the same time not impact each other's execution. For example, if Joe issues a transaction against a database at the same time that Mary issues a different transaction, both transactions should operate on the database in an isolated manner. The database should either perform Joe's entire transaction before executing Mary's or vice-versa. This prevents Joe's transaction from reading intermediate data produced as a side effect of part of Marys transaction that will not eventually be committed to the database. Note that the isolation property does not ensure which transaction will execute first, merely that they will not interfere with each other. 4. Durability - ensures that any transaction committed to the database will not be lost. Durability is ensured through the use of database backups and transaction logs that facilitate the restoration of committed transactions in spite of any subsequent software or hardware failures. 10) How do you perform a search based on part of a word? Answer: SELECT * FROM emp WHERE dept LIKE '%war%'. The above query will return a recordset with records consisting 'war' in the department name. E.g. soft<b>war</b>e (war), hard<b>war</b>e(war).

11) What is GROUP BY? Answer: 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. The corresponding SQL syntax is, SELECT "column_name1", SUM("column_name2") FROM "table_name" GROUP BY "column_name1"; 12) What is the difference between dropping, truncating and deleting from a table. Answer: 1. Dropping - (Table structure + Data are deleted), Invalidates the dependent objects ,Drops the indexes 2. Truncating - (Data alone deleted), Performs auto commit and is faster than delete 3. Delete - (Data alone deleted), Auto commit is not performed. 13) What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors? Answer: Cursors allow row-by-row prcessing of the resultsets. Different Types of cursors - Static, Dynamic, Forward-only, Keyset-driven. 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. 14) What are triggers? How to invoke a trigger on demand? Answer: 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. 15) What is a tuple? Answer: In a table, a tuple is a row. Each row in the table represents a collection of related data values.

Você também pode gostar