106 SQL Interview Question and Answer – Part 1

Jack16306
30 Min Read

SQL is a critical component of data management and analysis, making it an essential skill for anyone working in the technology industry. Soltechblog has compiled a list of SQL interview questions and answers to help aspiring professionals prepare for their interviews. Some of the most common questions include explaining the differences between SQL and NoSQL databases, discussing the importance of indexes, and understanding joins and subqueries. Additionally, candidates are often asked to demonstrate their ability to write complex SQL queries, so it is essential to have hands-on experience with SQL before an interview. By mastering these concepts and practicing writing SQL queries, candidates can confidently navigate their interviews and stand out as knowledgeable and skilled professionals.

1. SQL Interview Question

  1. What is Relational Database Management System (RDMBS)?
  2. What is Structured Query Language?
  3. What is a Database?
  4. What is primary key?
  5. What is a unique key?
  6. What is a foreign key?
  7. Explain the difference between spreadsheets and databases.
  8. What are table and fields?
  9. Explain the various SQL languages.
  10. What is normalization?
  11. What is denormalization?
  12. Explain the different types of normalization.
  13. What are views in SQL?
  14. What is join? Explain the different types.
  15. What are the different types of indexes?
  16. What is a cursor in SQL?
  17. What is query?
  18. What is a subquery?
  19. What is a trigger?
  20. Differentiate between the DELETE and TRUNCATE commands.
  21. What are local and global variables?
  22. What are constraints?
  23. What is data integrity?
  24. What is auto increment?
  25. What is a data warehouse?
  26. What is the difference between DROP and TRUNCATE statements?
  27. What are aggregate and scalar functions?
  28. What is alias in SQL?
  29. What is the difference between OLTP and OLAP?
  30. What is collation? What are the various types of collation sensitivity?
  31. How can we create tables in SQL?
  32. How can we insert data in SQL?
  33. How can we change a table name in SQL?
  34. What is SQL server?
  35. What is ETL in SQL?
  36. What are nested queries?
  37. What is the difference between CHAR and VARCHAR2 data types in SQL server?
  38. What is difference between SQL and PL/SQL?
  39. What is the difference between SQL and MySQL?
  40. What is cross join?
  41. What are user defined functions?
  42. What is a CLAUSE?
  43. What is recursive stored procedure?
  44. Explain UNION, MINUS and INTERACT commands?
  45. What TCP/IP port does SQL Server run?
  46. Which operator is used in query for pattern matching?
  47. How can we select unique records from a Table?
  48. List and explain each of the ACID properties that collectively guarantee that database transactions are processed reliably
  49. What is the main difference in the BETWEEN and IN condition operators?
  50. What are SQL functions used for?

2. SQL Interview Answer

1. What is Relational Database Management System (RDMBS)

RDBMS store data into a collection of tables, which is related by common fields between the columns of the table. It also provides relational operators to manipulate the data stored into the tables. Example: SQL Server.

2. What is Structured Query Language?

SQL stands for Structured Query Language , and it is used to communicate with the Database. This is a standard language used to perform tasks such as retrieval, updates, insertion and deletion of data from a database. Standard SQL Commands are Select.

3. What is a Database?

A Database is an organized form of data for easy access, storing, retrieval and managing of data. This is also known as structured form of data which can be accessed in many ways. Example: School Management Database, Bank Management Database

4. What is 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. This means, Primary key values cannot be NULL.

5. What is a unique key?

A Unique key constraint uniquely identifies each record in a database. This provides uniqueness for the column or set of columns. A Primary key constraint has automatic unique constraint defined on it. There can be many unique constraints defined per table, but only one Primary key constraint defined per table.

6. What is a foreign key?

A foreign key is one table which can be related to the primary key of another table. Relationships need to be created between two tables by referencing the foreign key with the primary key of another table.

7. Explain the difference between spreadsheets and databases.

Spreadsheets: A file that exists of cells in rows and columns and can help arrange, calculate and sort data. It can have numeric values, text, formulas and functions. It features columns and rows to keep inserted information legible and simple to understand. It is an electronic graph sheet.

Databases: It is an organized collection of data arranged for ease and speed of search and retrieval. It contains multiple tables. A database engine can sort, change or serve the information on the database. Basically, it is a set of information which is held in a computer.

8. What are table and fields?

