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.


Wednesday 3 May 2017

SQL Interview Questions

What is the difference between SQL and MySQL or SQL Server?
SQL or Structured Query Language is a language; language that communicates with a relational database thus providing ways of manipulating and creating databases. MySQL and Microsoft’s SQL Server both are relational database management systems that use SQL as their standard relational database language.

PL/SQL is a dialect of SQL that adds procedural features of programming languages in SQL. It was developed by Oracle Corporation in the early 90's to enhance the capabilities of SQL.
Following are various DDL or Data Definition Language commands in SQL −
  • CREATE − it creates a new table, a view of a table, or other object in database.
  • ALTER − it modifies an existing database object, such as a table.
  • DROP − it deletes an entire table, a view of a table or other object in the database.
Following are various DML or Data Manipulation Language commands in SQL −
  • SELECT − it retrieves certain records from one or more tables.
  • INSERT − it creates a record.
  • UPDATE − it modifies records.
  • DELETE − it deletes records.
Following are various DCL or Data Control Language commands in SQL −
  • GRANT − it gives a privilege to user.
  • REVOKE − it takes back privileges granted from user.
Yes. A column alias could be used in the ORDER BY clause.
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’. Whereas, zero is a number and blank space is a character.
True.

A query result displays all rows including the duplicate rows. To eliminate duplicate rows in the result, the DISTINCT keyword is used in the SELECT clause.

The BETWEEN operator displays rows based on a range of values. The IN condition operator checks for values contained in a specific set of values.

In such cases, the LIKE condition operator is used to select rows that match a character pattern. This is also called ‘wildcard’ search.

The default sorting order is ascending. It can be changed using the DESC keyword, after the column name in the ORDER BY clause.

SQL functions have the following uses −
  • Performing calculations on data
  • Modifying individual data items
  • Manipulating the output
  • Formatting dates and numbers
  • Converting data types
LOWER, UPPER, INITCAP

The MOD function returns the remainder in a division operation.

The NVL function converts a NULL value to an actual value.

The NVL(exp1, exp2) function converts the source expression (or value) exp1 to the target expression (or value) exp2, if exp1 contains NULL. The return value has the same data type as that of exp1.
The NVL2(exp1, exp2, exp3) function checks the first expression exp1, if it is not null then, the second expression exp2 is returned. If the first expression exp1 is null, then the third expression exp3 is returned.

The NULLIF function compares two expressions. If they are equal, the function returns null. If they are not equal, the first expression is returned.

The COALESCE function has the expression COALESCE(exp1, exp2, …. expn)
It returns the first non-null expression given in the parameter list.

There are two ways to implement conditional processing or IF-THEN-ELSE logic in a SQL statement.
  • Using CASE expression
  • Using the DECODE function
The result would be the Cartesian product of two tables with 20 x 10 = 200 rows.
The cross join produces the cross product or Cartesian product of two tables. The natural join is based on all the columns having same name and data types in both the tables.
Group functions in SQL work on sets of rows and returns one result per group. Examples of group functions are AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE.
By default, group functions consider all values including the duplicate values.
True.
True.
False. COUNT(*) returns the number of rows in a table.

It doesn’t have a GROUP BY clause. The subject_code should be in the GROUP BY clause.
   SELECT subject_code, count(name)
   FROM students
   GROUP BY subject_code;

The WHERE clause cannot be used to restrict groups. The HAVING clause should be used.
   SELECT subject_code, AVG (marks)
   FROM students
   HAVING AVG(marks) > 75
   GROUP BY subject_code;
False. Group functions can be nested to a depth of two.

A subquery is a SELECT statement embedded in a clause of another SELECT statement. It is used when the inner query, or the subquery returns a value that is used by the outer query. It is very useful in selecting some rows in a table with a condition that depends on some data which is contained in the same table.
False. A single row subquery returns only one row from the inner SELECT statement.
True.
True.

Here a single row operator = is used with a multiple row subquery.

IN, ANY, ALL.

The DML statements are used to add new rows to a table, update or modify data in existing rows, or remove existing rows from a table.

The INSERT INTO statement.
True.

Null values can be inserted into a table by one of the following ways −
  • Implicitly by omitting the column from the column list.
  • Explicitly by specifying the NULL keyword in the VALUES clause.
False. INSERT statement allows to add rows to a table copying rows from an existing table.

The INSERT statement can be used to add rows to a table by copying from another table. In this case, a subquery is used in the place of the VALUES clause.

All the rows in the table are modified.

Yes. Use of subqueries in UPDATE statements allow you to update rows in a table based on values from another table.
False. The DELETE statement is used for removing existing rows from a table.

All the rows in the table are deleted.

Yes, subqueries can be used to remove rows from a table based on values from another table.
True.
True.

The MERGE statement allows conditional update or insertion of data into a database table. It performs an UPDATE if the rows exists, or an INSERT if the row does not exist.
True.

VARCHAR2 represents variable length character data, whereas CHAR represents fixed length character data.
False. A DROP TABLE statement cannot be rolled back.

The ALTER TABLE statement.

A view is a logical snapshot based on a table or another view. It is used for −
  • Restricting access to data;
  • Making complex queries simple;
  • Ensuring data independency;
  • Providing different views of same data.
True.

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...