Tuesday 30 May 2017

SQL Interview Questions and Answers


There is given sql interview questions and answers that has been asked in many companies. For PL/SQL interview questions, visit our next page.

1) What is SQL?

SQL stands for structured query language. It is a database language used for database creation, deletion, fetching rows and modifying rows etc. sometimes it is pronounced as se-qwell.

2) When SQL appeared?

It appeared in 1974.

3) What are the usages of SQL?

o    To execute queries against a database
o    To retrieve data from a database
o    To inserts records in a database
o    To updates records in a database
o    To delete records from a database
o    To create new databases
o    To create new tables in a database
o    To create views in a database

4) Does SQL support programming?

No, SQL doesn't have loop or Conditional statement. It is used like commanding language to access databases.

5) What are the subsets of SQL?

1.    Data definition language (DDL)
2.    Data manipulation language (DML)
3.    Data control language (DCL)

6) What is data definition language?

Data definition language(DDL) allows you to CREATE, ALTER and DELETE database objects such as schema, tables, view, sequence etc.

7) What is data manipulation language?

Data manipulation language makes user able to access and manipulate data. It is used to perform following operations.
  • Insert data into database
  • Retrieve data from the database
  • Update data in the database
  • Delete data from the database

8) What is data control language?

Data control language allows you to control access to the database. It includes two commands GRANT and REVOKE.
GRANT: to grant specific user to perform specific task.
REVOKE: to cancel previously denied or granted permissions.

9) What are tables and fields in database?

A table is a set of organized data. It has columns and rows. Columns can be categorized as vertical, and Rows are horizontal.
A table contains specified number of column called fields but can have any number of rows which is known as record.

10) What is a primary key?

A primary key is a combination of fields which uniquely specify a row. This is a special kind of unique key. Primary key values cannot be NULL.

11) What is a foreign key?

A foreign key is specified as a key which is related to the primary key of another table. Relationship needs to be created between two tables by referencing foreign key with the primary key of another table.

12) What is a unique key?

A Unique key constraint uniquely identifies each record in the database. This provides uniqueness for the column or set of columns.

13) What are the type of operators available in SQL?

  1. Arithmetic operators
  2. Logical operators
  3. Comparison operator

14) What is view in SQL?

A view is a virtual table which contains a subset of data within a table. Views are not virtually present, and it takes less space to store. View can have data of one or more tables combined, and it is depending on the relationship.

15) What is an Index in SQL?

Index is used to increase the performance and allow faster retrieval of records from the table. An index creates an entry for each value and it will be faster to retrieve data.

16) Which are the different types of indexes in SQL?

There are three types of Indexes in SQL:
  • Unique Index
  • Clustered Index
  • NonClustered Index

17) What is Unique Index?

Unique Index:
This indexing does not allow the field to have duplicate values if the column is unique indexed. Unique index can be applied automatically when primary key is defined.

18) What is Clustered Index in SQl?

Clustered Index:
The clustered index is used to reorder the physical order of the table and search based on the key values. Each table can have only one clustered index.

19) What is NonClustered Index in SQL?

NonClustered Index:
NonClustered Index does not alter the physical order of the table and maintains logical order of data. Each table can have 999 non-clustered indexes.

20) What is the difference between SQL, MySQL and SQL Server?

SQL or Structured Query Language is a language which is used to communicate with a relational database. It provides a way to manipulate and create databases. On the other hand, MySQL and Microsoft's SQL Server both are relational database management systems that use SQL as their standard relational database language.

21) What is the difference between SQL and PL/SQL?

SQL or Structured Query Language is a language which is used to communicate with a relational database. It provides a way to manipulate and create databases. On the other hand, PL/SQL is a dialect of SQL which is used to enhance the capabilities of SQL. It was developed by Oracle Corporation in the early 90's. It adds procedural features of programming languages in SQL.

22) Is it possible to sort a column using a column alias?

Yes. You can use column alias in the ORDER BY clause for sorting.

23) What is the difference between clustered and non clustered index in SQL?

There are mainly two type of indexes in SQL, Clustered index and non clustered index. The differences between these two indexes is very important from SQL performance perspective.
  1. One table can have only one clustered index but it can have many non clustered index.(approximately 250).
  2. clustered index determines how data is stored physically in table. Actually clustered index stores data in cluster, related data is stored together so it makes simple to retrieve data.
  3. reading from a clustered index is much faster than reading from non clustered index from the same table.
  4. clustered index sort and store data rows in the table or view based on their key value, while non cluster have a structure separate from the data row.

24) What is the SQL query to display current date?

There is a built in function in SQL called GetDate() which is used to return current timestamp.

25) Which are the most commonly used SQL joins?

Most commonly used SQL joins are INNER JOIN and (left/right) OUTER JOIN.

26) What are the different types of joins in SQL?

