SQL Interview Questions
SQL Interview Questions
Ans: SQL stands for structured query language. It is a standard language for accessing and
manipulating databases. SQL can execute queries like create, insert, delete, update, etc. on database
Ans: A database is an organized collection of data so that it can be easily accessed and managed.
1) DDL (Data definition language): DDL changes the structure of table. DDL commands are:
create, alter, drop, truncate
2) DML (Data manipulation language): DML commands are used to modify database. DML
commands are: insert, update, delete
3) DCL (Data control language): DCL is used to manage access rights, permissions and security.
DCL commands are: grant, revoke
4) TCL (transaction control language): TCL commands are used to manage transactions. They can
be only used with DML commands. TCL commands are: commit, rollback, savepoint.
5) DQL (data query language): DQL is used to fetch the data from database. DQL queries are:
select.
Ans: DBMS and RDBMS both are used to store information in physical database. DBMS applications
store data as file. While RDBMS applications store data in tabular form.
Ans: Primary key is the constraint that uniquely identifies each record in a table. It cannot contain
null values. A table can have only one primary key.
Ans: Foreign key is a field in one table that refers to the primary key in another table. The table with
foreign key is called child table.
Ans: primary key is the unique, not null key that uniquely identifies every record in the table. While,
the unique key is a single column or combination of columns to uniquely identify database records.
Primary key does not allow null values while unique key allows storing null value.
Ans: Join clause is used to combine rows from two or more tables. Types of join: inner join, outer
join, cross join, self join
Ans: Union operator is used to combine two or more rows. it removes the duplicate rows from the
table. Union all is used to combine two or more rows but it does not remove duplicate records.
Ans: The main difference between them is that where clause is used to specify a condition for
filtering records before any groupings are made while having clause is used to specify a condition
from a group.
Ans: Transactions group a set of tasks into a single execution unit. Each transaction begins with a
specific job and ends when all the tasks are successfully executed.
Ans: Database is an organized collection of data so that it can be easily accessed and managed,
whereas schema is a logical representation of the database.
Ans: group by clause is used to arrange identical data into groups with the help of aggregate
functions. The purpose of group by clause is to arrange the rows which have same values in a group.
Ans: char datatype is used to store character strings of fixed length. It takes extra memory space as it
leaves blank spaces to reach to the assigned length. Whereas, varchar datatype is used to store
character strings of variable length. It does not take extra memory space. The storage size of char
datatype is n bytes i.e. set length while the storage size of varchar is actual length of entered string in
bytes.
Ans: A stored procedure is a prepared code that can be saved and can be used over and over again.
write a sql query and if it has to use again save it as a stored procedure and then call it to execute it.
Ans: A subquery is a query within another query. The outer query is called as main query and the
inner query is called as subquery.
Ans: A view is the virtual table that is based on the result of the SQL query.
Q19. What is the difference between cross join and inner join?
Ans: cross join is the full cartesian product of two sides of a join whereas, inner join is the
intersection i.e. reduction of cartesian product.
Q20. What is the purpose of commit statement?
Ans: Commit is the TCL command which is used to permanently save the changes. The database
cannot regain its previous state after execution of commit statement.
Ans: Rollback is TCL command which is used to undo the transactions that have not been saved in
the database.
Ans: A field with null value is one that has been left blank during filling records. If a field in a table is
optional, it is possible to insert or update a new record without adding a value to this field. Then, the
field will be saved with null value.
Ans: Constraints are the rules given to columns. They can be specified when the table is created.
Types:
Ans: the trigger is a special procedure which cannot be called directly like a stored procedure. Trigger
is called automatically when a data modification occurs. Trigger allows you to specify actions that
should be executed automatically when a specific event occurs.
Q26. What is the difference between unique constraint and a unique index?
Ans: A unique index ensures that the values in the index key columns are unique. Whereas, a unique
constraint ensures that no duplicate values can be inserted into the columns.
Ans: Union operation is only performed on tables that contains same number of columns with same
datatype. Whereas, join operation can be performed on the tables that has at least one common
field between them.
Ans: Grant statement is used to grant permissions to modify and retrieve data.
Q29. What is the difference between natural join and inner join?
Ans: Natural join joins two tables based on the same attribute name and datatypes. The resulting
table will contain all the attributes of both tables eliminating the duplicate columns. Inner join
combines data from two or more tables base don the specified condition. The resulting table of inner
join will contain all attributes from both tables including duplicate columns also.
Ans: Self join joins the table with itself. It allows to combine rows from the same table based on the
specific condition.
Ans: SQL aliases are used to give a table or a column a temporary name. An alias is created with AS
keyword. Ex. Select CustomerId AS ID from customers;
Ans: The SQL functions can be used to calculate statistics such as number of rows in table, total
value of a specific column, the average value of a column, the min or max value from the column,
etc.
Ans: like is used to check if the column value is similar to specified character pattern.
In between is used to check if the column value Is equal to any one of a specified set of values.
Q35. Explain the difference between aggregate function and scalar function.
Ans: Aggregate functions are used to do operations from values of the column and returns a single
value. Whereas, scalar functions are based on user inputs which returns single value.
Ans: normalization is the process of organizing data in a database to minimize redundancy and
dependency. It involves breaking down a table into smaller tables and establishing relations between
them.
Ans: a clustered index determines the physical order of data in table. A table can have only one
clustered index. It changes the way data is stored and can be created on only one column.
A non-clustered index does not affect the physical order of the data in table. It is stored separately
and contains pointer to the actual data. A table can have multiple non-clustered index.
Ans: a correlated subquery is a subquery that refers to a column from the outer query. It executes
once for each row.
Ans: top or limit clause is used to limit the number of rows returned by a query.
Ans: a data warehouse is a large, centralised repository which stores and manages data from various
sources. It is designed for efficient reporting, analysis and business intelligence purposes.
Ans: the simplest way to get current time and date is by using NOW() function. Another way is by
using current_timestamp() function.
Date_format() function can be used to return a customised format of date and time.