Sql Interview Questions:-
What is RDBMS?
Relational Database Management system (RDBMS) is a database management system (DBMS) that is based on the relational model. Data from relational database can be accessed or reassembled in many different ways without having to reorganize the database tables. Data from relational database can be accessed using an API , Structured Query Language (SQL).
What is DBMS ?
The database management system is a collection of programs that enables user to store, retrieve, update and delete information from a database.
What is SQL?
SQL stands for Structured Query Language , and it is used to communicate with the Database. This is a standard language used to perform tasks such as retrieval, updation, insertion and deletion of data from a database.
Standard SQL Commands are Select.
What is a Database?
Database is nothing but an organized form of data for easy access, storing, retrieval and managing of data. This is also known as structured form of data which can be accessed in many ways.
Example:- School Management Database, Bank Management Database.
What are tables and Fields?
A table is a set of data that are organized in a model with Columns and Rows. Columns can be categorized as vertical, and Rows are horizontal. A table has specified number of column called fields but can have any number of rows which is called record.
Field:- Stu ID, Stu Name, Date of Birth.
Data:- 201456, David, 11/15/1960.
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, and it has implicit NOT NULL constraint. It means, Primary key values cannot be NULL.
What is a unique key?
A Unique key constraint uniquely identified each record in the database. This provides uniqueness for the column or set of columns.
A Primary key constraint has automatic unique constraint defined on it. But not, in the case of Unique Key.
There can be many unique constraint defined per table, but only one Primary key constraint defined per table.
What is a foreign key? What is a foreign key?
A foreign key is one table which can be 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.
What is a join?
This is a keyword used to query data from more tables based on the relationship between the fields of the tables. Keys play a major role when JOINs are used
What are the types of join and explain each?
There are various types of join which can be used to retrieve data and it depends on the relationship between tables.
1. Inner join :- Inner join return rows when there is at least one match of rows between the tables.
2. Right Join :- Right join return rows which are common between the tables and all rows of Right hand side table. Simply, it returns all the rows from the right hand side table even though there are no matches in the left hand side table.
3. Left Join :- Left join return rows which are common between the tables and all rows of Left hand side table. Simply, it returns all the rows from Left hand side table even though there are no matches in the Right hand side table.
4. 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.
What is normalization?
Normalization is the process of minimizing redundancy and dependency by organizing fields and table of a database. The main aim of Normalization is to add, delete or modify field that can be made in a single table.
What is Denormalization?
DeNormalization is a technique used to access the data from higher to lower normal forms of database. It is also process of introducing redundancy into a table by incorporating data from the related tables.
What are the different type of SQL's statements?
SQL statements are broadly classified into three. They are :-
1. DDL - Data Definition Language :-
DDL is used to define the structure that holds the data. For example, Create, Alter, Drop and Truncate table.
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. The Select statement is considered as a limited version of the DML, since it can't change the data in the database. But it can perform operations on data retrieved from the DBMS, before the results are returned to the calling function.
3. DCL - Data Control Language :-
DCL is used to control the visibility of data like granting database access and set privileges to create tables, etc. Example - Grant, Revoke access permission to the user to access data in the database.
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 programmers to interact with any database like ORACLE, SQL ,MYSQL etc.
2. SQL is easy to learn. The statements are all made up of descriptive English words, and there aren't that many of them.
3. SQL is actually a very powerful language and by using its language elements you can perform very complex and sophisticated database operations.
What are all the different normalizations?
The normal forms can be divided into 5 forms, and they are explained below :-
1. First Normal Form (1NF) :-
This should remove all the duplicate columns from the table. Creation of tables for the related data and identification of unique columns.
2. Second Normal Form (2NF) :-
Meeting all requirements of the first normal form. Placing the subsets of data in separate tables and Creation of relationships between the tables using primary keys.
3. Third Normal Form (3NF):-
This should meet all requirements of 2NF. Removing the columns which are not dependent on primary key constraints.
4. Fourth Normal Form (4NF):-
Meeting all the requirements of third normal form and it should not have multi- valued dependencies.
What is a database transaction?
Database transaction takes database from one consistent state to another. At the end of the transaction the system must be in the prior state if the transaction fails or the status of the system should reflect the successful completion if the transaction goes through.
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 get 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 if it 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.
What is a View?
A view is a virtual table which consists of a subset of data contained in 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.
What is an Index?
An index is performance tuning method of allowing faster retrieval of records from the table. An index creates an entry for each value and it will be faster to retrieve data.
What is a Database Lock?
Database lock tells a transaction, if the data item in questions is currently being used by other transactions.
What are the type of locks?
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 an exclusive lock is applied on data item, other transactions can't read or write into the data item.
What are all the different types of indexes?
There are three types of indexes :-
1. 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.
2. Clustered Index :-This type of index reorders the physical order of the table and search based on the key values. Each table can have only one clustered index.
3. NonClustered Index :-NonClustered Index does not alter the physical order of the table and maintains logical order of data. Each table can have 999 nonclustered indexes.
What is a Composite Key?
A Composite primary key is a type of candidate key, which represents a set of columns whose values uniquely identify every row in a table.
What is a Composite Primary Key?
A Composite primary key is a set of columns whose values uniquely identify every row in a table. What it means is that, a table which contains composite primary key will be indexed based on the columns specified in the primary key. This key will be referred in Foreign Key tables.
For example :- if the combined effect of columns, "Student_ID" and "Student Name" in a table is required to uniquely identify a row, its called a Composite Primary Key. In this case, both the columns will be represented as primary key.
Define SQL Insert Statement?
SQL INSERT statement is used to add rows to a table. For a full row insert, SQL Query should start with "insert into" statement followed by table name and values command, followed by the values that need to be inserted into the table. The insert can be used in several ways :-
1. To insert a single complete row.
2. To insert a single partial row.
What is a Cursor?
A database Cursor is a control which enables traversal over the rows or records in the table. This can be viewed as a pointer to one row in a set of rows. Cursor is very much useful for traversing such as retrieval, addition and removal of database records.
Define SQL Update Statement?
SQL Update is used to update data in a row or set of rows specified in the filter condition.
The basic format of an SQL UPDATE statement is, Update command followed by table to be updated and SET command followed by column names and their new values followed by filter condition that determines which rows should be updated.
Define SQL Delete Statement?
SQL Delete is used to delete a row or set of rows specified in the filter condition.
The basic format of an SQL DELETE statement is, DELETE FROM command followed by table name followed by filter condition that determines which rows should be updated.
What is a relationship and what are they?
Database Relationship is defined as the connection between the tables in a database. There are various data basing relationships, and they are as follows :-
1. One to One Relationship.
2. One to Many Relationship.
3. Many to One Relationship.
4. Self-Referencing Relationship.
What is a stored procedure?
Stored Procedure is a function consists of many SQL statement to access the database system. Several SQL statements are consolidated into a stored procedure and execute them whenever and wherever required.
What is a trigger?
A DB trigger is a code or programs that automatically execute with response to some event on a table or view in a database. Mainly, trigger helps to maintain the integrity of the database.
Example :-When a new student is added to the student database, new records should be created in the related tables like Exam, Score and Attendance tables.
What is Self-Join?
Self-join is query used to join a table to itself. Aliases should be used for the same table comparison.
What is Cross Join?
Cross Join will return all records where each row from the first table is combined with each row from the second table.
What is a materialized view?
Materialized views are also a view but are disk based. Materialized views get updates on specific duration, base upon the interval specified in the query definition. We can index materialized view.
What is the difference between DELETE and TRUNCATE commands?
DELETE command is used to remove rows from the table, and WHERE clause can be used for conditional set of parameters. Commit and Rollback can be performed after delete statement.
TRUNCATE removes all rows from the table. Truncate operation cannot be rolled back.
What are the advantages of a stored procedure?
Stored Procedures are precomplied and stored in the database. This enables the database to execute the queries much faster. Since many queries can be included in a stored procedure, round trip time to execute multiple queries from source code to database and back is avoided.
What is the difference between Cluster and Non cluster Index?
A clustered index reorders the way records in the table are physically stored. There can be only one clustered index per table. It makes data retrieval faster.
A non clustered index does not alter the way it was stored but creates a completely separate object within the table. As a result insert and update command will be faster.
What is Union, minus and Interact commands?
MINUS operator is used to return rows from the first query but not from the second query.
INTERSECT operator is used to return rows returned by both the queries.
The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types.
What is a constraint?
Constraint can be used to specify the limit on the data type of table. Constraint can be specified while creating or altering the table statement. Sample of constraint are :-
1. NOT NULL
5. PRIMARY KEY
6. FOREIGN KEY
What are all types of user defined functions?
Three types of user defined functions are :-
1. Scalar Functions
2. Inline Table valued functions
3. Multi statement valued functions
Scalar returns unit, variant defined the return clause. Other two types return table as a return.
What is OLTP (Online Transaction Processing)?
In OLTP :- online transaction processing systems relational database design use the discipline of data modeling and generally follow the Codd rules of data normalization in order to ensure absolute data integrity.
Using these rules complex information is broken down into its most simple structures (a table) where all of the individual atomic level elements relate to each other and satisfy the normalization rules.
When is the use of UPDATE_STATISTICS command?
This command is basically used when a large processing of data has occurred. If a large amount of deletions any modification or Bulk Copy into the tables has occurred, it has to update the indexes to take these changes into account. UPDATE_STATISTICS updates the indexes on these tables accordingly.
What is collation?
Collation is defined as set of rules that determine how character data can be sorted and compared. This can be used to compare A and, other language characters and also depends on the width of the characters.
ASCII value can be used to compare these character data.
What are all different types of collation sensitivity?
Following are different types of collation sensitivity :-
1. Case Sensitivity :- A and a and B and b.
2. Accent Sensitivity
3. Kana Sensitivity :- Japanese Kana characters.
4. Width Sensitivity :- Single byte character and double byte character.
What is Auto Increment?
Auto increment keyword allows the user to create a unique number to be generated when a new record is inserted into the table. AUTO INCREMENT keyword can be used in Oracle and IDENTITY keyword can be used in SQL SERVER.
Mostly this keyword can be used whenever PRIMARY KEY is used.
What is Datawarehouse?
Datawarehouse is a central repository of data from multiple sources of information. Those data are consolidated, transformed and made available for the mining and online processing. Warehouse data have a subset of data called Data Marts.
What is the difference between a Local and a Global temporary table?
1. A local temporary table exists only for the duration of a connection or, if defined inside a compound statement, for the duration of the compound statement.
2. A global temporary table remains in the database permanently, but the rows exist only within a given connection. When connection is closed, the data in the global temporary table disappears. However, the table definition remains with the database for access when database is opened next time.
What is CHECK Constraint?
A CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity.
What is BCP? When does it used?
BulkCopy is a tool used to copy huge amount of data from tables and views. BCP does not copy the structures same as source to destination. BULK INSERT command helps to import a data file into a database table or view in a user-specified format.
What is subquery?
A subquery is a query within another query. The outer query is called as main query, and inner query is called subquery. SubQuery is always executed first, and the result of subquery is passed on to the main query.
What are the types of subquery?
There are two types of subquery :-
1. Correlated subquery:- A correlated subquery cannot be considered as independent query, but it can refer the column in a table listed in the FROM the list of the main query.
2. Non-Correlated subquery :- A Non-Correlated subquery can be considered as independent query and the output of subquery are substituted in the main query.