Joins are used to merge two tables or retrieve data from tables. It depends on the relationship between tables.
Following are the most commonly used joins in SQL:
  • Inner Join
  • Right Join
  • Left Join
  • Full Join

27) What is Inner Join in SQL?

Inner join:
Inner join returns rows when there is at least one match of rows between the tables.

28) What is Right Join in SQL?

Right Join:
Right join is used to retrieve rows which are common between the tables and all rows of Right hand side table. It returns all the rows from the right hand side table even though there are no matches in the left hand side table.

29) What is Left Join in SQL?

Left Join:
Left join is used to retrieve rows which are common between the tables and all rows of Left hand side table. It returns all the rows from Left hand side table even though there are no matches in the Right hand side table.

30) What is Full Join in SQL?

Full Join:
Full join return rows when there are matching rows in any one of the tables. This means, it returns all the rows from the left hand side table and all the rows from the right hand side table.

31) What is "TRIGGER" in SQL?

Trigger allows you to execute a batch of SQL code when an insert, update or delete command is executed against a specific table.
Actually triggers are special type of stored procedures that are defined to execute automatically in place or after data modifications.

32) What is self join and what is the requirement of self join?

Self join is often very useful to convert a hierarchical structure to a flat structure. It is used to join a table to itself as like if that is the second table.

33) What are set operators in SQL?

Union, Intersect or Minus operators are called set operators.

34) What is the difference between BETWEEN and IN condition operators?

The BETWEEN operator is used to display rows based on a range of values. The IN condition operator is used to check for values contained in a specific set of values.

35) What is a constraint? Tell me about its various levels.

Constraints are representators of a column to enforce data entity and consistency. There are two levels :
  1. column level constraint
  2. table level constraint

36) Write an SQL query to find names of employee start with 'A'?

1.    SELECT * FROM Employees WHERE EmpName like 'A%'  

37) Write an SQL query to get third maximum salary of an employee from a table named employee_table.

1.    SELECT TOP 1 salary   
2.    FROM (  
3.    SELECT TOP 3 salary  
4.    FROM employee_table  
5.    ORDER BY salary DESC ) AS emp  
6.    ORDER BY salary ASC;      

38) What is the difference between DELETE and TRUNCATE statement in SQL?

The main differences between SQL DELETE and TRUNCATE statements are given below:
No.
DELETE
TRUNCATE
1)
DELETE is a DML command.
TRUNCATE is a DDL command.
2)
We can use WHERE clause in DELETE command.
We cannot use WHERE clause with TRUNCATE
3)
DELETE statement is used to delete a row from a table
TRUNCATE statement is used to remove all the rows from a table.
4)
DELETE is slower than TRUNCATE statement.
TRUNCATE statement is faster than DELETE statement.
5)
You can rollback data after using DELETE statement.
It is not possible to rollback after using TRUNCATE statement.

39) What is ACID property in database?

ACID property is used to ensure that the data transactions are processed reliably in a database system.
A single logical operation of a data is called transaction.
ACID is an acronym for Atomicity, Consistency, Isolation, Durability.
Atomicity: it requires that each transaction is all or nothing. It means if one part of the transaction fails, the entire transaction fails and the database state is left unchanged.
Consistency: the consistency property ensure that the data must meet all validation rules. In simple words you can say that your transaction never leaves your database without completing its state.
Isolation: this property ensure that the concurrent property of execution should not be met. The main goal of providing isolation is concurrency control.
Durability: durability simply means that once a transaction has been committed, it will remain so, come what may even power loss, crashes or errors.

40) What is the difference among NULL value, zero and blank space?

Ans: A NULL value is not same as zero or a blank space. A NULL value is a value which is 'unavailable, unassigned, unknown or not applicable'. On the other hand, zero is a number and blank space is treated as a character.

41) What is the usage of SQL functions?

SQL functions are used for following purpose:
  • To perform calculations on data.
  • To modify individual data items.
  • To manipulate the output.
  • To format dates and numbers.
  • To convert data types.

42) Which are the different case manipulation functions in SQL?

There are three case manipulation functions in SQL:
  • LOWER
  • UPPER
  • INITCAP

43) What is the usage of NVL function?

The NVL function is used to convert NULL value to a actual value.

44) Which function is used to return remainder in a division operator in SQL?

The MOD function returns the remainder in a division operation.

45) What is the syntax and use of the COALESCE function?

The syntax of COALESCE function:
1.    COALESCE(exp1, exp2, ... expn)  
The COALESCE function is used to return the first non-null expression given in the parameter list.

46) What is the usage of DISTINCT keyword?

The DISTINCT keyword is used to ensure that the fetched value is only a non-duplicate value.


SQL Interview Questions and Answers

There is given sql interview questions and answers that has been asked in many companies. For PL/SQL interview questions, visit our next...