SQL Interview Questions
==> What is the primary key?
Answer :It is the uniquely identify each record in the table.
==> What is Foreign key?
Answer : Foreign key is key which the reference of the primary key of another table .
==>What is Table ?
Answer: It is the collection of the data in an organized manner in form of rows and columns.
==>What is Field?
Answer: A field refers to the number of columns in the table.
==> What is DELETE command ?
Answer : Delete command is used to delete row in table . Example : DELETE FROM column_name WHERE condition
==> What is Truncate command?
Answer: Truncate command is used to delete all rows from the table.
==>What is DROP command ?
Answer: It is used to delete the table in the database. Example : DROP TABLE table_name
==>What is DDL ?
Answer: DDL is stands for Data Definition Language and it Consists of the command that can be used to define the database.
==> What is DML ?
Answer : DML is stands for Data Manipulating Language and Consists of the command that can be used to Manipulation of data in present in database.
==> What is DCL ?
Answer : DCL stands for Data Control Language and Command which deals with the right, Control of the database system.
==> What is Constrains ?
Answer: It is the used to specify the limit of the data type of the table. It can be specified while creating or altering the table statement.
Ex: NOT NULL,UNIQUE,CHECK,DEFAULT, INDEX.
==>Difference between the CHAR and VARCHAR ?
Answer : -> CHAR has the fixed length and the length size of CHAR is 10.
→ VARCHAR has no fixed length and the length size of VARCHAR is 10,20 or so many. Example : VARCHAR(50)
==> What is join in SQL ?
Answer : Joins clauses is used to combined the rows from two or more tables, based on the related column between them .
==> Types of join ?
Answer : (INNER) JOIN
: Returns records that have matching values in both tables
LEFT (OUTER) JOIN
: Returns all records from the left table, and the matched records from the right tableRIGHT (OUTER) JOIN
: Returns all records from the right table, and the matched records from the left tableFULL (OUTER) JOIN
: Returns all records when there is a match in either left or right table
== >What is Unique Key?
Answer: Unique identify a single row in the table. No duplicate values allowed.
==>What is the Data Integrity?
Answer: It defines accuracy of data consistency of data Integrity constrains to enforce business rules on data.
==> What is Clustered index and non-clustered index in SQL?
Answer : Clustered index: Clustered index is used for easy retrieval of data from the database and it is faster. One table have only one clustered index .
Non-clustered index : Non-Clustered index is used for easy retrieval of data from the database and it is slower. One table have many non-clustered index .
==> Write SQL query for display to current date?
Answer : SELECT GETDATE();
== >What do you mean by Denormalization ?
Answer : Increase the performance of entire infrastructure as it introduce redundancy into the table. Adds the redundant data into a table and combine data from various table into a single table.
==>What is the entity ?
Answer: An object or any a person, place or thing in the real world about which data can be stored in database.
==> What is relationship ?
Answer: Relations or links between the entities that have something to do with each other.
==>What is index in SQL.
Answer: Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.
Example : CREATE INDEX index_name ON column_name.
==> Explain different types of index in SQL ?
Answer: 1) Clustered Index : Clustered indexes sort and store rows data in a table or view based on their fundamental values.
2) Unique Index: The unique index in SQL guarantees and confirms that the index key does not contain any duplicate values.
3)Non-Clustered Index: Non-clustered index poses a structure, which is separated from data rows.
==>What is the normalization and advantages of it ?
Answer: Normalization is the process to organizing data to avoid duplication redundancy.
Advantages: → Better data organization.
→ Efficiency data access and allow easy to modification.
==> Difference types of normalization ?
Answer : 1 NF : Each table cell should have a single value. so basically all the records must be unique.
2 NF : Database should be 1 NF and should also have single column primary key.
3 NF: Database should be 2 NF and must not have transitive functional dependencies.
BCNF: It has to be in 3rd normal form and every function dependency A → B.
==> What is ACID property in a database ?
Answer: Atomicity: Execute transition either full transition or none transition ,
Consistency: Measurement transition of correctness,
Isolation: It transition which should not transitions of another transitions,
Durability: Update the transition value permanent.
==> What do you mean by triggered ?
Answer : It defines to execute the automatically in place or after the modifications.
==> What are different types of operator in SQL?
Answer : 1) Arithmetic Operator
2) Bitwise operator
3) Comparison operator
4) Compound operator
5) Logical operator
==> What is cross join and natural join ?
Answer : Cross join : produces the cross product or Cartesian product of two value.
Natural join : Based on all the column having the same name and data types in both the tables.
==> What is subquery in SQL ?
Answer : A subquery is a query inside the another query where a query is defined to retrieve data or information back from the database.
Example : SELECT column_name FROM table_name WHERE column_name IN(SELECT column_name FROM table_name WHERE condition).
==> Can you list the ways to get the count of records in a table?
Answer: To count the number of records in a table.
Example : SELECT COUNT(column_name or *) FROM table_name;
==> Write a query to find the names of employees that begin with the “A”?
Answer: SELECT * FROM employee WHERE Empname LIKE ‘A%’;
==> Write the SQL query to get the third highest salary of an employee from employee table ?
Answer : SELECT TOP 1 salary FROM(SELECT TOP 3 salary FROM employee_table ORDER BY salary DESC) AS emp ORDER BY salary DESC.
==> What are the different types of Relationship ?
Answer : 1) Many to many relationship
2) One to One Relationship
3) One to Many Relationship
4) Many to One Relationship.
==> What is the main difference between “BETWEEN’ and “IN”?
Answer : BETWEEN : Used to display rows based on range of values in a row.
Example: SELECT * FROM Students WHERE ROLL_NUM BETWEEN 15 AND 50.
IN : used to check for values in a specific set of values.
Example : SELECT * FROM students WHERE ROLL_NUM IN (8,15,25).
==> What is MERGE in SQL ?
Answer: it allows conditional update or insertion of data into the table. It performs an update if a row exists, or an insert if the row does not exists.
==> What is the recursive stored procedure ?
Answer : Recursive stored procedure refers to a stored procedure which calls by itself until it reaches the some boundary conditions.
==>What is clause in SQL?
Answer : SQL clause helps to limit the result set by providing the condition to the query. Example : HAVING and WHERE clause.
==>What is the difference between “HAVING” and “WHERE” clause ?
Answer: HAVING : can be used only with SELECT statement. It is usually used in GROUP BY statement.
Example : SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
WHERE : It is used to extract only those records that fulfill a specified condition.
Example : SELECT column1, column2, … FROM table_name WHERE condition;
==> How can you fetch common records from two tables?
Answer :SELECT column_name FROM table_name INTERSECT SELECT column_name FROM table_name.
==> What is an ALIAS command ?
Answer: Alias are used to rename a table name or column name.
Example : SELECT column_name FROM table_name AS alias_name;
==> What is aggregations ?
Answer: Used to evaluate the mathematical calculations and returns a single values. Example : MAX(), COUNT().
==> How to fetch the alternate records from a table ?
Answer: To display odd number →
SELECT StudentID FROM(SELECT rowno, StudentId FROM Student) WHERE mod(rowno,2)=0;
To display even number →
SELECT StudentID FROM(SELECT rowno, StudentId FROM Student) WHERE mod(rowno,2)=1;
==> How can you select unique records from a table ?
Answer : SELECT DISTINCT emply_id FROM employee;
==> How can use first 5 character of the string?
Answer: SELECT SUBSTRING(Studentname,1,5) as Studentname FROM Student
==> What is the advantages and disadvantages of Stored Procedure ?
Answer: Advantages: It can be used as a modular programming which means create once, store and call for several times whenever it is required.
Disadvantages: It can be executed only in database.
==>What are the local and global variable?
Answer: Local Variable : It can be used and exist only inside the function
Global variable : It can used throughout the function. It can not be created whenever that function is called.