0% found this document useful (0 votes)
17 views

SQL Interview Questions & Answers

Uploaded by

mmway007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views

SQL Interview Questions & Answers

Uploaded by

mmway007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
SOL INTERVIEW QUESTIONS & ANSWERS SQLINTERVIEW QUESTIONS AND ANSWERS MOST FREQUENTLY ASKED SOL INTERVIEW QUESTIONS 1. What is the difference between "Stored Procedure” and "Punction"? . Aprocedure can have both input and output parameters, but a function can only have input parameters, Inside a procedure we can use DML (INSERT/UPDATE/DELETE) statements, But inside a function we can't use DML statements. . We can't utilize a Stored Procedure in a Select statement. But we can use a fanction in aSelect statement. 1. We can usea Try-Catch Block in a Stored Procedure but inside a function we can't use a Try-Catch block. , We can use transaction management in a procedure but we can't ina function, ‘We can't join a Stored Procedure but we can join functions. 1. Stored Procedures cannot be used in the SQL statements anywhere In the WHERE/HAVING/SELECT section. But we can use a function anywhere. ‘Apracedure can return 0 or n values (max 1024). But a function can return only 1 value that is mandatory. Aprocedure can't be called from a function but we can calla function from a procedure. 2.What is difference between “Clustered Index” and “Non Clustered Index"? 1. AClustered Index physically stores the data of the table in the order of the keys values and the datais resorted every time whenever a new value is inserted ora value is updated in the column on which itis defined, whereas. non-clustered index creates a separate list of key values (or creates a table of pointers) that points towards the location of the data in the data pages. . Clustered Index requires no separate storage than the table storage. It forces the rows to be stored sorted on the index key whereas a non-clustered index requires separate storage than the table storage te store the index information. ‘Atable with a Clustered Index ts called a Clustered Table, Its rows are stored in a B- Tree structure sorted whereas a table without any clustered indexes is called a non- clustered table. Its rows are stored in a heap structure unsorted. . The default index is created as part of the primary key column as a Clustered Index. Ina Clustered Index, the leaf node contains the actual data whereas in anon- clustered index, the leaf node cot pointer ta the data rows of the table. AChustered Index always has an Index Id of 1 whereas non-clustered indexes have Index Ids > 1. 7. A Table can have only 1 Clustered Index whereas prior to SQL Server 2008 only 249 noneclustered indexes can be created. With SQL Server 2008 and above 999 non- ‘clustered indexes can be created. A Primary Key constraint creates a Clustered Index by default whereas A Unique Key constraint creates a non-clustered index by default. SOL INTERVIEW QUESTIONS AND ANSW" 3.What is the difference between the “DELETE” and “TRUNCATE” commands? . The DELETE command is used to remove rows froma table based on a WHERE condition whereas TRUNCATE removes all rows from a table, . So we can use a where clause with DELETE to filter and delete specific records whereas we cannot use a Where clause with TRUNCATE. . DELETEis executed using a row lock, each row in the table is locked for deletion whereas TRUNCATE is executed using a table lock and the entire table is locked for removal of all records, . DELETE is a DML. command whereas TRUNCATE is a DDL.command. DELETE retains the identity of the column value whereas in TRUNCATE, the Identify column is reset to its seed value if the table contains any identity column, To.use Delete you need DELETE permission on the table whereas to use Truncate on atable youneedat least ALTER permission on the table, . DELETE uses more transaction space than the TRUNCATE statement whereas: ‘Truncate uses less transaction space than DELETE statem . DELETE can be used with indexed views whereas TRUNCATE cannot be used with indexed views. The DELETE statement removes rows one at atime and records an entry in the transaction log for each deleted row whereas TRUNCATE TABLE removes the data bby deallocating the data pages used to store the table data and records only the page deallocations in the transacthon log. 10, Delete activates a trigger because the operation is logged individually whereas ‘TRUNCATE TABLE can't activate a trigger because the operation does not log individual row deletions. 4.What is the difference between the “WHERE” chiuse and the “HAVING” clause? |. WHERE clause can be used with a Select, Update and Delete Statement Clause but the HAVING clause can be used only with a Select statement. . We can't use an aggregate functions in the WHERE clause unless itis in a sub-query contained in a HAVING clause whereas we can use an aggregate function inthe HAVING clause. We can use a column name inthe HAVING clause but the column must be contained in the group by cause. WHERE is. used before the GROUP BY clause whereas a HAVING clause is used to impose a condition on the GROUP Function and is used after the GROUP BY clause in the query. |. AWHERE clause applies to each and every row whereasa HAVING clause applies to summarized rows (summarized with GROUP BY), . Inthe WHERE clause the data that is fetched from memory depending ona condition whereas in HAVING the completed data is first fetched and then separated depending an the candition. SQL INTERVIEW QUESTIONS AND ANSWERS 5.What is the difference between “Primary Key” and “Unique Key? 1. We can have only one Primary Key in a table whereas we can have more than one Unique Key ina table. 2. The Primary Key cannot have a NULL value whereas a Unique Key may have enly ‘one null value, 3. By default, a Primary Key is a Clustered Index whereas by default, a Unique Key isa ‘unique non-clustered index. 4. A Primary Key supports an Auto Increment value whereas a Unique Key doesn't Support an Aute Increment value. 6. What is the difference between a "Local Temporary Table” and "Global Temporary Table"? 1. ALocal Temporary Table is created by giving ita prefix of f whereas a Global Temporary Table is created by giving ita prefix of FM. 2, ALocal Temporary Table cannot be shared among multiple users whereas a Global ‘Temporary Table can be shared among multiple users, 3. A Local Temporary Table is only available to the current DB connection for the current user and are cleared when the connection is closed whereas a Global ‘Temporary Table is available to any conection once created. They are cleared when the last connection is closed. ‘7.What are super, primary, candidate and foreign keys? Ans: A super key Is.a set af attributes ofa relation schema upon which all attributes of the schema are functionally dependent. No two rows can have the same value of super key attributes. A Candidate key is minimal super key, Le, no proper subset of Candidate key attributes can bea super key. ‘A Primary Rey is one of the candidate keys. One of the candidate keysis selected as most Imporvinc and Becomes the primey ly. There cannot be mire that one pray hey table. Foreign key isa field (or collection of fields) in one table that uniquely identifies a row of another table. B.What is the difference between primary key and unique constraints? ‘Ans: Primary key cannot have NULL value, the unique constraints can have NULL values, There is only one primary key in a table, but there can be multiple unique constrains. 4[Page SQL INTERVIEW QUESTIONS AND ANSWERS 9.What is database normalization? Ans: It isa process of analyzing the given relation schemas based on their functional dependencies and primary keys to achieve the following desirable properties: L)Minimizing Redundancy 2) Minimizing the Insertion, Deletion, And Update Anomalies Relation schemas that do not meet the properties are decomposed into smaller relation schemas that could meet desirable properties, 10.What is SQL? SQL is Structured Query Language designed for inserting and modifying in a relational database management system, 11.What are the differences between DDL, DML and DCL in SQL? Ans: Following are some details of three. DDL stands for Data Definition Language. SQL queries like CREATE, ALTER, DROP and RENAME come under this, DML stands for Data Manipulation Langtage. SQL queries like SELECT, INSERT and UPDATE:come under this ‘DCL stands for Data Control Language. SQL queries like GRANT and REVOKE come under this, 12.What is the difference between having and where clause? Ans: HAVING is used to specify a condition for a group or an aggregate function used in select statement. The WHERE clause selects before grouping. The HAVING clause selects rows alter grouping, Unlike HAVING clause, the WHERE clause cannot contain aggregate functions. 13.What is Join? ‘Ans: An SQL Join is used to combine data from two or more tables, based on a common fietd between them. For example, consider the following two tables, Student Table EnroliNo StudentName Address 1000 geek gecksquizi 1001 geek? gecksquiz? 1002 geek3 geeksquiz3 StudentCourse Table SQL INTERVIEW QUESTIONS AND ANSWERS CourselD EnroliNo 1000 1000 1000 1002 1003 Following is join query that shows names of students enrolled in SELECT StudentCourse.CourseID, Student.StudentName FROM StucentCourse INNER JOIN Customers: ON StudentCourse-EnrollNo = Student-EnroliNe ORDER BY StucentCourse.CourselD; ‘The above query would produce following result, CourselD StudentName geek geek geek geeks geek Bae ee ‘14.What is Identity? ‘Ans: Identity (or AutoNumber) is a column that automatically generates numeric values. A, start and increment value can be set, but mast DBA leave these at 1. A GUID colunmn also generates numbers; the value of this cannot be cantrolled. Identity/GUID columns do not need to be indexed 15.What is a view in SQL? How to create one ‘Ans: A view is a virtual table based on the result-set ofan SQL statement, We can create using create view syntax, CREATE VIEW view_name AS SELECT column_nanie(s) FROM table_name WHERE condition 16.What are the uses of view? 1, Views can represent a subset of the data contained in a table: consequently, a view can limit the degree of exposure of the underlying tables to the outer world: a given user may have permission ta query the view, while denied access to the rest af the base table, 6|Page SQLINTERVIEW QUESTIONS AND ANSWERS 2. Views can joliy and simplify multiple tables into a single virtual table 3. Views can act as aggregated tables, where the database engine aggregates data (sum, average etc.) and presents the calculated resultsas part of the data 4, Views can hide the complexity of data; for example a view could appear as Sales2000 or Sales2001, transparently partitioning the actual underlying table. 5, Depending on the SQL engine used, views can provide extra security. 17.What isa Trigger? ‘Ans: A Trigger isa corle that associated with insert, update or delete operations, The code isexecuted automatically whenever the associated query is executed an a table. Triggers can be useful to maintain integrity in database. ‘18.What is a stored procedure? Ans: A stored procedure is like a funetion that contains a set of operations compiled together. It contains a set of operations that are commonly used in.an application to do some common database tasks. 19.What is the difference between Trigger and Stored Procedure? ‘Ans: Unlike Stored Procedures, Triggers cannot be called directly. They can only be associated with queries. 20,What is a transaction? What are ACID properties? Ans: A Database Transaction isa set of database operations that must be treated as whole, means either all operations are executed or none of th An example can be bank transaction from one account to anether account. Either both debit and credit operations must be executed or none of them. ACID (Atomicity, Consistency, Isolation, Durability) isa set of properties that guarantee that database transactions are processed reliably. 24.What are indexes? Ans: A database index is a data structure that impraves the speed of data retrieval operations on a database table at the cost of additional writes and the use of more storage space fo maintain the extra copy of dava, Data can be stored only in one order on disk. To support faster access according ta different values, faster search like binary search for different values Is desired, For this purpose, indexes are created on tables. These indexes need extra space on disk, but they allow faster search accarding to different frequently searched values. 22.What are clustered and non-clustered Indexes? ‘Ans: Clustered indexes is the index according ta which data Is physically stared on disk. Therefore, only one clustered index can be created ona given database table, Non-clustered indexes don't define physical erdering of data, but logical ordering Typically, a tree is created whose leaf point to disk records. B-Tree or B+ tree are used for this purpase. 7Pane SQL RVIEW QUESTIONS AND ANSWERS 23.What are Primary Keys and Foreign Keys? Ans: Primary keys are the unique identifiers for each row. They must contain unique values and cannot be null. Due to their importance in relational databases, Primary keys are the most fundamental aspect of all keys and constraints. A table can have only one primary key. Foreign keys are a method of ensuring data integrity and manifestation of the relationship between tables, 24.What is SQL? ‘Ans: Structured Query Language(SQL) is a language designed specifically for communicating with databases. SQL fs an ANSI (American National Standards Institute) standard . 25. What are the different type of SQL or different commands in SQL? ‘Ans: Frequently asked SQL Interview Questions 1.DDL.~ Data Definition Language.DDL is used to define the structure that holds the data. 2.DML- Data Manipulation Language DML is used for manipulation of the data itself. Typical operations are Insert, Delete, Update and retrieving the data from the table 3.DCL~DataControlLanguage DCL is used to control the visibility of data like granting database access and set privileges tocreate table etc, 4.TCL-TransactionControl Language It contains 26, What are the Advantages of SQL? 1. SQL is not a proprietary language used by specific database vendors. Almost every major DBMS supports SQL. so learning this one language will enable programmer to interact with any database like ORACLE, SQL MYSQL ete. 2. $QLis easy to learn. The statements are alll made up of descriptive English words, and there aren't that many of them. 3. SQL is actually avery powerful language and by using its language elements yeu can perform very complex and sophisticated database operations. 8IPace SQLINTERVIEW QUESTIONS AND ANSWERS 27. what is a field ina database ? A field isan area within a record reserved for a specific piece of data. Examples: Employee Name , Employee ID etc 28,What is a Record in a database ? A record is the collection of values / fields of a specific entity:i.e.a Employee , Salary ete. 29, What is a Table in a database ? A table is a collection of records of a specific type. For example, employee table , salary table ete, 30,What is a database transaction? Database transaction take database from ane consistent state to another. At the end of the transaction the system must be in the prior state if transaction fails or the status of the system should reflect the successful completion if the transaction goes through. ‘31.What are properties of a transaction? Properties of the transaction can be summarized as ACID Properties. 1. Atomicity A transaction consists of many steps. When all the steps in a transaction gets completed, it will get reflected in DB or if any step fails, all the transactions are rolled back. 2. Consistency The database will move from one consistent state to another, if the transaction succeeds and remain in the original state, if the transaction fails. 3. Isolation Every transaction should operate as ifit is the only transaction in the system 4. Durability Once a transaction has completed successfully, the updated rows/records must be available for all other transactions on a permanent basis, 32.What is a Database Lock? Database lock tell a transaction, if the data item in questions is currently being used by other transactions, o|Pace SQ). INTERVIEW QUESTIONS AND ANSWERS 33. What are the type oflocks ? 1. Shared Lock When a shared lock is applied on data item, other transactions can only read the item, but can't write Into it. 2. Exclusive Lock When a exchisive lock is applied on data item, other transactions can't read or write into the data item. 34.What are the different type of normalization? Frequently asked SQL Interview Questions In database design, we start with one single table, with all pessible columns. Lot of redundant data would be present since it’s a single table. The pracess of removing the redundant data, by splitting up the table ina well defined fashion is called normalization. 1. First Normal Form (INF) A relation is said to be in first normal form if and only if all underlying domains contain atomic values only. After INF, we can still have redundant data 2. Second Normal Ferm (2NF) A relation is said to be in 2NF ifand only ifit is in INF and every non key attribute is fully dependent on the primary key. sfter 2NF, we can stil have redundant data 3. Third Normal Form (3NF) relation is said to be in 3NF, ifand only if itis in 2NF and every non key attribute isnon- transitively dependent on the primary key 35.What is a primary key? Frequently asked SQL Interview Questions A primary key isa colunm whose values uniquely identify every row in a table. Primary key values can never be reused, If a row is deleted from the table, its primary key may not beassigned to any new rows in the future, To define a field as primary key, following conditions had to be met : 1, No two rows can have the same primary key value, 2. Every row must have a primary key value 3. Primary key field cannot be nul 4. Values in primary key columns can never be modified or updated 1[Poge

You might also like