A table is a set of data that are organized in a model with Columns and Rows. Columns can be categorized as vertical, and Rows are horizontal. A table has a specified number of column called fields but can have any number of rows which are called records.

Example:
Table: Employee.
Field: Emp ID, Emp Name, Date of Birth.
Data: 201456, David, 11/15/1960.

9. Explain the various SQL languages.

There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.

  • Data Definition Language (ddl)

DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc. All the commands of DDL are auto-committed which means that it permanently saves all the changes in the database.

Some commands that come under DDL: CREATE; ALTER; DROP; TRUNCATE

  • Data Manipulation Language

DML commands are used to modify the database. It is responsible for all forms of changes in the database. The commands of DML are not auto-committed which means that it can’t permanently save all the changes in the database.

Some commands that come under DML: INSERT; UPDATE; DELETE

  • Data Control Language

DCL commands are used to grant and take back authority from any database user.

Some commands that come under DCL: Grant; Revoke

  • Transaction Control Language

TCL commands can only be used with DML commands like INSERT, DELETE and UPDATE. These operations are automatically committed in the database, which is why they cannot be used while creating tables or dropping them.

Some commands that come under TCL: COMMIT; ROLLBACK; SAVEPOINT

  • Data Query Language

DQL is used to fetch the data from the database.

It uses only one command: SELECT

10. What is normalization?

Normalization is the process of minimizing redundancy and dependency by organizing fields and tables of a database. The main aim of Normalization is to add, delete or modify fields that can be made in a single table.

11. What is denormalization?

Denormalization is a technique used to access the data from higher to lower normal forms of database. It is also a process of introducing redundancy into a table by incorporating data from the related tables.

12. Explain the different types of normalization.

Some types are:

  • First Normal Form (inf): This should remove all the duplicate columns from the table. Creation of tables for the related data and identification of unique columns.
  • Second Normal Form (2NF): Meeting all requirements of the first normal form. Placing the subsets of data in separate tables and Creation of relationships between the tables using primary keys.
  • Third Normal Form (3NF): This should meet all requirements of 2NF. Removing the columns which are not dependent on primary key constraints.
  • Fourth Normal Form (4NF): Meeting all the requirements of third normal form and it should not have multi- valued dependencies.

13. What are views in SQL?

A view is a virtual table which consists of a subset of data contained in a table. Views are not virtually present, and it takes less space to store. View can have data of one or more tables combined, and it is depending on the relationship.

14. What is join? Explain the different types.

This is a keyword used to query data from more tables based on the relationship between the fields of the tables. Keys play a major role when JOINs are used.

There are various types of joins which can be used to retrieve data and it depends on the relationship between tables.

  • Left Outer Join: If we want all the records from left table and only matching records from right table then will use left outer join/left join.
  • Right Outer Join: If we want to display all the records from right table and only matching records from left table then will right outer join/right join.
  • Full Outer Join: If we want to display all the records from both the tables then will use full outer join.
  • Inner Join: If we want only the matching records from both the tables then will use Inner join/Simple join.

15. What are the different types of indexes?

An index is a performance tuning method of allowing faster retrieval of records from the table. An index creates an entry for each value and makes it 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.

16. What is a cursor in SQL?

A database Cursor is a control which enables traversal over the rows or records in the table. This can be viewed as a pointer to one row in a set of rows. Cursor is very much useful for traversing such as for retrieval, addition and removal of database records.

17. What is query?

A DB query is a code written in order to get the information back from the database. Queries can be designed in such a way that it matches with our expectation of the result set.

18. What is a subquery?

A subquery is a query within another query. The outer query is called as main query, and inner query is called subquery. SubQuery is always executed first, and the result of subquery is passed on to the main query.

There are two types of subquery – Correlated and Non­Correlated.

A correlated subquery cannot be considered as an independent query, whereas a Non-Correlated sub query can be considered as independent query and the output of subquery are substituted in the main query.

19. What is a trigger?

A DB trigger is a code or programs that automatically execute with response to some event on a table or view in a database. Mainly, trigger helps to maintain the integrity of the database.

Example: When a new student is added to the student database, new records should be created in the related tables such as the Exam, Score and Attendance tables.

20. Differentiate between the 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.

