sql interview questions

Top 46 Interview Questions for SQL That You Must Prepare

There is a wide demand for SQL developers in India. Structured Query Language obtains, manages and retrieves data stored in relational databases. Before appearing for an interview, it is important to brush up on your knowledge to be able to answer interview questions for SQL. Since SQL deals with a large volume of data, it is also crucial to understand queries in SQL interview questions so that retrieving data from any database is easy.

Apart from this, most of the programming languages use SQL database and complete grasp on this topic will help you stand out among your peers. Data is the new oil of this century and you have to master the art of manipulating the data learning various database query languages. As the world embraces digital technology, knowledge of artificial intelligence, application of IoT (Internet of Things), machine learning, big data, etc.

Whatever technology comes in, they will require a database. You can imagine the requirement of database developers, engineers, and data scientists. They need to have knowledge of the SQL database, MongoDB, Oracle, etc.

Are you looking forward to building your career in SQL? You need to go through a rigorous interview. The following interview questions will assist you to get an overview of what SQL interview is like and what type of questions to expect.

Most Common SQL Interview Questions

Here is a set of SQL Interview Questions and SQL Interview Questions on Queries that will be useful to prepare before appearing for an interview:

1. What is SQL?

Structured Query Language or SQL is a computer language. It communicates with relational databases. As a result, SQL can create a database, retrieve data from a database, create or update tables in a database.

2. What is a database?

A database is a structured form of data that a computer system stores. In addition, a database management system obtains a database. It contains tables, queries, views, etc

3. What is DBMS?

Database Management System (DBMS) is a software which deals with the management, modification, creation and retrieval of a database. It acts as an interface between the database and the end user software and ensures the accessibility of our data.

4. What is an RDMS?

RDMS stands for Relational Database Management System. It collects data in the form of a compilation of tables, which are related by a constant field.

5. Does SQL support programming language characteristics?

SQL does not support programming because it is not a programming language, it is a command language. Therefore, it only uses commands to handle data in a database.

6. What is the difference between SQL and MySQL?

SQL is a standard language mainly used for manipulation structured databases. Whereas, MySQL is a relational database management system that is used to manage SQL databases.

7. What is a join?

Join is a keyword utilized to query data from tables based on their connection among the fields of the tables. When joins are used, keys play a major role.

8. What is a Self Join?

A Self Join is a Join wherein a table is joined with itself. This is specially the case when the table has a FOREIGN KEY which then references its own PRIMARY KEY. Self Join uses the INNER JOIN or LEFT JOIN clause and a table alias is used to assign different names to the table within a query.

9. What is a Cross Join?

Cross Join is a Cartesian product of two tables included in a join.It combines each row of the first table with each row of the second table. If a WHERE clause is used in a cross join, then the query will work like an INNER JOIN.

10. What are the types of join?

Various types of joins are utilized to recover data depending upon the connection between tables.

a) Inner Join: Inner join returns rows if there is at least one match of rows within tables.

b) Right Join: Right join returns rows that are shared among the tables and all rows on the right side table even if there are no equivalents on the left side table.

c) Left Join: Left join returns rows that are mutual between the tables and all rows on the left side table even if there are no equivalents on the right side table.

d) Full Join: Full join replaces rows if there are coordinating rows in any of the tables. This means all the rows on the left side table as well as all the rows on the right side table.

11. What is a view in SQL?

In SQL, a view is a virtual table. It contains rows and columns just like a real table in a database. Selecting fields from one or more tables in a database create a view. It can either have all the rows of a table or specific rows based on certain conditions.

12. Explain the Union, Minus and Intersect Commands.

UNION: A Union operator works upon combining the results of two tables. Thereafter, it also eliminates duplicate rows that are present in the table.

MINUS: The Minus operator returns rows from the first query. But it does not help in removing the same form the second query. Therefore, the matching records of first and second query and all other rows from the first query are displayed as a result set by this operator.

INTERSECT: The Intersect operator works on finally returning the rows that are returned by both the queries.

13. Define query.

SQL interview questions on queries are very common too. But before solving queries in SQL interview questions, you need to be able to define what a query is. A query is a code formulated to get data from the database. A query meets our expectations of the desired outcome.

14. Explain a subquery?

A subquery is a query within a query. The outer query is called the main query and, as a result, the inner query becomes a subquery. A subquery is continually solved first, and the results are transferred to the main query.

15. What are the types of subqueries?

There are two types of subqueries:

a) Correlated: a correlated query is not an independent query

