Sql interview questions and answers

  • 771 views
Uploaded on

Sql interview questions and answers

Sql interview questions and answers

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • For more SQL interview questions to practice,have a look on @ http://skillgun.com/sql/interview-questions-and-answers
    Are you sure you want to
    Your message goes here
    Be the first to like this
No Downloads

Views

Total Views
771
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
63
Comments
1
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com Database Fundamentals Explain codd’s rules. 1. Foundation Rule A relational database management system must manage its stored data using only its relational capabilities. 2. Information Rule All information in the database should be represented in one and only one way - as values in a table. 3. Guaranteed Access Rule Each and every datum (atomic value) is guaranteed to be logically accessible by resorting to a combination of table name, primary key value and column name. 4. Systematic Treatment of Null Values Null values (distinct from empty character string or a string of blank characters and distinct from zero or any other number) are supported in the fully relational DBMS for representing missing information in a systematic way, independent of data type. 5. Dynamic On-line Catalog Based on the Relational Model The database description is represented at the logical level in the same way as ordinary data, so authorized users can apply the same relational language to its interrogation as they apply to regular data. 6. Comprehensive Data Sublanguage Rule A relational system may support several languages and various modes of terminal use. However, there must be at least one language whose statements are expressible, per some well-defined syntax, as character strings and whose ability to support all of the following is comprehensible: a. data definition b. view definition c. data manipulation (interactive and by program) d. integrity constraints e. authorization f. transaction boundaries (begin, commit, and rollback). 7. View Updating Rule All views that are theoretically updateable are also updateable by the system. 8. High-level Insert, Update, and Delete The capability of handling a base relation or a derived relation as a single operand applies nor only to the retrieval of data but also to the insertion, update, and deletion of data. 9. Physical Data Independence Application programs and terminal activities remain logically unimpaired whenever any changes are made in either storage representation or access methods. 10. Logical Data Independence Application programs and terminal activities remain logically unimpaired when information preserving changes of any kind that theoretically permit unimpairment are made to the base tables. 11. Integrity Independence Integrity constraints specific to a particular relational database must be definable in the relational data sublanguage and storable in the catalog, not in the application programs. 12. Distribution Independence The data manipulation sublanguage of a relational DBMS must enable application programs and
  • 2. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com terminal activities to remain logically unimpaired whether and whenever data are physically centralized or distributed. 13. Nonsubversion Rule If a relational system has or supports a low-level (single-record-at-a-time) language, that low- level language cannot be used to subvert or bypass the integrity rules or constraints expressed in the higher-level (multiple-records-at-a-time) relational language. If any 6 rules are satisfied with the database then that will comes under RDBMS. What is database? A database is a logically coherent collection of data with some inherent meaning, representing some aspect of real world and which is designed, built and populated with data for a specific purpose. What is DBMS? It is a collection of programs that enables user to create and maintain a database. In other words it is general-purpose software that provides the users with the processes of defining, constructing and manipulating the database for various applications. What is RDBMS? Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained across and among the data and tables. In a relational database, relationships between data items are expressed by means of tables. Interdependencies among these tables are expressed by data values rather than by pointers. This allows a high degree of data independence. An RDBMS has the capability to recombine the data items from different files, providing powerful tools for data usage. Describe the three levels of data abstraction? There three levels of abstraction: 1. Physical level: The lowest level of abstraction describes how data are stored. 2. Logical level: The next higher level of abstraction, describes what data are stored in database and what relationship among those data. 3. View level: The highest level of abstraction describes only part of entire database. Define the "integrity rules"? There are two Integrity rules. 1. Entity Integrity: States that "Primary key cannot have NULL value" 2. Referential Integrity: States that "Foreign Key can be either a NULL value or should be Primary Key value of other relation.
  • 3. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com What is Data Independence? Data independence means that "the application is independent of the storage structure and access strategy of data". In other words, the ability to modify the schema definition in one level should not affect the schema definition in the next higher level. Two types of Data Independence: 1. Physical Data Independence: Modification in physical level should not affect the logical level. 2. Logical Data Independence: Modification in logical level should affect the view level. NOTE: Logical Data Independence is more difficult to achieve What is Data Model? A collection of conceptual tools for describing data, data relationships data semantics and constraints. What is Object Oriented model? This model is based on collection of objects. An object contains values stored in instance variables with in the object. An object also contains bodies of code that operate on the object. These bodies of code are called methods. Objects that contain same types of values and the same methods are grouped together into classes. What is E-R model? This data model is based on real world that consists of basic objects called entities and of relationship among these objects. Entities are described in a database by a set of attributes. What is an Entity? It is a 'thing' in the real world with an independent existence. What is Weak Entity set? An entity set may not have sufficient attributes to form a primary key, and its primary key compromises of its partial key and primary key of its parent entity, then it is said to be Weak Entity set. What is an attribute? It is a particular property, which describes the entity. What is a Relation Schema and a Relation? A relation Schema denoted by R(A1, A2, ..., An) is made up of the relation name R and the list of attributes Ai that it contains. A relation is defined as a set of tuples. Let r be the relation which contains set tuples (t1, t2, t3, ..., tn). Each tuple is an ordered list of n-values t=(v1,v2, ..., vn).
  • 4. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com What is degree of a Relation? It is the number of attribute of its relation schema. What is Relationship? It is an association among two or more entities. Explain one-to-many relationship with an example? One-to-many relationships can be implemented by splitting the data into two tables with a primary key and foreign key relationship. Here the row in one table is referenced by one or more rows in the other table. An example is the Employees and Departments table, where the row in the Departments table is referenced by one or more rows in the Employees table. Explain many-to-many relationship with an example? Many-to-Many relationship is created between two tables by creating a junction table with the key from both the tables forming the composite primary key of the junction table. An example is Students, Subjects and Stud_Sub_junc tables. A student can opt for one or more subjects in a year. Similarly a subject can be opted by one or more students. So a junction table is created to implement the many-to-many relationship. What is Relational Algebra? It is procedural query language. It consists of a set of operations that take one or two relations as input and produce a new relation. What is primary key, foreign key and unique key?  Primary key: The primary key is used to uniquely identify each row in a table and does not allow null values.  Foreign key: A foreign key is one or more columns whose values are derived from the primary key values of same or different table.  Unique Key: Unique key identifies a each row in the table uniquely. Unique key allows null values. What is meant by query optimization? The phase that identifies an efficient execution plan for evaluating a query that has the least estimated cost is referred to as query optimization.
  • 5. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com Normalization What is normalization? It is the process of efficiently organizing the data. It is the decomposition of a table into several tables to remove the undesirable properties. For that the relation schema is analyzed their Functional Dependencies (FDs) and primary key. So it is a systematic method to decompose a table. The design must be dependency preserving, content preserving (lossless join) and free from interrelation join constraints. We need to have a formal algorithmic approach to find if there are problems in a proposed table design. If the table structure is not proper then there will be problems like redundancy, insertion, deletion and update anomalies. Database communities have developed a series of guidelines to ensure that the databases are normalized. These are referred to as normal forms numbered from one to six. What is 1 NF (Normal Form)? The domain of attribute must include only atomic (simple, indivisible) values. What is 2NF? A relation schema R is in 2NF if it is in 1NF and every non-prime attribute A in R is fully functionally dependent on primary key. What is 3NF? A relation schema R is in 3NF if it is in 2NF and if every non prime attribute is non-transitively dependent on primary key. What is BCNF (Boyce-Codd Normal Form)? A relation schema R is in BCNF if it is in 3NF and for every FD X  A, X must be a candidate key. What is Functional Dependency? For any two tuples t1 and t2 in relation ,r, if t1[X] = t2[X] then they have t1[Y] = t2[Y]. This means the value of X component of a tuple uniquely determines the value of component Y. Eg. RollNo determines Name or Name is dependent on RollNo
  • 6. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com What is Fully Functional dependency? It is based on concept of full functional dependency. A functional dependency X  Y is full functional dependency if removal of any attribute A from X means that the dependency does not hold any more. What is De-normalization? De-normalization is the process of optimizing the read performance of a database by adding redundant data or by grouping data. De-normalization is used in OLAP systems. Transactions What is a Transaction? A transaction is a logical unit of work performed against a database in which all steps must be performed or none. What are ACID properties? A database transaction must be Atomic, Consistent, Isolation and Durability.  Atomic: Transactions must be atomic. Transactions must fail or succeed as a single unit.  Consistent: The database must always be in consistent state. There should not be any partial transactions  Isolation: The changes made by a user should be visible only to that user until the transaction is committed.  Durability: Once a transaction is committed, it should be permanent and cannot be undone. SQL Write down the general syntax of a select statement? The basic syntax of a select statement is SELECT Columns | * FROM Table_Name [WHERE Search_Condition] [GROUP BY Group_By_Expression] [HAVING Search_Condition] [ORDER BY Order_By_Expression [ASC|DESC]] What are the differences between Truncate and Delete?
  • 7. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com Both truncate and delete are used to delete data from a table. The following are the differences between the truncate and delete statements.  Truncate is a DDL statement. Delete is a DML statement  Truncate does not generate rollback segments. Whereas Delete does.  In case of delete, rollback recovers data before issuing a commit. In case of truncate, you cannot recover data.  Truncate does not fire any delete triggers created on the table. Whereas the delete does. What are the differences between Having and Where clause?  The Where clause filters rows before grouping. Having clause filters rows after grouping.  You cannot use aggregate functions in Where clause. In Having clause, you can use aggregate functions. How to delete duplicate records in a table? DELETE FROM <table name> WHERE rowid in (SELECT MAX(rowid) FROM <table name> Group by <primary key columns>; What are the differences between UNION and UNION ALL.  Union and union all are used to merge rows from two or more tables.  Union set operator removes duplicate records. Whereas union all does not.  Union operator sorts the data in ascending order. union all does not.  Union all is faster than union operator. What is ROWID and rownum in oracle? The ROWID is a unique database-wide physical address for every row on every table. Once assigned (when the row is first inserted into the database), it never changes until the row is deleted or the table is dropped. The ROWID consists of the following three components, the combination of which uniquely identifies the physical storage location of the row.  Oracle database file number, which contains the block with the rows  Oracle block address, which contains the row  The row within the block (because each block can hold many rows) The ROWID is used internally in indexes as a quick means of retrieving rows with a particular key value. Application developers also use it in SQL statements as a quick way to access a row once they know the ROWID Rownum is temporary serial number allocated to each returned row during query execution. Rownum is a pseudo column which is generated for query data set at runtime.
  • 8. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com The difference between ROWNUM and ROWID is that ROWNUM is temporary while ROWID is permanent. Another difference is that ROWID can be used to fetch a row, while ROWNUM only has meaning within the context of a single SQL statement, a way of referencing rows within a fetched result set. 16. What are different Types of Join? 1. Cross Join A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. The common example is when company wants to combine each product with a pricing table to analyze each product at each price. 2. Inner Join A join that displays only the rows that have a match in both joined tables is known as inner Join. This is the default type of join in the Query and View Designer. 3. Outer Join A join that includes rows even if they do not have related rows in the joined table is an Outer Join. You can create three different outer join to specify the unmatched rows to be included: 1. Left Outer Join: In Left Outer Join all rows in the first-named table i.e. "left" table, which appears leftmost in the JOIN clause are included. Unmatched rows in the right table do not appear. 2. Right Outer Join: In Right Outer Join all rows in the second-named table i.e. "right" table, which appears rightmost in the JOIN clause are included. Unmatched rows in the left table are not included. 3. Full Outer Join: In Full Outer Join all rows in all joined tables are included, whether they are matched or not. 4. Self Join This is a particular case when one table joins to itself, with one or two aliases to avoid confusion. A self join can be of any type, as long as the joined tables are the same. A self join is rather unique in that it involves a relationship with only one table. The common example is when company has a hierarchal reporting structure whereby one member of staff reports to another. Self Join can be Outer Join or Inner Join. PL/SQL 12. What is Cursor? Cursor is a database object used by applications to manipulate data in a set on a row-by- row basis, instead of the typical SQL commands that operate on all the rows in the set at one time. In order to work with a cursor we need to perform some steps in the following order: 1. Declare cursor 2. Open cursor 3. Fetch row from the cursor 4. Process fetched row 5. Close cursor
  • 9. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com 6. Deallocate cursor What is a Trigger? 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, and 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. What is De-normalization? As the name indicates, de-normalization is the reverse process of normalization. It’s the controlled introduction of redundancy in to the database design. It helps improve the query performance as the number of joins could be reduced. What is a Stored Procedure? Stored Procedure is a group of sql statements that has been created once and stored in server database. Stored procedures will accept input parameters so that single stored procedure can be used over network by multiple clients using different input data. Stored procedures will reduce network traffic and increase the performance. Advantages a) Stored procedure allows modular programming. You can create the procedure once, store it in the database, and call it any number of times in your program. b) Stored Procedure allows faster execution. If the operation requires a large amount of SQL code is performed repetitively, stored procedures can be faster. They are parsed and optimized when they are first executed, and a compiled version of the stored procedure remains in memory cache for later use. This means the stored procedure does not need to be reparsed and reoptimized with each use resulting in much faster execution times. c) Stored Procedure can reduce network traffic. An operation requiring hundreds of lines of Transact-SQL code can be performed through a single statement that executes the code in a procedure, rather than by sending hundreds of lines of code over the network. d) Stored procedures provide better security to your data
  • 10. http://itntechnicalinterview.blogspot.com http://itntechnicalinterview.blogspot.com Users can be granted permission to execute a stored procedure even if they do not have permission to execute the procedure's statements directly.