SQL Interview Questions and Answers for Freshers

SQL Interview Questions and Answers for Fresher as well as Experienced Database Developers, Database Testers, and Database Administrators.

SQL Interview Questions and Answers on Relational Database Management System Fundamentals, SQL Language Fundamentals, and SQL Queries & Subquaries.

SQL Interview Questions and Answers

1. What is SQL?

SQL – Structured Query Language, an ANSI (American National Standards Institute) Standard language for accessing databases and It was initially developed at IBM in the 1970s.

SQL is the standard language to communicate with relational database management systems like Oracle, MS Access, MS SQL Server, MySQL, DB2, Sybase Etc…

SQL is used to Create New Databases, New Tables in a Database, Insert records in a Database, Update records in a Database, and Delete records in a Database.

SQL is used to retrieve data from a Database, Execute queries against a Database, Create stored procedures in a Database, Create views in a Database, Set permissions on tables, procedures, and views.

2. What is the difference between SQL, SQL Server, and MySQL?

SQL – Structured Query Language used to manage the relational databases like Oracle, MS SQL Server, MySQL, Sybase, etc,

SQL Server and MySQL are relational database management systems used to store, retrieve, modify and administer a database using SQL.

MS SQL Server is a commercial Database Management System whereas MYSQL is an Open Source software.

3. What are the different subsets of SQL?

The important subsets of SQL are:

i. DDL – Data Definition Language – It allows you to perform various operations on the database such as CREATE, ALTER, and DELETE objects.

ii. DML – Data Manipulation Language – It allows you to access and manipulate data. It helps you to insert, update, delete and retrieve data from the database.

iii. DCL – Data Control Language – It allows you to control access to the database. Example – Grant, Revoke access permissions.

4. What do you mean by DBMS? What are its different types?

A Database Management System (DBMS) is a software application that interacts with the user, applications, and the database itself to capture and analyze data.

A DBMS allows a user to interact with the database. The data stored in the database can be modified, retrieved and deleted and can be of any type like strings, numbers, images, etc.

There are two types of DBMS:

Relational Database Management System/SQL Database: The data is stored in relations (tables). Example – Oracle

Non-Relational Database Management System/NoSQL Database: There is no concept of relations. Example – MongoDB

5. What is the difference between DELETE and TRUNCATE statements?

Delete – Delete is a DML (Data Manipulation Language) command, it is used to delete a row in a table, we can roll back data after using delete statement, and It is slower than truncate statement.

Truncate – Truncate is a DDL (Data Definition Language) command, it is used to delete all the rows from a table, we cannot roll back data, and It is faster.

6. What do you mean by the table, field, and record in SQL?

A table refers to a collection of data in an organised manner in form of rows and columns where the rows are known as records and the columns are known as fields.

7. What are the important SQL language elements?

The important SQL Language elements are:

1. Identifiers:
Names of Database Objects such as Tables, Views, Columns, etc,

2. Data Types:
Define the type of data that is contained by a Column

3. Constants:
Symbols that represent specific data types

4. Operators:
Perform Arithmetic, Comparison, and Logical Operations

5. Functions: Built-in Functions to perform specific operations

6. Clauses:
Constituent components of statements and queries.

7. Expressions:
Produce scalar values, or tables containing columns and rows of data.

8. Queries:
Retrieve the data based on specific criteria, this an important element of SQL.

9. Statements Etc…

8. What is the difference between CHAR and VARCHAR data types in SQL?

Both Char and Varchar are used for characters datatype but varchar is used for character strings of variable length whereas Char is used for strings of fixed length.

CHAR Datatype:
It is a datatype in SQL which is used to store character strings of fixed length specified. If the length of the string is less than set or fixed-length then it is padded with extra blank spaces so that its length became equal to the set length.

VARCHAR Datatype:
It is a datatype in SQL which is used to store character string of variable length but a maximum of the set length specified. If the length of the string is less than set or fixed-length then it will store as it is without padded with extra blank spaces.

9. What is an Index in SQL? What are the different types of indexes in SQL?

In most SQL projects the data is in millions and billions. Because of the huge volume of the data performance of SQL statements degrades. So there are a lot of different ways to improve the performance of the application, the performance of reports, or the performance of SQL queries.

Definition Of Index:

An index refers to a performance tuning method of allowing faster retrieval of records from the table. An index creates an entry for each value and hence it will be faster to retrieve data.

There are different types of Indexes in SQL:

1. Clustered Index
2. Non-Clustered Index
3. Unique Index
4. Filtered Index
5. Columnstore Index
6. Hash Index
Etc,

10. What do you mean by data integrity?

Data Integrity defines the accuracy as well as the consistency of the data stored in a database. It also defines integrity constraints to enforce business rules on the data when it is entered into an application or a database.

11. What is a Query in SQL? What are the parameters of SQL Queries?

SQL Query?
A query is a question or inquiry about a set of data. We use Structured Query Language (SQL) to retrieve meaningful and relevant information from databases. When building a structure, we pull data from tables and fields.

An SQL query typically includes Query parameter, Column list, Sort list, Options, and Format.

Example:

SQL > Select * from employee where department_id=10 and salary>3500

12. What is key in SQL? What are the different types of Keys in SQL?

A Key in DBMS is an attribute or set of attributes that helps you to identify a row(tuple) in a relation(table). They allow you to find the relation between two tables.

different types of Keys in SQL:

1. Super Key – A super key is a group of single or multiple keys which identifies rows in a table.

2. Primary Key – is a column or group of columns in a table that uniquely identifies every row in that table.

3. Candidate Key – is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with no repeated attributes.

4. Alternate Key – is a column or group of columns in a table that uniquely identifies every row in that table.