b) Non-correlated: a non-correlated query can be contemplated as an independent query

16. What is the meaning of a stored procedure?

A stored procedure is a function that consists of many SQL statements to obtain the database system. Furthermore, these statements are then incorporated into a stored procedure and executed as needed.

17. Explain what is a recursive stored procedure?

After reaching some boundary condition, a stored procedure calls by itself. This is called a recursive stored procedure. The procedure is important because with the help of this procedure the programmers can use a particular given set of code any number of times.

18. What is the difference between CHAR and VARCHAR2 datatype in SQL?

Char and Varchar2 are the two types of datatypes which are used for characters. Varchar2 is used for character strings of variable lengths. Char on the other hand is used for character strings of a fixed length.

19. What is the ALIAS command?

A table or a column can be named ALIAS. The Alias name can be used to refer in the WHERE clause to help identify the column or the table.

20. What is an index?

A database index is a data structure that increases the rate of data retrieval procedures on a database table at the expense of additional writes and the application of more storage space to maintain the additional copy of the data.

21. Differentiate between cluster and non-cluster index?

Cluster index is used to alter the way the records are collected and obtains data from a database effortlessly. Whereas a non-cluster index does not change the system in which records are kept, instead, it creates a completely separate object within the table.

22. What is a data warehouse?

A data warehouse is a primary treasury of data from various sources of data. Furthermore, this data is then compressed, reconstructed and available for the online process. The warehouse has a collection of data named as data marts.

23. What are transactions and their controls?

A sequence task that is performed on databases is known as transaction. The task is performed in a logical manner to gain certain results. Transactions help with functions and operations like creating, updating and deleting records performed in the database. Basically, transaction is a group of SQL queries that are executed on database records.

The four kinds of transaction controls are;

  • COMMIT: All changes that are made through the transaction are saved with the help of Commit.
  • ROLLBACK: As the name suggests, this control is used to roll back the transactions. The changes that are made by the transaction are reverted and database remains the same as before.
  • SET TRANSACTION: It is simply used to set the name of the transaction.
  • SAVEPOINT: Savepoint is basically used to set the point on which the transaction is to be rolled back.

24. What are the properties of transaction?

The properties of transaction are famously known as ACID properties;

  • Atomicity: It ensures that all transactions are performed completely. The task is to make sure that all transactions are completed successfully. If there is a failure point, the transaction is aborted there and then the previous transaction is rolled back to its initial stage where the changes are undone.
  • Consistency: It makes sure that all the changes that are made through successful transactions get reflected in a proper way on the database.
  • Isolation: Isolation is used to make sure that all transactions are performed independently. The changes that are made by one transaction are not reflected on other transactions.
  • Durability: Durability makes sure that all the changes that are made in the database with committed transactions do not change and persist as they are, even if there is a system failure.

25. Explain what is Auto Increment?

Auto Increment is a keyword that enables the users to create a unique number. It is generated when a new record is inserted into the table. The keyword of Auto Increment can be used in Oracle.

26. What are the local and global variables and what is the difference between them?

Local Variables: The variables that exist or are used inside the function are known as local variables. These variables are not known to any other functions and therefore cannot be used or referred. Whenever the function is called, the variables can be created.

Global Variables: The variables which exist and can be used throughout the program are called Global variables. A variable if declared in global cannot then be used in functions. Whenever the function is called, global variables cannot be created.

27. Explain what is the difference between TRUNCATE and DROP statements?

TRUNCATE: The Truncate command removes all the rows from the table. These rows cannot be rolled back later.

DROP: The Drop command removes the tables from the database. This operation also cannot be rolled back later.

28. Define collation.

In a literal sense, collation is the gathering of data in a standard order. Collation attributes to a collection of rules that encompass how character data can be classified and distinguished.

29. What do you understand by online transaction processing?

OLTP executes transactions based applications. Subsequently, these applications execute data entry, data retrieval, and data processing. OLTP executes data management into a simple and efficient method.

30. What is CLAUSE?

SQL clause restricts the result set by implementing conditions to the query. As a result, this normally distributes a few rows of the entire collection of records.

31. What do you understand by user-defined functions?

It is not essential to write the same logic multiple times. User-defined functions are the functions formulated to use the same logic whenever expected.

32. Name the types of user functions.

There are three types of user functions:

1. Scalar

2. Inline Table-Valued functions

3. Multi Statement Valued functions

33. Define what is a UNIQUE constraint?

The UNIQUE constraint helps to make sure that all values in the column are different. Therefore, there is uniqueness in the columns and even the rows can be identified uniquely. The unique constraint can be defined in multiple ways per table.

34. What is a Foreign Key?

A single field or a collection of fields in a table is a part of Foreign Key. The foreign key constraint helps in ensuring that there’s referential integrity in relation between two tables. The table which consists the foreign key constraint is labelled as the child table. On the other hand, the table which contains the candidate key is labelled the parent table or the referenced table.

35. What is Data Integrity?

Data integrity is the assurance of accuracy as well as the consistency of data over its entire life-cycle. It is a critical aspect to the design, execution and the usage of any system which stores, processes, or retrieves data. It ensures recoverability and searchability, traceability (to origin), and connectivity.

36. What are tables and fields?

An organised collection of data consisting of rows and columns is known as a table. Columns can be specified as vertical and rows can be specified as horizontal. The columns in tables are called fields.

37. What are constraints in SQL?

Constraints in SQL specify the rules concerning data in the table and can be applied to single and multiple fields in SQL using the ALTER TABLE command. The constraints in SQL are:

  • NOT NULL – This constraint limits the NULL value from being inserted into a column.
  • CHECK – Verifies and ensures that all values in a field satisfy a certain given condition.
  • DEFAULT – Automatically assigns a default value if no value has been specified for the field.
  • UNIQUE – This constraint makes sure that unique values are inserted into the field.
  • INDEX – This constraint indexes a field which helps in providing faster retrieval of records.
  • PRIMARY KEY – It has the function to uniquely identify every single record in a table.
  • FOREIGN KEY – Ensures the referential integrity for a record that is present in another table.

38. What is a Primary Key?

PRIMARY KEY is a constraint in SQL used for unique identification of tables. It should contain UNIQUE values and an implicit NOT NULL constraint. For each table in SQL, only one primary key is permitted, which comprises of single or multiple fields ( columns).

39. Does SQL support programming language features?

You should not be mistaken and get confused with SQL as a language. SQL is a language but it is not a programming language and does not support programming. It is a command language. SQL does not have any conditional statements like that in loops.

It only has commands which can be used to update the data, delete, and perform other such functions in the database. Basically, SQL helps in manipulating the data in a database.

40. State what is the difference between SQL and PL/SQL.

  • SQL is a data oriented language whereas PL/SQL is a procedural language.
  • SQL is a query execution or commanding language and PL/SQL is a complete programming language.
  • PL/SQL is procedural in nature. SQL on the other hand is extremely declarative in nature.
  • The language SQL is used for manipulating data whereas PL/SQL is used for creating applications.
  • SQL is capable of executing one statement at a time. PL/SQL on the other hand is capable of executing a block of statements in PL/SQL.
  • The use of SQL is to tell the database what to do. The use of PL/SQL is to tell the database how to do it.
  • SQL can be embedded in PL/SQL. But PL/SQL cannot be embedded in SQL.

Queries in SQL:

Frequently, queries in SQL Interview Questions get asked more than theoretical questions. Because being able to solve queries in SQL during your interview will prove to the recruiter that you know your basics and commands.

So continuing with the list, here is a set of most commonly asked queries in SQL interview questions that you must prepare:

41. How to create duplicate tables with and without data?

a) Creating a duplicate table with data:

Create table Student_Replica as Select * from Student;

b) Creating a duplicate table without data:

Create table Student_Replica as Select * from Student where 1=2;

42. Find out the data between the range.

a) Using Between..and operator

Select * from Employee where salary between 25000 and 50000;

b) Using operators (Greater than and less than)

Select * from Employee where salary >= 25000 and salary <= 50000;

43. How to calculate even records from the table?

Select * from(select rownum as rno,E.* from Student) where Mod(rno,2)=1;

44. What is the query to delete all the user tables from a specific database?

Begin

For I In

(Select * from Tabs) —Tabs is a system table in which users get the different user-defined table names.

Loop

Execute immediate (‘Drop Table ‘||i.table_name||’cascade constraints’);

End loop;

End;

45. What is the query to fetch the last day of next month in Oracle?

Select LAST_DAY (ADD_MONTHS (SYSDATE,1)) from dual;

46. How to add email validation using a single SQL statement?

SELECT

Email

FROM

Employee

where NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}’, ‘i’);

If you design your career as a SQL developer, you are sure to have multiple job opportunities because of the high demand for SQL developers. And with these question and answer, you can prepare yourself in the best way possible, and grab the job of your choice. All the best!

Comments are closed.