0% found this document useful (0 votes)
9 views

SQL Interview Questions

1. A primary key uniquely identifies each row in a table and cannot contain NULL values. A unique key also uniquely identifies rows but can contain NULL values. Foreign keys relate data between tables through a primary key. 2. There are several types of joins including inner, right, left, and full joins. Inner joins return rows with matches between tables. Right and left joins return all rows from one table along with matched rows from another. Full joins return all rows when there are matches in either table. 3. Constraints like NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY impose restrictions on column values to enforce data integrity.

Uploaded by

checharenisha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Interview Questions

1. A primary key uniquely identifies each row in a table and cannot contain NULL values. A unique key also uniquely identifies rows but can contain NULL values. Foreign keys relate data between tables through a primary key. 2. There are several types of joins including inner, right, left, and full joins. Inner joins return rows with matches between tables. Right and left joins return all rows from one table along with matched rows from another. Full joins return all rows when there are matches in either table. 3. Constraints like NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY impose restrictions on column values to enforce data integrity.

Uploaded by

checharenisha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

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

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

4. 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.
Inner join.
Inner join return rows when there is at least one match of rows between the tables.
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.
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.
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.

5. What is an Index? What are all the different types of indexes?


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.

There are three types of indexes -.


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.
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.
Non-Clustered Index.
Non-Clustered Index does not alter the physical order of the table and maintains logical
order of data. Each table can have 999 non-clustered indexes.
6. 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.

7. 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. NOT NULL - Restricts NULL value from being inserted into a column.
• CHECK - Verifies that all values in a field satisfy a condition.
• DEFAULT - Automatically assigns a default value if no value has been specified for the field.
• UNIQUE - Ensures unique values to be inserted into the field.
• INDEX - Indexes a field providing faster retrieval of records.
• PRIMARY KEY - Uniquely identifies each record in a table.
• FOREIGN KEY - Ensures referential integrity for a record in another table.

8. How Do you find all Employees with its managers? (Consider there is manager id also in
Employee table)
Select e.employee_name,m.employee name from Employee e,Employee m where
e.Employee_id=m.Manager_id;

9. How to find count of duplicate rows?


Select rollno, count (rollno) from Student Group by rollno Having count (rollno)>1 Order by
count (rollno) desc;

10. What are views in SQL? Explain types of Views in SQL


Views are nothing but the logical structure of the table where we can fetch the data from
different tables or same table.
There are 2 types of views in Oracle:
Simple View: Simple view has been created on only a single table.
Complex view: Views which are created using more than 1 table which has joins clauses are
known as complex views.

11. Query to find Second Highest Salary of Employee?


Select distinct Salary from Employee e1 where 2=Select count(distinct Salary) from Employee
e2 where e1.salary<=e2.salary;

select min(salary)from(select distinct salary from emp order by salary desc)where


rownum<=2;

12. How to get 3 Highest salaries records from Employee table?


select distinct salary from employee a where 3 >= (select count(distinct salary) from
employee b where a.salary <= b.salary) order by a.salary desc;

select min(salary)from(select distinct salary from emp order by salary desc)where


rownum<=3;

You might also like