21. What are local and global variables?

  • Local variables are the variables which can be used or exist inside the function. They are not known to the other functions and those variables cannot be referred to or used. Variables can be created whenever that function is called.
  • Global variables are the variables which can be used or exist throughout the program. Same variable declared in global cannot be used in functions. Global variables cannot be created whenever that function is called.

22. What are constraints?

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.

23. What is data integrity?

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

24. What is auto increment?

Auto increment keyword allows the user to create a unique number to be generated when a new record is inserted into the table. AUTO INCREMENT keyword can be used in Oracle and IDENTITY keyword can be used in SQL SERVER.

25. What is a data warehouse?

Data warehouses are a central repository of data from multiple sources of information. This data is consolidated, transformed and made available for the mining and online processing. Warehouse data have subsets of data called Data Marts.

26. What is the difference between DROP and TRUNCATE statements?

TRUNCATE removes all the rows from the table, and it cannot be rolled back. DROP command removes a table from the database and operation cannot be rolled back

28. What are aggregate and scalar functions?

Functions are methods used to perform data operations. SQL has many in-built functions used to perform string concatenations, mathematical calculations etc.

SQL functions are categorized into the following two categories: Aggregate Functions and Scalar Functions.

  • Aggregate SQL Functions

The Aggregate Functions in SQL perform calculations on a group of values and then return a single value. Following are a few of the most commonly used Aggregate Functions:

Function Description
SUM() Used to return the sum of a group of values.
COUN() Returns the number of rows either based on a condition, or without a condition.
AVG() Used to calculate the average value of a numeric column.
MI() This function returns the minimum value of a column.
MA() Returns a maximum value of a column.
FIRS() Used to return the first value of the column.
LAST() This function returns the last value of the column.
  • Scalar SQL Functions

The Scalar Functions in SQL are used to return a single value from the given input value. Following are a few of the most commonly used Scalar Functions:

Function Description
LCAS() Used to convert string column values to lowercase
UCAS() This function is used to convert a string column values to Uppercase.
LEN() Returns the length of the text values in the column.
MlD() Extracts substrings in SQL from column values having string data type.
ROUN() Rounds off a numeric value to the nearest integer.
NOW() This function is used to return the current system date and time.
FORMAT() Used to format how a field must be displayed.

29. What is alias in SQL?

SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.

  • What is the difference between OLTP and OLAP?
  • OLAP

Online Analytical Processing, a category of software tools which provide analysis of data for business decisions. OLAP systems allow users to analyze database information from multiple database systems at one time.

The primary objective is data analysis and not data processing.

  • OLTP

Online transaction processing shortly known as OLTP supports transaction-oriented applications in a 3- tier architecture. OLTP administers day to day transaction of an organization.

The primary objective is data processing and not data analysis. Unlike OLAP systems, the goal of OLTP systems is serving real-time transactions.

30. What is collation? What are the various types of collation sensitivity?

Collation is defined as a set of rules that determine how character data can be sorted and compared.

ASCII value can be used to compare these character data.

  • Case sensitivity: A and a are treated differently.
  • Accent sensitivity: a and á are treated differently.
  • Kana sensitivity: Japanese kana characters Hiragana and Katakana are treated differently.
  • Width sensitivity: Same character represented in single-byte (half-width) and double-byte (full­width) are treated differently.

31. How can we create tables in SQL?

The command to create a table in SQL is extremely simple:

We will start off by giving the keywords, CREATE TABLE, then we will give the name of the table. After that in braces, we will list out all the columns along with their data types.

For example, if we want to create a simple employee table:

CREATE TABLE employee ( name varchar(25), age int, gender varchar(25), ….);

32. How can we insert data in SQL?

It is possible to write the INSERT INTO statement in two ways:

  • Specify both the column names and the values to be inserted:

INSERT INTO table_name (column!, column2, column3, …)

VALUES (value!, value2, value3, …);

  • If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the INSERT INTO syntax would be as follows:

INSERT INTO table_name

VALUES (value!, value2, value3, …);

33. How can we change a table name in SQL?

We will start off by giving the keywords ALTER TABLE, then we will follow it up by giving the original name of the table, after that, we will give in the keywords RENAME TO and finally, we will give the new table name.

For example, if we want to change the “employee” table to “employee_information”, this will be the command:

ALTER TABLE employee

RENAME TO employee_information;

34. What is SQL server?