5. Foreign Key – is a column that creates a relationship between two tables. The purpose of Foreign keys is to maintain data integrity and allow navigation between two different instances of an entity.

6. Compound Key – has two or more attributes that allow you to uniquely recognize a specific record. It is possible that each column may not be unique by itself within the database.

7. Composite Key – is a combination of two or more columns that uniquely identify rows in a table. The combination of columns guarantees uniqueness, though individual uniqueness is not guaranteed.

8. Surrogate Key – An artificial key that aims to uniquely identify each record is called a surrogate key. These kinds of keys are unique because they are created when you don’t have any natural primary keys.

13. What is the difference between DROP and TRUNCATE commands?

‘DROP’ command removes a table and it cannot be rolled back from the database whereas the ‘TRUNCATE’ command removes all the rows from the table.

14. What is Normalization and what are the advantages of it?

Normalization in SQL is the process of organizing data to avoid duplication and redundancy. Some of the advantages of Normalization are:

• Better Database organization
• More Tables with smaller rows
• Efficient data access
• Greater Flexibility for Queries
• Quickly find the information
• Easier to implement Security
• Allows easy modification
• Reduction of redundant and duplicate data
• More Compact Database
• Ensure Consistent data after modification

15. What is the ACID property in a database?

ACID stands for Atomicity, Consistency, Isolation, Durability. It is used to ensure that the data transactions are processed reliably in a database system.

Atomicity: Atomicity refers to the transactions that are completely done or failed where transaction refers to a single logical operation of data. It means if one part of any transaction fails, the entire transaction fails and the database state is left unchanged.

Consistency: Consistency ensures that the data must meet all the validation rules. In simple words, you can say that your transaction never leaves the database without completing its state.

Isolation: The main goal of isolation is concurrency control.

Durability: Durability means that if a transaction has been committed, it will occur whatever may come in between such as power loss, crash, or any sort of error.

16. What are Entities and Relationships?

Entities: A person, place, or thing in the real world about which data can be stored in a database. Tables store data that represents one type of entity.

For example – A bank database has a customer table to store customer information. The customer table stores this information as a set of attributes (columns within the table) for each customer.

Relationships: Relation or links between entities that have something to do with each other.

For example – The customer name is related to the customer account number and contact information, which might be in the same table. There can also be relationships between separate tables (for example, customer to accounts).

17. What are joins in SQL?

A JOIN clause is used to combine rows from two or more tables, based on a related column between them. It is used to merge two tables or retrieve data from there.

There are 4 types of joins,

Inner join: Inner Join in SQL is the most common type of join. It is used to return all the rows from multiple tables where the join condition is satisfied.

Left Join: Left Join in SQL is used to return all the rows from the left table but only the matching rows from the right table where the join condition is fulfilled.

Right Join: Right Join in SQL is used to return all the rows from the right table but only the matching rows from the left table where the join condition is fulfilled.

Full Join: Full join returns all the records when there is a match in any of the tables. Therefore, it returns all the rows from the left-hand side table and all the rows from the right-hand side table.

18. What do you mean by “Trigger” in SQL?

Trigger in SQL is a special type of stored procedure that is defined to execute automatically in place or after data modifications. It allows you to execute a batch of code when an insert, update, or any other query is executed against a specific table.

19. Are NULL values the same as that of zero or a blank space?

A NULL value is not at all same as that of zero or a blank space. The NULL value represents a value that is unavailable, unknown, assigned, or not applicable whereas zero is a number and blank space is a character.

20. What is a subquery in SQL?

A subquery is a query inside another query where a query is defined to retrieve data or information back from the database.

In a subquery, the outer query is called as the main query whereas the inner query is called subquery.

Subqueries are always executed first and the result of the subquery is passed on to the main query. It can be nested inside a SELECT, UPDATE or any other query.

A subquery can also use any comparison operators such as >,< or =.

There are two types of subquery namely, Correlated and Non-Correlated.

Correlated subquery: These are queries that select the data from a table referenced in the outer query. It is not considered as an independent query as it refers to another table and refers to the column in a table.

Non-Correlated subquery: This query is an independent query where the output of the subquery is substituted in the main query.

21. What is the SELECT statement?

SELECT is a SQL command that is used for selecting data from a database. The data which is returned is saved in a result table, called the result-set.

Example:
SELECT EMP_ID FROM EMPLOYEE;

Note: In the above SQL statement, ‘EMP_ID’ is a column and ‘EMPLOYEE’ is a Table.

22. What is a relationship and what are they?

Solution:- Relationship in DBMS is the interconnection of the tables within the database. There are various relationships, and they are as follows:

One to One Relationship.
One to Many Relationship.
Many to One Relationship.
Self-Referencing Relationship.

23. What is a stored procedure?

A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. Stored Procedure is a function that consists of many SQL statements to access the database system.

Stored procedure supports faster execution instead of executing multiple queries. This reduces network traffic and provides better security to the data.

The disadvantage is that it can be executed only in the Database and uses more memory for storage.

24. What is CLAUSE in SQL?

SQL clause helps to limit the result set by providing a condition to the query. A clause helps to filter the rows from the entire set of records.

For example – WHERE, HAVING clause.

25. What is a View? What are Views used for?

A view is a virtual table that consists of a subset of data contained in a table. Since views are not present, it takes less space to store. The view can have data of one or more tables combined and it depends on the relationship.

A view refers to a logical snapshot based on a table or another view.

It is used for the following reasons:

• Restricting access to data.
• Making complex queries simple.
• Ensuring data independence.
• Providing different views of the same data.


SQL Step by Step Video Tutorial – Full Course 

SQL Videos

SQL Syllabus

SQL Tutorial for Beginners

SQL Queries Examples

SQL Online Test/Quiz

Follow me on social media: