Having a Database is one of the crucial requirements of today’s industry. Wherein relational database is the most commonly used one. A relational Database basically has relations or, in easier words, a database where we can store data in the form of tables. Before you go in and attend a SQL interview, it is essential to know the various SQL interview questions for experienced professionals, or the SQL interview questions for freshers. So here we have a list of a few SQL interview questions which are likely to be asked in the interview.
Q1. What is a Database?
Ans. It is a place where we can store data in a systematic and organized manner. So that the retrieval of the data is easy.
Q2. What is SQL?
Ans. SQL stands for Structured Query Language. It is a standard query language which is used for maintaining relational databases and performing different tasks like manipulating data, accessing the data and so on.
Q3. What are the uses of SQL?
Ans. SQL is used for maintaining the database. Wherein we can create a new table or a database, read the data, update the data, retrieve the data or delete the data.
Q4. What is a Primary key?
Ans. Primary key is a constraint which is used to uniquely identify the records from the table. It is a combination of Unique and Not Null constraints. A table can have only one primary key and is recommended to have a primary key but not mandatory.
Q5. What is foreign key?
Ans. Foreign key is used to establish connection between any two tables. Also, a foreign key of one table can be related as the primary key for another table. We can have any number of foreign keys which can accept duplicate values and can be null.
Q6. What are SQL statements?
Ans. In SQL statements we have:
- DDL (Data Definition Language) which has Create, Alter, Rename, Truncate and Drop.
- DML (Data Manipulation Language) which has Insert, Update, Delete.
- DCL (Data Control Language) which has Grant, Revoke.
- TCL (Transmission Control Language) which has Commit, Savepoint, Rollback.
- DQL (Data Query Language) has Select, Projection, Selection and Joins.
Q7. What are Joins?
Ans. Joins are basically used to retrieve the data from multiple tables simultaneously. Types of Joins:
- Cartesian Join also known as Cross Join.
- Inner Join also known as Equi Join.
- Outer Join, which is further divided into:
- Left Outer Join.
- Right Outer Join.
- Full Outer Join.
- Natural Join.
- Self Join.
Q8. What is Inner Join?
Ans. Inner Join is used to obtain only matched records using join condition. Join condition is a condition on which two tables are being merged.
Q9. What is Outer join?
Ans. Outer join is used to obtain both matches along with unmatched records from the table.
Q10. What are the different operators used in SQL?
Ans. We have
- Arithmetic Operators like ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION.
- Comparison Operators like =, !=, <, >, <=, >=.
- Logical Operators like AND, OR,NOT.
- Special Operators like IN, NOT IN, BETWEEN, NOT BETWEEN, IS, IS NOT, LIKE, NOT LIKE.
Q11. What is the query?
Ans. A database query is a code which is written in order to get data back from the database.
Q12. What is Subquery?
Ans. A query written inside another query is known as a subquery. It generally consists of an inner query that is executed first, and an output is generated. This output from the inner query is given to the outer query as an input. Taking this input from the inner query, the outer query executes completely and generates the result. So from here, we can say that the outer query depended on the inner query.
Q13. Why do I go for subquery?
Ans. When we have to find an unknown value, and when the data to be displayed is present in one table and the condition to be applied is present in another table, we go to subquery. Basically to establish connections between those two tables.
Q14. What are the types of subquery?
Ans. There two types of subquery: Correlated and Non-Correlated,
A correlated subquery is not considered as an independent query, but it can refer the column in the table listed in the FROM the list of the main query, whereas a non-correlated sub query can be considered as a independent query and the output of subquery are substituted in the main query.
Q15. What is the difference between drop and delete?
Ans. DROP is used to delete all the records along with the table structure, whereas DELETE is used to delete a single record.
Q16. What is the difference between truncate and drop?
Ans. TRUNCATE is used to delete all the records from the table but not the table structure, whereas in DROP we delete all the data along with the table structure.
Q17. Can we get back the table if we have already dropped the table?
Ans. Yes, we can get back the table by using the FLASHBACK statement. This statement is used to gain all the records along with the table structure as it is.
Q18. Write an SQL query to find the employee name which starts with A.
Ans. SELECT * FROM EMPLOYEE WHERE EMPNAME LIKE ‘A%’ ;
Q19. Write a query to get the second maximum salary of an employee from the employee table.
Ans. SELECT MAX (SAL) FROM EMP WHERE SAL < ( SELECT MAX (SAL) FROM EMP );
Q20. Write a query to display employee name and salary, if the employee is getting a salary more than Adam.
Ans. SELECT EMPNAME, SAL FROM EMP WHERE SAL > ( SELECT SAL FROM EMP WHERE EMPNAME = ‘ADAM’);
This brings us to the end of the blog on SQL interview questions and answers. The above SQL interview questions would be helpful for fresher’s applying for the jobs as well for the people who have experience along with their few projects. These were a few basic examples that you could just brush up before your interview.