SQL server has stayed on top as one of the most popular database management products ever since its first release in 1989 by Microsoft Corporation. The product is used across industries to store and process large volumes of data. It was primarily built to store and process data that is built on a relational model of data.

SQL Server is widely used for data analysis and also scaling up of data. SQL Server can be used in conjunction with Big Data tools such as Hadoop.

SQL Server can be used to process data from various data sources such as Excel, Table, .Net Framework application, etc.

35. What is ETL in SQL?

ETL stands for Extract, Transform and Load. It is a three step process, where we would have to start off by extracting the data from sources. Once we collate the data from different sources, we have our raw data. This raw data has to be transformed into a tidy format, which will come in the second phase. Finally, we would have to load this tidy data into tools which would help us to find insights.

36. What are nested queries?

Triggers may implement DML by using INSERT, UPDATE, and DELETE statements. These triggers that contain DML and find other triggers for data modification are called Nested Triggers.

37. What is the difference between CHAR and VARCHAR2 data types in SQL server?

When stored in a database, varchar2 uses only the allocated space. E.g. if you have a varchar2(1999) and put 50 bytes in the table, it will use 52 bytes.

But when stored in a database, char always uses the maximum length and is blank-padded. E.g. if you have char(1999) and put 50 bytes in the table, it will consume 2000 bytes.

38. What is difference between SQL and PL/SQL?

SQL is a Structured Query Language to create and access databases whereas PL/SQL comes with procedural concepts of programming languages.

39. What is the difference between SQL and MySQL?

SQL is a Structured Query Language that is used for manipulating and accessing the relational database. On the other hand, MySQL itself is a relational database that uses SQL as the standard database language.

40. What is cross join?

Cross join is a Cartesian product where number of rows in the first table multiplied by number of rows in the second table.

41. What are user-defined functions?

User-defined functions are the functions written to use that logic whenever required. It is not necessary to write the same logic several times. Instead, function can be called or executed whenever needed.

42. What is a CLAUSE?

SQL clause is defined to limit the result set by providing condition to the query. This usually filters some rows from the whole set of records.

Example – Query that has WHERE condition.

43. What is recursive stored procedure?

This is a stored procedure which calls by itself until it reaches some boundary condition. This recursive function or procedure helps programmers to use the same set of code any number of times.

44. Explain UNION, MINUS and INTERACT commands?

  • UNION operator is used to combine the results of two tables, and it eliminates duplicate rows from the tables.
  • MINUS operator is used to return rows from the first query but not from the second query. Matching records of first and second query and other rows from the first query will be displayed as a result set.
  • INTERSECT operator is used to return rows returned by both the queries.

45. What TCP/IP port does SQL Server run?

By default, SQL Server runs on port 1433.

46. Which operator is used in query for pattern matching?

LIKE operator is used for pattern matching, and it can be used with:

  • % – Matches zero or more characters.
  • _ (Underscore) – Matching exactly one character.

47. How can we select unique records from a Table?

Select unique records from a table by using DISTINCT keyword.

48. List and explain each of the ACID properties that collectively guarantee that database transactions are processed reliably.

ACID Properties are used for maintaining the integrity of database during transaction processing. ACID in DBMS stands for Atomicity, Consistency, Isolation, and Durability.

  • Atomicity: A transaction is a single unit of operation. You either execute it entirely or do not execute it at all. There cannot be partial execution.
  • Consistency: Once the transaction is executed, it should move from one consistent state to another.
  • Isolation: Transaction should be executed in isolation from other transactions. During concurrent transaction execution, intermediate transaction results from simultaneously executed transactions should not be made available to each other.
  • Durability: After successful completion of a transaction, the changes in the database should persist, even in the case of system failures.

49. What is the main difference in the BETWEEN and IN condition operators?

BETWEEN operator is used to display rows based on a range of values in a row whereas the IN condition operator is used to check for values contained in a specific set of values.

  • Example of BETWEEN: SELECT * FROM Students where ROLL_NO BETWEEN 10 AND 50;
  • Example of IN: SELECT * FROM students where ROLL_NO IN (8,15,25);

50. What are SQL functions used for?

SQL functions are used for the following purposes:

  • To perform some calculations on the data
  • To modify individual data items
  • To manipulate the output
  • To format dates and numbers
  • To convert the data types

Share this Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *