SQL Interview QZ and Answer Ok
SQL Interview QZ and Answer Ok
SQL Interview QZ and Answer Ok
1. 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.
2. 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).
3. What is SQL ?
Institute) standard.
DDL is used to define the structure that holds the data. For example, Create,
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,
DCL is used to control the visibility of data like granting database access and set
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
3. SQL is actually a very powerful language and by using its language elements
Salary etc.
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
3. Isolation
Database lock tells a transaction, if the data item in questions is currently being
1. Shared Lock
When a shared lock is applied on data item, other transactions can only read the
2. Exclusive Lock
When an exclusive lock is applied on data item, other transactions can't read or
In database design, we start with one single table, with all possible columns. A
lot of redundant data would be present since it’s a single table. The process of
removing the redundant data, by splitting up the table in a well defined
A relation is said to be in first normal form if and only if all underlying domains
contain atomic values only. After 1NF, we can still have redundant data.
A relation is said to be in 2NF if and only if it is in 1NF and every non key
attribute is fully dependent on the primary key. After 2NF, we can still have
redundant data.
A relation is said to be in 3NF, if and only if it is in 2NF and every non key
A primary key is a column whose values uniquely identify every row in a table.
Primary key values can never be reused. If a row is deleted from the table, its
primary key may not be assigned to any new rows in the future. To define a field
as primary key, following conditions had to be met :
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.
Primary Key. In this case, both the columns will be represented as primary key.
17. What is a Foreign Key ?
When a "one" table's primary key field is added to a related "many" table in order
to create the common field which relates the two tables, it is called a foreign key
For example, the salary of an employee is stored in salary table. The relation is
Unique key is same as primary with the difference being the existence of null.
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.
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.
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.
data duplication, data is stored in related tables. Join keyword is used to fetch
data from related tables. "Join" return rows when there is at least one match in
Right Join
Return all rows from the right table, even if there are no matches in the left table.
Outer Join
Left Join
Return all rows from the left table, even if there are no matches in the right table.
Full Join
Self-join is query used to join a table to itself. Aliases should be used for the
Cross Join will return all records where each row from the first table is combined
The views are virtual tables. Unlike tables that contain data, views simply contain
updates on specific duration, base upon the interval specified in the query
Advantages:
2. The view can be used to hide some of the columns from the table.
3. Views can provide Access Restriction, since data insertion, update and
Disadvantages:
2. Since the view is created when a query requesting data from view is triggered,
3. When views are created for large tables, it occupies more memory.
table, views.
retrieve data.
cannot be performed. Where condition can be used along with delete statement
table.
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
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.
Departmen
Employee_id First_name Last_name Salary Joining_date
t
01-JAN-13 12.00.00
1 John Abraham 1000000 Banking
AM
01-JAN-13 12.00.00
2 Michael Clarke 800000 Insurance
AM
01-FEB-13 12.00.00
3 Roy Thomas 700000 Banking
AM
01-FEB-13 12.00.00
4 Tom Jose 600000 Insurance
AM
01-FEB-13 12.00.00
5 Jerry Pinto 650000 Insurance
AM
01-JAN-13 12.00.00
6 Philip Mathew 750000 Services
AM
01-JAN-13 12.00.00
7 TestName1 123 650000 Services
AM
01-FEB-13 12.00.00
8 TestName2 Lname% 600000 Insurance
AM
Table Name : Incentives
Incentive_dat
Employee_ref_id Incentive_amount
e
1 01-FEB-13 5000
2 01-FEB-13 3000
3 01-FEB-13 4000
1 01-JAN-13 4500
2 01-JAN-13 3500
EMPLOYEE
(convert(varchar,joining_date,103),7,4) , SUBSTRING
(convert(varchar,joining_date,100),1,3) , SUBSTRING
15. Get all employee details from the employee table order
by First_Name Ascending
Select * from employee order by FIRST_NAME asc
16. Get all employee details from the employee table order
by First_Name descending
17. Get all employee details from the employee table order
by First_Name Ascending and Salary descending
Select * from employee order by FIRST_NAME asc,SALARY desc
to_char(joining_date,'YYYY')='2013'
SQL Queries in SQL Server, Select * from EMPLOYEE where
SUBSTRING(convert(varchar,joining_date,103),7,4)='2013'
year(joining_date)='2013'
to_char(joining_date,'Mon')='Jan'
SUBSTRING(convert(varchar,joining_date,100),1,3)='Jan'
month(joining_date)='01'
<to_date('01/01/2013','dd/mm/yyyy')
>to_date('31/01/2013','dd/mm/yyyy')
from EMPLOYEE
EMPLOYEE
SQL Queries in MySQL, Select CONVERT(DATE_FORMAT(joining_date,'%Y-
EMPLOYEE
EMPLOYEE
department
(JOINING_DATE,'YYYY'),to_char(JOINING_DATE,'MM')
datepart(MM,JOINING_DATE)
SQL Queries in MySQL, select year (JOINING_DATE) Join_Year,month
year(JOINING_DATE), month(JOINING_DATE)
MINUS
EMPLOYEE
SQL Queries in SQL Server and MySQL, SELECT case DEPARTMENT when
'Banking' then 'Bank Dept' when 'Insurance' then 'Insurance Dept' when
from INCENTIVES)
55. Insert into employee table Last Name with " ' " (Single
Quote - Special Character)
a.EMPLOYEE_ID=b.EMPLOYEE_REF_ID
FIRST_NAME='John' )
incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
INCENTIVE_AMOUNT >3000
incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
62. Select first_name, incentive amount from employee and
incentives table for all employees even if they didn't get
incentives and set incentive amount as 0 for those
employees who didn't get incentives.
SQL Queries in Oracle, Select FIRST_NAME,nvl(INCENTIVE_AMOUNT,0)
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
SQL Queries in Oracle, select * from (select * from employee order by SALARY
SQL Queries in MySQL, select * from employee order by salary desc limit 2
SQL Queries in Oracle, select * from (select * from employee order by SALARY
SQL Queries in MySQL, select * from employee order by salary desc limit N
SQL Queries in Oracle, select min(salary) from (select * from (select * from
SQL Queries in SQL Server, select min(SALARY) from (select top 2 * from
employee) a
SQL Queries in SQL Server, select min(SALARY) from (select top N * from
employee) a
EMPLOYEE
Both UNION and UNION ALL is used to select information from structurally
similar tables. That means corresponding columns specified in the union should
have same data type. For example, in the above query, if FIRST_NAME is
DOUBLE and LAST_NAME is STRING above query wont work. Since the data
type of both the columns are VARCHAR, union is made possible. Difference
between UNION and UNION ALL is that , UNION query return only distinct
values.
71. Write create table syntax for employee table
Oracle -CREATE TABLE EMPLOYEE (
EMPLOYEE_ID NUMBER,
SALARY FLOAT(126),
KEY(EMPLOYEE_ID)
74. Write syntax to set 2 fields(EMPLOYEE_ID,FIRST_NAME)
as primary key in employee table
KEY(EMPLOYEE_ID,FIRST_NAME)
DECLARE
seq_no number(12);
BEGIN
END;
SHOW ERRORS;
REFRESH COMPLETE
NEXT SYSDATE + 1 AS
where a.EMPLOYEE_ID=b.EMPLOYEE_REF_ID
Create materialized view log for fast refresh. Following materialized view script
BUILD IMMEDIATE
INCENTIVES b
SQL Injection is one of the the techniques uses by hackers to hack a website by